Decision Making:

 

Selection structure

A selection structure selects a statement or set of statements to execute on the basis of a condition. A statement or set of statements that is executed when a particular condition is true and ignored when the condition is false is called conditional statement.

Suppose a program displays pass if the student gets 40 or more than 40 marks. It displays fail when the marks are below 40. The program checks the marks before displaying the message. This process is known as decision making or selection.

Types of Selection structure:

There are two basic types of selection structures in C++ language. These are as follows:

If else structure: it is used to execute or skip a statement or set of statements by checking a condition.

Switch statement: it is used when there are many choices available and only one should be executed.

 

If statement

If is a keyword in C++ language. If statement is a decision making statement. It is the simplest form of selection construct. It is used to execute or skip a statement by checking a condition.

The condition is given as a relational expression. If the condition is true, the statement after if statement is executed. If the condition is false, the statements after if is not executed.

SYNTAX

The syntax is as follows:

If (condition)

Statement;

The above syntax is used for single statement. A set is statements can also be made conditional. In this case, these statements are written in curly braces. The set of statements are also called compound statement.

EXAMPLE

#include <iostream>

Using namespace std;if-condition

Void main ()

{

Int marks;

Cout<<”Enter your marks”;

Cin>>marks;

If(marks>=40)

     Cout<<”you have passed”;

}

NOTE: cin is an input function, used to get input from the user.

 

If else statement

It is another type of if statement. It executes one block of statement when the condition is true and the other when it is false. In any situation, one block is executed and the other is skipped. In if else statement:

  • Both blocks of statement can never be executed.
  • Both blocks of statements can never be skipped.

SYNTAX

if (condition)

   statement;

else

  statement;

two or more statements are written in curly brackets { }.

EXAMPLE

# include <iostream>

Void main ()if-else-statement

{

Int n;

Cout<<”enter a number:”;

Cin>>n;

If(n%2==0)

  Cout<<”n is even”;

Else

   Cout<<”n is odd”;

}

 

If else if statement

“If else if” statement can be used to choose one block of statements from many blocks of statements. It is used when there are many options and only one block of statements should be executed on the basis of a condition.

SYNTAX

The syntax of this structure is:

if (condition)if-else-if-statement

{

Block 1;

}

else if (condition)}

{

Block 2;

}

else if (condition)

{

Block 3;

}

:

:

else

{

Block n;

}

Working of If else if

The test conditions in if else statement with multiple alternatives are executed in a sequence until a true condition is reached. If a condition is true, the block of statements following the condition is executed. The remaining blocks are skipped. If a condition is false, the block of statements following the condition is skipped. The statement after the last else are executed if all conditions are false.

 

Switch statement

The switch statement is another conditional structure. It is good alternative of nested if else. It can be used easily when there are many choices available and only one should be executed. Nested if becomes very difficult in such situation.

SYNTAX
the syntax for writing this structure is as follows:

Switch (expression)

{
case val1:switch-statement

statements1;

break;

case val2:

statements2;

break;

:

:

case valn:

statement;

break;

default:

statements;

}

Working switch statement

Switch statement compares the result of a single expression with multiple cases. Expression can be any valid that results in integer or character value. The expression is evaluated at the top of switch statement and its result is compared with different cases. Each case label represents one choice. If the result matches with any case, the corresponding block of statements is executed. Any number of cases can be used in one switch statement.

The default label appears the end of all case labels. It is executed only once when the result of expression does not match with any case label. Its use is optional. The position of default label is not fixed. It may be placed before the first case statement or after the last one.

The break statement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.

EXAMPLE

# include <iostream>

Void main ()

{

Int n;

Cout<<”enter number of a weekday:”;

Cin>>n;

Switch (n)

{

case 1:

cout<<”Friday”;

break;

case 2:

cout<<”Saturday:”;

break;

case 3:

cout<<”Sunday”;

break;

default:working-switch-statement

cout<<”out of range”;

}

}

 

 Conditional operator

Conditional operator is a decision making structure. It can be used in place of simple if else structure. It is also called ternary operator as it uses three operands.

SYNTAX

The syntax of conditional operator is as follows:

(condition) ? true case statement: false case statement;

Condition: is specified as relational or logical expression. The condition is evaluated as true or false.

True case: it is executed if expression evaluates to true.

False case: it is executed if expression evaluates to false.

EXAMPLE

Suppose we have a variable A. the following statement:

X = (A>50) ? 1 : 0;

Will assign 1 to X if the condition A>50 is true. It will assign 0 to X if the condition is false. The above statement can be written using if else statement as follows:

If (A>50)

X=1;

Else

X=0;