selection statements

24
Selection Flow Of Control Made by: Abhishek Kasana Abhishek Sharma Saurabh Aggarwal Harsh Dabas

Upload: harsh-dabas

Post on 19-May-2015

440 views

Category:

Software


0 download

DESCRIPTION

its a

TRANSCRIPT

Page 1: Selection statements

Selection Flow Of ControlMade by:

Abhishek KasanaAbhishek SharmaSaurabh AggarwalHarsh Dabas

Page 2: Selection statements

Flow Of ControlFlow of control is basically of three types:

1) Sequential flow of control 2) Selection flow of control 3) Iteration flow of control

Page 3: Selection statements

Selection Flow Of ControlSelection flow of

control is also known as selective execution. Here we may select one of the two blocks to execute based on a certain condition. Execution of one block excludes the other .

Page 4: Selection statements

Types Of Selection Flow Of ControlC++ provides two

Selection Statements:

Single Branching Statement [ If ()….else]

Multiple Branching Statement [ Switch()..case]

if-else

Selection

switch

Page 5: Selection statements

Single Branching Statement[if()…else]

Single Branching statement is very versatile form of selection statement. It offers following types of selection.IfIf()…elseNested ifElse if

Page 6: Selection statements

The if Statement Of C++An if statement test a particular condition; if the condition

evaluates to true, a course-of-action is followed i.e., statement(s) following are executed. Otherwise the course-of –action is ignored.

Syntax: if (expression ) { statement(s); }

Where if is the keyword, expressionis a booleon expression within a set of parenthesis and statement can be a simple or compound statement.

Page 7: Selection statements

Some test expressions:(a) if(grade==‘A’)

cout<<“You Did Well”;(b) if(a>b)

cout<<“A has more than B has.”;(c) if(x)

{ cout<<“x is non-zero\n”; cout<<“Hence it results into “;}

(d) if((x>=2)&&(x<=10)){cout<<“A compound test condition resulted true\n”;}

Page 8: Selection statements

Remember:A false value is 0 in C++ and a non–zero

value is considered true in C++.Please note that if(x) type of condition might

not work in newer compilers. Though it works in Turbo-C++, for newer compilers like codeblocks, we change the condition to if(x!=0) and change if(!x) into if(x==0).

Page 9: Selection statements

Selection if( )…else … Also possible to make two way selection If the expression is

true, statement1 isexecuted

Otherwise statement2is executed

Syntax; if (expression) { statement(s)1 } else { statement(s)2 }

Page 10: Selection statements

Always Remember:

In an if-else statement, only the code associated with if (i.e., statement 1) or the code associated with else (i.e., statement 2) executes, never both.

Page 11: Selection statements

One or more if statements embedded within the if statement are called nested ifs.

The following if-else statement is a nested if declaration nested to level two:

Nested if

Page 12: Selection statements

Nested if can have following the forms:1)if nested inside

if -part

if(expresssion1) { : if(expression2) statement 1; else statement 2; : } else body-of-the-

else;

2)If nested inside else part

if (expression1)body-of-if;Else{: if (expression 2) statement-1; else statement-2; :}

3)If nested inside both if part and else part

If (exprssion1){ : if (expression2) statement 1; else statement 2; :}Else{ : if(expression 3) statement 3; else statement 4; :}

Page 13: Selection statements

The if else if ladder

A common programming construct in C++ is the if-else-if ladder ,which is often also called the if-else-if staircase because of its appearance . It takes the following general form:

if (expression1) statement 1; else if (expression 2) statement 2;

else if (expression3)statement 3: : else statement n:

This can also be written as :If (expression 1) statement 2;Else if (expression 2) statement 2;Else if (expression 3) statement 3:

:Else statement n:

Page 14: Selection statements

Graphical representation of if-else-if ladder:

Page 15: Selection statements

The Dangling Else ProblemThe nested if – else statement introduces a

source of potential ambiguity referred to as dangling else problem. This problem arises when in a nested if statement, number of ifs is more than the number of else clause. This question then arise , with which if does the additional else clause properly match up.

Page 16: Selection statements

16

The Dangling elseHow to determine which if the else goes

withExample:

if (abs (x - 7)) if (x < 7) cout << “x approaches 7 from left”; else cout << “x approaches 7 from the right”; else cout << “x not close to 7”;

if (abs (x - 7)) if (x < 7) cout << “x approaches 7 from left”; else cout << “x approaches 7 from the right”; else cout << “x not close to 7”;

Rule : An else goes with the closest unmatched if

?

?

Page 17: Selection statements

17

The Dangling ElseRule : an else goes with the closest unmatched if

Consider … how do you force an else to go with a previous if?

if (x < y)

if (y > 3) cout << “message about y > 3”; else cout << “message about x and y”;

if (x < y)

{ if (y > 3) cout << “message about y > 3”; } else cout << “message about x and y”;

if (x < y)

{ if (y > 3) cout << “message about y > 3”; } else cout << “message about x and y”;

Use { curly brackets } to nest the statements

Page 18: Selection statements

This statement is used when we have to select one option out of many alternatives.

It is a multi branch statement that makes the control to jump to one of the several

statements based on the value of an integer variable or an expression. The general

Form of the switch is:

switch(expression){ case constant 1: statement sequence 1; break; case constant 2: statement sequence 2; break; case constant 3: statement sequence 3; break; . . case constant n-1: statement sequence n-1; break; default: statement sequence;}

Multiple Branching Statement Switch statment

Page 19: Selection statements
Page 20: Selection statements

Nested Switch Like if statement , a switch statement can also be nested .There

can be a switch as part of the statement sequence of another switch.

Example:Switch(a){Case 1 : switch(b)

{ case 0: cout<<“divide by Zero ---Error!!”;

break; case 1: res=a/b;

} break;

Case 2 : :

:}

Page 21: Selection statements

Some important things to know about SwitchA switch statement can work only for equality comparisons.No two case labels in the same switch can have identical

values. But, in case of nested switch statements the case constant of the inner and outer switch can contain common values.

If character constants are used in the switch statement , they are automatically converted to their integers(i.e.. their equivalent ASCII codes) .

Switch Statement is more efficient than if in a situation that supports the nature of switch operation.

If a case statement does not include a break statement then fallthrough occurs.

Default statement gets executed when no match is found. The default statement is optional and , if it is missing , no action takes place if all matches fail.

Page 22: Selection statements

Switch vs. If ElseS.no Switch If Else

1 The Expression is tested for equality only .

The expression cam be tested for inequality as well. (<,>)

2 Only one value is used to match against all case labels.

Multiple expression can be tested for branching.

3 Switch case is not effective when checking for a range value.

If else is a better option to check ranges.

4 Switch case cannot handle floating point values

If else can handle floating point values

5 The case label must be constant(Characters of integers).

If else can use variables also for conditions.

6 Switch statement provides a better way to check a value against a number of constants .

If else is not suitable for this porpose .

Page 23: Selection statements

ConclusionC++ provides two kinds of selection statements: if and

switch.The if-else statement tests an expression and depending

upon its truth value, one of the two sets-of-action is executed.

The if-else can be nested also i.e., an if statement can have another if statement .In a nested if-else statement, an else goes to immediately preceding unmatched if .

A switch is another selection statement in C++ that tests a value against a set of integer or character constants.

A switch statement can be nested also .An if-else is more flexible and versatile compared to

switch but switch is more efficient in a situation when the same variable is compared against a set of values for equality.

Page 24: Selection statements

THANK YOU