Conditional statement enables a program to execute only specific block of code. There are two type of conditional statements in general:
if statement specifies a block of code to be executed when a condition is true.
Syntax:
if (condition) { // block of code to be executed if the condition is true }
Example:
#include<iostream> int main() { int myNum = 10; if(myNum == 10){ std::cout << "myNum is 10"; } return 0; }
In the example, the code std::cout << "myNum is 10";
will only be executed when the value of myNum is 10.
However, in other cases, we need another statement to specify code to be executed
when the condition is false. Then, we can use else for this case.
Syntax:
if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false }
Example:
#include<iostream> int main() { int myNum = 10; if(myNum == 10){ std::cout << "myNum is 10"; }else{ std::cout << "myNum is not 10"; } return 0; }
Lastly, the else if statement is used when there are more than two conditions that may occur.
else if statement specifies a new condition if the first condition is false.
Syntax:
if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false }
Example:
#include<iostream> int main() { int myNum = 10; if(myNum < 1){ std::cout<<"myNum is less than 1"; }else if (myNum<10){ std::cout<<"myNum is less than 10"; }else{ std::cout<<"myNum is greater than or equal with 10"; } return 0; }
Short-hand if-else, also called ternary operator,
is a simple construction of an if-else statement.
The ternary operator is used to simplify a block of
if-else statements to a single line of code.
Syntax:
variable = (condition) ? expressionTrue : expressionFalse;
Below is the simplified version of the previous
if-else example:
#include<iostream> #include<string> int main() { int myNum = 10; string result = (myNum == 10)? "myNum is 10" : "myNum is not 10"; std::cout<<result; }
Besides the if-else statement, there is also the switch statement.
The switch statement is used when there are too many conditions involving one variable.
Instead of using many if-else statements, you can use the switch statement.
Syntax:
switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
The expression in the code specifies which variable that will be inspected.
If the expression fits the case x, then the program runs the first code block, etc.
If the expression does not fit in any of the cases, it will run the default code block.
The break statement at the end of each code block is used to
breaks out the switch block and stops the execution.
default code block is optional.
Example:
#include<iostream> int main() { int day = 2; switch (day) { case 1: std::cout<<"Monday"; break; case 2: std::cout<<"Tuesday"; break; case 3: std::cout<<"Wednesday"; break; case 4: std::cout<<"Thursday"; break; case 5: std::cout<<"Friday"; break; case 6: std::cout<<"Saturday"; break; case 7: std::cout<<"Sunday"; break; } return 0; }
As its name says, loops are used to loop codes. Loops enable a code to run endlessly as long as it still corresponds with the specified condition. There are three types of loops in C++:
While loop loops through a block of code as
long as the specified condition is true.
Syntax:
while (condition) { // code block to be executed }
Example:
#include<iostream> int main() { int a = 1; while(a<5){ std::cout<<"a is " << a << endl; a++; } return 0; }
The loop in the example will produce the output below:
Output:
a is 1
a is 2
a is 3
a is 4
Notice that the loop while keeps looping until a < 5.
When the loop reaches a = 5, it will stop looping and the program is terminated.
Do-while loop is a variant of the while loop.
But, the different is do-while loop will execute once before
checking if the condition is true or not.
While loop does not run when the condition is false,
while do-while loop runs at least once in every condition
(even if the condition is false).
Syntax:
do { // code block to be executed }while (condition);
Example:
#include<iostream> int main() { int a = 1; do{ std::cout<<"Hello!"; a++; } while (a<1); return 0; }
The example loop above will still run once, even if the condition is false.
If we use a while loop in this example, the loop will not run.
For loop is used when we know how many times we want to loop through a block.
Syntax:
for (statement 1; statement 2; statement 3) { // code block to be executed }
Example:
#include<iostream> int main() { for(int i = 0; i<5 ; i++){ std::cout<<"Hello!"; } return 0; }
The output of the program above will be Hello repeated five times.
Code explanation:
i = 0i = 5),
the loop will stop (because the condition of i < 5 is not true anymore).
As seen before, a break statement is used to jump out of a switch statement.
A break statement can also be used to jump out of a loop.
Example:
#include<iostream> int main() { int a = 1; while(a<10){ std::cout<<"Hello!"; if(a == 4){ break; } a++; } return 0; }
The code above will produce the output of Hello! printed 4 times.
When the variable a reaches 4, it will automatically jump out of the loop,
resulting in only 4 printed outputs.
The continue statement is used to skip the rest of the code and jump
to the next iteration in the loop.
Example:
#include<iostream> int main() { for(int i=0; i<5; i++){ if(i == 3){ continue; } std::cout<<i; } return 0; }
If we run the code above, we will get the output
0124 with no '3'. This is because when i reaches 3,
we use the continue statement to skip the rest of the loop and
move on to the next iteration (i = 4).