What are Statements in C++?
Statements in C++ are declarations to your computer that it needs to perform an action. When your computer runs across a statement it will immediately understand that it is being told to do something specific. It is being given instructions. So that’s what C++ statements are. They are instructions that your computer can recognize and execute on command. Those instructions can be many different things and can be carried out in many many different ways. Luckily for us, to program, we don’t have to understand all the nitty-gritty little intricacies of how the computer understands these statements. We only have to know what actions a computer takes when it comes across them.
Why Are They Called Statements?
Statements are used in many programming languages. The programming languages that utilize them are called imperative languages. The definition of imperative is understood to be:
of the nature of or expressing a command; commanding.
Putting two and two together we can gather that imperative programming languages use commands in some way. It’s in the definition. They command your computer to perform an action. The way they do this, as you might have already guessed, is by utilizing the aforementioned statements.
What are the Different Kinds of Statements in C++?
There are many different kinds of statements. This guide will focus on C++ but, naturally, different imperative languages won’t use statements in the exact same ways. C++, like most imperative languages, has different types of statements and within those types are specific statements that alter the way the types are utilized. For instance, both what are known as “if” and “else” statements are considered to be of the conditional type. While they are the same type they do not function the same way. Knowing this lets take a look at the list of the different kinds of statements below and see what each specific instruction does.
Assignment Statements
Before we get in to the more advanced kinds of statements let’s look at a more trivial kind. This kind is known as an assignment statement and its only purpose is to assign values stored in variables. For example, x = 5 would be considered an assignment statement because it sets the stored value of x to 5. While it isn’t particularly required to know the definition of these it is good to know that it is considered a statement.
Conditional Statements
Conditional statements are just as they sound. They will run code within the body of the statement if the condition is true and will skip over it if not. The condition must be set by the programmer then he or she may declare what the computer must do. Let’s take a look at the syntax and some example codes for these.
- if – (example) – an if statement will run the code within its body only if the condition is true.
The basic syntax for the if statement is:
... if(condition is true) { run code } ...
- else – (example)- the else statement is similar to the if statement but is the default code that will run if the condition is false. Is is similar to the default statement mentioned later but is generally used with an if.
The basic syntax for the else statement is:
... if(condition is true) { run code } ...else { run this code instead } ...
- switch (example) – the switch statement is a conditional case that works off values called “cases”. A variable is tested against the values of these cases and if any match the code for that case will be executed.
The basic syntax for a switch statement is:
... switch(variable) { case 1: run this code; break; case 2: run this code; break; ...case n: run this code; break; default: default code; //program will default to this code } ...
Notice another statement must be used in the above code called a break statement. We call this kind of statement a “jump” statement. We also use what is known as a default statement. This is similar to the else statement in that if none of the case values are satisfied it will default to whatever code is within the default body instead.
Jump Statements
Jump statements basically result in a “jump” from one part of your program to another. They are generally used to exit loops and for switch statements. These are particularly useful if you want to be able to jump to a specific part of your program on demand. The Jumps unconditional and if the program meets one of them the jump will happen no matter what.
- break (see switch example program) – the break statement basically breaks the control of the loop. The program exits the loop of switch instantly when the break statement is met.
The basic syntax for a break statement looks similar to:
... loop(condition) { ... break; //loop will be exited here and no further code in this loop will be executed ... } ...
- continue (example)- the continue statement breaks a particular iteration of a loop for a specified condition. When the condition statement is met by the program it will immediately jump to a new iteration of a loop.
The basic syntax for the continue statement looks like this:
loop(condition) { ... continue; //program immediately jumps back to the very beginning of the loop ... }
- return (example) – the return statement immediately transfers control back to the calling function. In the case that you return out of the main function the control goes back to your OS. Make sure you use return 0 with an integer value and return with void since int functions require a return type with an expression while void functions do not.
Syntax for using the return statement would look similar to:
int main() //calling function ... { int function_within_main() //function being called { ... return 0; //immediately returns to the main function } ... }
- goto (example) – the goto statement immediately jumps to a specified point within a function. Note that it cannot jump to another function.
Syntax for using the goto statement looks something like:
... function() { ... goto point_name; //program jumps to whatever point you named here ... point_name: //this is your named point some code; ... }
Iterative Statements
Iterative statements, commonly called “loops”, will run a block of code multiple times based on set conditions. One thing programmers have to watch out for when using iterative statements is infinite loops. Infinite loops are the result of the conditional statement never becoming false within your loop. When using iterative statements it is imperative that you pay close attention to your set conditions. A very useful feature when using iterative statements is what is known as nesting. Nesting is basically putting one iterative statement within another. For more on that, check out this example and explanation of nesting a while loop.
- while (example)– the while statement is an iterative statement referred to as a loop. While statements run continuously until the set condition returns false.
The basic syntax of a while loop looks similar to:
... while(condition is true) { SOME CODE; //repeat until condition is no longer true } ...
- do-while (example) – the do while loop is very similar to the while loop. The difference between the two is that the do-while loop will run the code within the loop first BEFORE checking the condition.
The basic syntax of a do-while loop looks similar to:
... do { SOME CODE; //as you can see it will run the code first } while(condition is true); //then check if the condition is true ...
- for (example) – the for loop is another iterative statement. The main advantage of using the for loop is that an initial condition, a conditional statement, and an iteration statement can all be set at the beginning. The for loop will continue to run as long as the conditional statement is true.
The basic syntax for a for loop is similar to:
... for(initial condition; conditional statement; iteration statement) { SOME CODE; //this and the iteration statement repeatedly runs as long as the conditional statement is true } ...