Post on 18-Jan-2022
2
Control Structures
Sequential execution Statements executed one after the other in the order written
Transfer of control When the next statement executed is not the next one in
sequence
All programs written in terms of 3 control structures: Sequence structure
Built into C++. Programs executed sequentially by default.
Selection structures C++ has three types - if, if/else, and switch
Repetition structures C++ has three types - while, do/while, and for
3
Control structure example
Sequence structureint main()
{
int i=2,j=10;
int z= (++j)*--i
cout<<z;
return 0;
}
Selection structureint main()
{
int grade=0;
cout<<“enter grade”;
cin>>grade;
if(grade>90)
{
cout<<“high grade”;
}
else
{
cout<<“low grade”;
}
}
Repetition structureint main()
{
int grade=0;
cout<<“enter grade”;
cin>>grade;
while (grade<50)
{
grade +=10;
}
cout<<grade;
}
4
Combination of Control Structures
Two types of control structures combination:
Control structure stacking: use them one after the other.
Control structure nesting: use one control structure inside the body of another control structure.
5
Stacking vs. Nesting
Stacking control structure int main()
{
int grade=0;
cout<<“enter grade”;
cin>>grade;
if(grade>90)
{
cout<<“high grade”;
}
else
{
cout<<“low grade”;
}
}
Nesting control structure int main()
{
int grade=0;
cout<<“enter grade”;
cin>>grade;
if (grade<90)
{
if (grade<60)
{
if(grade<50)
{
cout<<“fail”;
}
else
{
cout<<“pass”;
}
}
}
}
6
The if Selection Structure I
Selection structure used to choose among alternative courses of action
Pseudocode example: If student’s grade is greater than or equal to 60
Display “Passed”
If the condition is true
print statement executed and program goes on to next statement
If the condition is false
print statement is ignored and the program goes onto the next statement
Indenting makes programs easier to read C++ ignores white-space characters
7
The if Selection Structure II
Translation of pseudocode statement into C++:if ( grade >= 60 )
cout << "Passed";
Diamond symbol (decision symbol)
indicates decision is to be made
Contains an expression that can be true or false.
Test the condition, follow appropriate path
if structure is a single-entry/single-exit
structure
8
The if Selection Structure III
Flowchart (or UML activity diagram) of pseudocode statement
true
false
grade >= 60 print “Passed”
A decision can be made on
any expression.
zero - false
nonzero - true
Example:
3 - 4 is true
9
The if/else Selection Structure I
if
Only performs an action if the condition is true (single selection structure)
if/else
A different action is performed when condition is true and when condition is false (double selection structure).
Psuedocodeif student’s grade is greater than or equal to 60
Display “Passed”else
Display “Failed”
C++ codeif ( grade >= 60 )
cout << "Passed";else
cout << "Failed";
10
The if/else Selection Structure II
Ternary conditional operator (?:) (the only C++ ternary operator) Takes three arguments (condition, value if true, value if false)
Our C++ code could be written as:cout << ( grade >= 60 ? “Passed” : “Failed” );//omitting ( ) is syntax error
OR
grade >= 60 ? cout << “Passed” : cout << “Failed”;
Remember that the precedence of ?: is low, so do not forget the parenthesis that is used to force its priority.
truefalse
display “Failed” display “Passed”
grade >= 60
11
Nested if/else Structure I
Nested if/else structures Test for multiple cases by placing if/else selection structures
inside if/else selection structures.if student’s grade is greater than or equal to 90
Display “A”else
if student’s grade is greater than or equal to 80Display“B”
else if student’s grade is greater than or equal to 70
Display “C”else
if student’s grade is greater than or equal to 60 Display “D”
elseDisplay“F”
Once a condition is met, the rest of the statements are skipped
The above code can be rewritten using else if as in the next slide
12
Nested if/else Structure II
if student’s grade is greater than or equal to 90Display “A”
else if student’s grade is greater than or equal to 80Display “B”
else if student’s grade is greater than or equal to 70 Display“C”
else if student’s grade is greater than or equal to 60 Display “D”
elseDisplay“F”
Pay attention to the indentation level and how it is differed from the previous slide.
13
The if/else Body I
The body of if or else is the statement that will be implemented after if or else.
If no braces after if or else then the body will be only the first statement after them.
If there are braces, then the body will be the compound statement.
14
The if/else Body II
Compound statement: Set of statements within a pair of braces Example:
if ( grade >= 60 )cout << "Passed.\n";
else {cout << "Failed.\n";cout << "You must take this course again.\n";
}
Without the braces,cout << "You must take this course again.\n";
would be automatically executed and the body of the else will be the first statement after it only.
Block of code Compound statements with declarations
15
The if/else Body III
Every if/else or if is considered as one statement with their bodies. How??
Placing lines of codes between else and the body of its if is syntax error.
Leaving the parenthesis after the if empty (you have not put an expression for the condition) is syntax error.
if () // no condition syntax error
cout<<"b";
If( grade<50)
{ cout<<“F”;
cout<<“Fail”;
}
cout<<“you fail”; // syntax error
else
Cout<<“pass”;
if (grade > 60)cout<<"b";
cout<<"hello"; // will produce a syntax error else
cout<<"c";