.

Decision Making Statements in C++

The conditional statement which allows to take decision based on the result of the condition. These statements are called as conditional statements or decision making.

The program is executed in the written order when no jump occurs in the program. When there is a specific task involved the change of order of execution takes place. This involves the kind of decision making. Based on the condition the output may be true or false.

There are different decision making statements. They are

  • if statement
  • if else statement
  • if then else statement
  • goto statement
  • switch statement
decision making statements img1

Figure : Decision making statement

If statements

This statement is used to control the flow of the program based on some condition. The block of statement is executed when the result of decision is true, but when it is false the statement inside the code is skipped. If statement may change its structure based on the situation and complexity. There are different types of if statements are

  • simple if statement
  • if else statement
  • nested if statement
  • else if ladder

Syntax

Example:

decision making statements img2

Figure : Flowchart for if statement

If…else Statement

This statement is used to control the flow of the program based on some condition. The block of statement is executed when the result of decision is true, but when the result is false else part is executed. It is used to execute the code developed based on the situation.

The syntax for if else statement is

decision making statements img3

Figure : Flowchart to if else statement

Example

If...else if...else Statement

When there is a multiple decisions else if statements is used in C++. There is if statement with else statement, if condition is written inside other if statement.

Syntax

Example

Switch statement

The switch statement is executed inside the decision making statement. Each condition is executed within the loop and there is a break statement between the decisions. When the compiler executes the break command the task is switched over to next command.

Syntax

Example

Nested if statement

A nested if statement is a statement that has if statement present within another if statement. The if statement is present in the body of another if statement.

Syntax

Example

The ? : Operator(conditional operator)

This is the conditional operator that is used in C++ to return the values that depends on result of the expression.

Syntax

	(expression 1) ? expression 2 : expression 3 
	If expression 1 evaluates to true, then expression 2 is evaluated.
	If expression 1 evaluates to false, then expression 3 is evaluated instead.
	
.