Control Structures Selection
Control Structures Selection
§ Control Structures
§ Relational Operators
§ Logical (Boolean) Operators
§ Logical Expressions
§ Selection and
Structures
§ The Function
3
§ Statements can be
executed in sequence
§ One right after the other
§ No deviation from the
specified sequence
4
§ A selection
structure can be
used
§ Which statement
is executed is
selected by
whether the
expression is true
or false
5
§ Statements can be
repeated
§ The number of
repetitions depends
on when the
expression turns false
6
Beware of
mistaking the
assignment = for
the equality ==
7
Examples:
Expression Meaning Value
8 < 15 8 is less than 15 true
6 != 6 6 is not equal to 6 false
2.5 > 5.8 2.5 is greater than 5.8 false
5.9 <= 7.5 5.9 is less than or
equal to 7.5 true
8
Given
string str1 = "Hello"; string str2 = "Hi";
string str3 = "Air"; string str4 = "Bill";
string str5 = "Big";
Binary operators
Highest
View
Sample
Program
Lowest
12
§ Consider
§ Syntax
Note parentheses
§ Example around the condition
§ Syntax
View sample
program
§ Example
17
statements in
curly brackets
18
§ Example
The compound
statement
IF
20
§ Contrast
• A sequence of
statements
• A sequence of separate statements
§ What happens in each case when it is
the first if condition that is true?
sequence will jump out of
the structure whenever match is found
• sequence of separate 's – each is
checked, no mater where the match is
25
switch (choice) {
case 1 : do_option_one(); break;
case 2 :
case 3 : do_2_3_a ();
do_2_3_b (); break;
default : do_something_else (); }
27
§ The causes
control to be shifted
switch (choice) {
case 1 : do_option_one(); to first statement
break; after the switch
case 2 :
case 3 : do_2_3_a ();
statement
do_2_3_b (); § the
break;
default : do_something_else (); statement is
} executed if the value
// next statement of the switch
expression is NOT
found among switch
labels
30
§ Example: