LECTURE # 6 :STRUCTURED PROGRAMMING
Selection StatementsD. Abdalrahman R. Qubaa First Class, System and Control Engineering Dep.
Content
* Control structures Types of selection statements if single-selection statement if..else double-selection statement Nested if..else statements Dangling else problem switch multiple-selection statement Common errorsControl Structure (Logic Structure)
Used to design data flow in modules and program as a whole Basic structures Sequential structure Processes one instruction after another Selection structures Decision structure Make choices by using relational or logical operators( Ex. If or if..else) Case structure Enable to pick up one of a set of tasks (Ex. switch) Loop structure Enable to repeat tasks (Ex. for)*
Selection Statements
Three types Single-selection statement ( Ex. if) Select or ignore a single group of actions Double-selection statement (Ex. if..else) Select between two groups of actions Multiple-selection statement (Ex. switch) Select among many group of actions*
if single-selection Statement
Begin with if followed by condition; then action or group of actions are listed Syntax Single action Multiple actionsIf condition is true, action is performed Otherwise, action is ignored
* if (condition) { action1; action2; .. actionN; }
if(grade >= 60) Cout << “Passed”; if (condition) action;
if..else double-selection Statement
Begin with if followed by condition; then action or group of actions are listed End with else then action or group of actions are listedIf condition is true, action that followed by if is performed Otherwise, action that followed by else is performed
* if (condition) action1; else action2;
if(grade >= 60) cout << “Passed”;else cout << “failed ”;
Question ?
Which of the operator that provide the similar result of if..else double-selection statement? (from previous lecture)?:
*
Nested if..else Statements
Nested If : means to write an if statement within another if statement. One inside another, test for multiple cases Once condition met, other statements skipped* if (condition1) action1; else if (condition2) action2; else if (condition3) action3; ... else actionN;
if (condition1) { if (condition2) action1; else { if (condition3) action2; else action3; } } else action4;
Example
For example, the following code will print Excellent for exam grades greater than or equal to 90, very good for grades greater than or equal to 80, good for grades greater than or equal to 70, median for grades greater than or equal to 60, and pass for all other grades.* if ( grade >= 90 ) cout << “Excellent";else if ( grade >= 80 ) cout << “very good"; else if ( grade >= 70 ) cout << “good"; else if ( grade >= 60 ) cout << “median"; else cout << “pass";
Dangling-else Problem
Each else associated with immediately preceding if There is exception when placing braces { }* int x = 10, y = 2;if ( x > 5) if( y > 5) cout << "x and y are > 5"<< endl; else cout<<"x is <=5“; Have logic error
(int x = 10, y = 2;)if ( x > 5) { if( y > 5) cout << "x and y are > 5"<< endl; }elsecout<<"x is <=5";cout<<"x is >=5“; Correctness
Example: Determine the output for each of the following when x = 9 and y = 11, and when x and y = 11.
a) if ( x < 10 ) if ( y > 10 ) cout << "*****" << endl; else cout << "#####" << endl; cout << "$$$$$" << endl; ANS:
b) if ( x < 10 ) { if ( y > 10 ) cout << "*****" << endl; } else { cout << "#####" << endl; cout << "$$$$$" << endl; } ANS:
*
Combining more than one condition
* OperatorMeans
Description
&&
And
The Expression Value Is true If and Only IF both Conditions are true
||
OR
The Expression Value Is true If one Condition Is True
To combine more than one condition we use the logical operators.
Example : check whether num1 is between 0 and 100 IF ( (num1 >= 0) && (num1 <=100) ) Cout <<“The Number Is between 1 and 100” ; Else Cout <<“ The Number Is Larger Than 100”;
Using bool Variables
( bool flag1 = true, flag2 = false;)if ( flag1 )… … else… … if ( flag1 || flag2 )… … … else … … … *
Implicit Typecasting
int x1 = -15, x2 = 0;if ( x1 )… … else… … if ( x1 || x2 )… … … else … … … *Confusing ==
Confusing the equality operator == with the assignment operator = results in logic errorsif ( x==2 )cout<<“x is equal to 2”;else cout<<“x is not equal to 2”;if ( x=3 )cout<<“x is equal to 2”;else cout<<“x is not equal to 2”; *This message will always be printed !!!
switch Multiple-selection Statement
The switch statement: that allows us to make a decision from the number of choices Perform actions based on possible values of variable or expression Begin with switch followed by controlling expression Value of expression compared to case labels then execute action for that case No matching, the execution go to the optional default statement break causes immediate exit from switch statement* if (condition1) switch (expression) { case value1: action1; break; case value2: action2; break; ... case valueN: actionN; break; default: action; }
switch Multiple-selection Statement
* Switch (Expression ) { case constant 1 : Action statements; break ; case constant 2 : Action statements; break ; case constant 3 : Action statements; break ; default : Action statements; }Expression It could be an integer constant like 1, 2 or 3, or an expression that evaluates to an integer . Constant : is a specific value.
Switch (10); Int i ; Switch (i); switch ( i + j * k ) switch ( 23 + 45 % 4 * k )
Switch (10.0); float i ; Switch (i); switch ( i + j * k ) switch ( 23 + 45 % 4 * k )
Case 50: Case 40 +10;
Case >= 50; Case a+ b;
* switch Multiple-selection Statement
switch (grade) { case 90 : cout <<"You Got A \n"; break; case 80 : cout <<"You Got B \n"; break; case 70 : cout <<"You Got C \n"; break; case 60 : cout <<"You Got D \n"; break; default : cout <<" Sorry , You Got F \n"; }Example
* switch (number){ case 0: cout << "Too small, sorry!";case 1: cout << "Good job! " << endl; // fall throughcase 2: cout << “very good!" << endl; // fall through case 3: cout << "Excellent!" << endl; // fall through case 4: cout << "Masterful!" << endl; // fall through case 5: cout << "Incredible!" << endl; break; default:cout << "Too large!" << endl; break; }Switch Versus IF – Else IF There are some things that you simply cannot do with a switch. These are: A float expression cannot be tested using a switch Cases can never have variable expressions (for example it is wrong to say case a +3 : ) Multiple cases cannot use same expressions. switch works faster than an equivalent IF - Else ladder.