C++ DECISION MAKING STATEMENT


C++ Decision Making Statement

C++ Decision making statements are used to decide the flow of execution of the program depending on some condition. If the condition is true, a block of statement is executed, otherwise another block of statement is executed. There are mainly 4 types of decision making statements. These are:

C++ if statement

C++ if statement is used to execute a particular block of statement if the condition is true. It is mainly of 4 types as follows:

1. if – then statement: if statement is used to execute a block of statement if the condition is true. It has the syntax as follows:

It has the flow diagram as follows:

C++ if statement
For example:

2. if-else statement: if-else statement is used to execute a block of statement if the condition is true. If the condition is false, then another block of statement is executed. It has the syntax as follows:

It has the flow diagram as follows:

It has the flow diagram as follows:

For example:

3. nested if statement: if statement can be nested to any numbers which means we can use as many if statement inside another if statement as we want. It has the syntax as follows:

For example:

It has the flow diagram as follows:

cpp nested if statement flowchart
4. else – if statement: In the else – if statement, if block is executed if the condition is true, otherwise next condition is tested in the else block. If it is true, then else block is executed. It has the syntax as follows:

For example:

cpp else if statement flowchart
C++ Switch Statement

In the switch statement a variable used to evaluate the expression. The variable is tested against a list of values. Each value is known as case. If the value matched with the tested condition, then that case is executed. We cannot use logical operator in the switch statement. It has the syntax as follows:

For example:

Flowchat of C++ Switch Statement:
cpp switch case flowchart

Conditional Operator Statement

Conditional operator statement can be used in the same way as if else statement. It has 2 operators ‘?’ and ‘:’ associated with it. It has the syntax as follows:

Here, Exp1, Exp2 and Exp3 are expressions. Exp1 is evaluated. If Exp1 is true, then Exp2 is the result of Exp1 and Exp2 is evaluated. If Exp1 is false, then Exp3 is the result of Exp1 and Exp2 is evaluated. For example:

goto statement: goto statement is an unconditional decision making statement. It is used to transfer the control to the target address. This statement is hard to understand and modify. It has the syntax as follows:

label is used to specify the target address. Label statement is identified by the label and preceded by a colon (:). For example:

 

Close Menu