C++ Decision Making Statements

C++ Decision Making Statements


Decision Making in C++ (if , if..else, Nested if, if-else-if )
Decision-making is about deciding the order of execution of statements based on certain conditions or repeating groups of statements until certain specified conditions a met.
C++ handles decision making by supporting the following statements are as follows:
1. if statement
2. if.. else statement
3. switch statement
4. Jump Statements:
  1. Break 
  2. Continue
  3. goto
  4. return
1.if statement
The if statement used to specify a block of code to be executed, if the same condition is false.
The Syntax of else statement is as follows:
if (condition) {
  // if condition is true then block of code to be executed
} else{
  // if condition is false then block of code to be executed
}
Example
Output
2.if else statement
C++ if-else statement used to specify a new condition if the first condition is false.
The Syntax of else-if statement is as follows:
if (condition1) {
  // if condition1 is true then block of code to be executed now.
else if (condition2) {
  // if condition1 is false then block of code to be executed now.

else {
  // if condition1 and condition2 is false then block of code to be executed now.
}
 
Example
Output
3.Switch Statement
The switch statement used to specify many alternative blocks of code to be executed. In other words, The Switch statement is used to perform different actions based on different conditions like on or off.
The Syntax of Switch statement is as follows:
switch (expression)
{
    case condition1: // code to be executed if expression = condition 1;
        break;
    case condition2: // code to be executed if expression = condition 2;
        break;
    default: // code to be executed if expression doesn't match any cases
}
How does the switch statement work?
The expression is evaluated once and compared with the values of each case condition.
If there is a match, the corresponding code after the matching condition is executed. For example, if the value of the variable is equal to expression = 1, the code after expression = 2: is executed until the break statement is encountered.
If there is no match, the code after default: is executed.
Example
Output
4.Jump Statement
Jumping statement unconditionally transfer program control within a function. C++ language provides us multiple statements through which we can transfer the control anywhere in the program.
There are jump statement is as follows:
  1. Break 
  2. Continue
  3. goto
  4. return
i.Break 
In Computer Programming, The break statement terminates loop immediately when it is encountered, Simply comes out of the loop. Immediate after the closing brace of loop will be executed. 
The Syntax of the Break statement is as follows:
                   Break;
Example
Output
ii. Continue Statement 
The continue statement skips some statements inside the loop. Loop will be restarted when continue is encountered.
The Syntax of the continue statement is as follows:
                    Continue;
Example
Output
iii. Return Statement 
C++ The return keyword returns a value after completing its assigned task. Only stored in the variable is passed to the function.
Example :
http://web.webdesigninghouse.com/admin/upload/699040042_rk.png
Output :
http://web.webdesigninghouse.com/admin/upload/2024704470_rk%20output.png
iv. goto Statement 
  • It is well known as ‘the jumping statement’. It is primarily used to transfer control of execution to any place in a program. It is useful to provide branching within a loop.
  • When the loops are deeply nested at that if an error occurs then it is difficult to get excited from such loops. Simple break statement cannot work here properly. In this situations, goto statement is used. A goto statement in the C++ programming language provides an unconditional jump from goto a labeled statement in the same function.
The Syntax for goto statement is as follows:
goto label;
..
.
label statement;
Here Label can be any pain text except the C++ keyword and it can be set anywhere in the C++ program above.
Example
  
Output