C++ Decision Making Statement

Decision-Making Statements are those statements in C++ Programming Language which allows you to make the decision based on certain condition to determine the order in which statement has to be executed, or repeat a group of statements until the specified conditions are met.

C programming language provides the following types of decision making statements.

  • if statement 
  • if…else statement 
  • nested if statements 
  • switch statement 
  • nested switch statements 

if statement 

An if statement contains a Boolean expression and block of statements enclosed within.
Structure of if statement :

if…else statement 

If the Boolean expression is true then statement block is executed otherwise (if false) program directly goes to the next statement without executing Statement block.
Structure of if…else

If the condition is true, then the compiler will execute the if the block of statements, if false then else block of statements will be executed.
We can use multiple if-else for one inside other this is called Nested if-else.

switch statement 

A switch statement is used instead of nested if…else statements. It is multiple branch decision statements. A switch statement tests a variable with a list of values for equivalence. Each value is called a case.
The case value must be a constant integer.

Structure of switch() statement :

Individual case keyword and a Semi-colon (:) is used for each constant. Switch tool is used for skipping to a particular case, after jumping to that case it will execute all statements from cases beneath that case this is called as “Fall Through”.
In the example below, for example, if the value 2 is entered, then the program will print two one something else!

Example

To avoid fall through, the break statements are necessary to exit the switch. If value 4 is entered, then in case 4 it will just print four and ends the switch. The default label is non-compulsory, It is used for cases that are not present.

Question-related to C++ programming