BUE Programming Lec 3 Switch and Loops
BUE Programming Lec 3 Switch and Loops
PROGRAMMING AND
LECTURE 3 SOFTWARE DESIGN
CONTROL
STRUCTURES
1
AGENDA
Control Structures
Selection
If…else
Switch
Iteration
While
Do…while
For Loop
Types of Errors
Syntax
Logical
Runtime
2
CONTROL STRUCTURES
• if … else
• switch case
3
IF AND IF-ELSE STATEMENTS
Syntax:
if(condition) Example:
{ if(x==y)
// if body {
4
IF AND IF-ELSE STATEMENTS
Example:
Syntax:
if(condition)
if(x==y)
{ {
// if body cout<<“Both numbers are equal”<<endl;
// Statements to execute if condition is true
}
}
else
else
{
{
cout<<“Both Numbers are not equal”<<endl;
// if body
// Statements to execute if condition is false
}
Do I really need the braces in this example?
}
5
NESTED IF AND NESTED IF-ELSE STATEMENTS
Syntax
if(condition1)
if(condition2) if(x==y)
{
if(y==z)
// if body
{
// Statements to execute if condition1 and
condition2 are true cout<<“The three numbers are equal”<<endl;
}
}
Another equivalent Code:
Another Equivalent Solution:
if(condition1 && condition2)
{ if(x==y && y==z)
// if body {
// Statements to execute if condition1 and cout<<“The three numbers are equal”<<endl;
condition2 are true 6
} }
Syntax if(x==y)
if(condition1) {
{// outer if body if(y==z)
if(condition2) {
{ cout<<“The three numbers are
// inner if body equal”<<endl;
// Statements to execute if condition1 and }
condition2 are true
else
}
{
else
cout<<“The x=y and y not equal z”<<endl;
{
}
// Statements to execute if condition1 is true and
condition2 is false }
} else
} {
else cout<<“x is not equal y”<<endl;
{ }
// Statements to execute if condition1 is false and
7
}
DANGLING ELSE Having two if statements with only one written else and no braces
8
Branching: if Statement
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
Not if (x = 100)
if (x == 100) equivalent
cout << "x is 100"; cout << "x is 100";
else else
cout << "x is not 100"; cout << "x is not 100";
11
NOTE Logic Error Empty Body
if (i > j) if (i > j)
if (i > k) This is better if (i > k)
cout << "A"; with correct cout << "A";
else indentation else
cout << "B"; cout << "B";
(a) (b) 12
To force the else clause to match the first if clause, you must add a pair of
braces:
int i = 1, j = 2, k = 3;
if (i > j)
{
if (i > k) Common Errors:
cout << "A"; • Adding semi-colon at the
end of the condition
} • Forgetting the braces for
else blocks of code.
• Dangling else.
cout << "B";
Output? 13
(a) (b)
This is better
14
Branching: Switch Statement
Remark: Used Only for equal comparison
for integers or characters
15
A. Branching: Switch Statement (cont.)
16
BRANCHING: SWITCH STATEMENT
19
void main()
{
GRADES char grade;
INTERPRETATION cout << "Enter student grade\n";
cin >> grade;
if (grade =='A' || grade == 'a')
cout << "Excellent\n";
else if (grade =='B' || grade == 'b')
cout << "Very Good\n";
Develop aProgram that else if (grade =='C' || grade == 'c')
reads student grade and cout << "Good\n";
interprets it. else if (grade =='D' || grade == 'd')
cout << "You can do better\n";
else if (grade =='E' || grade == 'e')
How many
branches
cout << "Disappointing\n";
execute? else
cout << "Invalid Grade\n";
}
20
GRADE INTERPRETATION
24
Design Structures
Sequence
Selection/Decision
Repetition
25
CONTROL STRUCTURES
• while loop
• do….while loop
• for loop
26
WHILE LOOP
while (condition) while (condition)
{ statement;
statement1;
statement2;
}
27
Iteration: While Loop
}
28
Iteration: While Loop (cont.)
while(bool_expression)
{ statement1; start count
if bool_exp is true
{ execute body
statement2; index update
re-evaluate bool_exp
} }
else
1 exit the loop
2
29
Iteration: While Loop (cont.)
Example
30
Iteration: While Loop (cont.)
Remark
Index update (INSIDE LOOP BODY) MUST change the result
of the bool_exp so we can exit the loop at some point of
execution, otherwise you will go into an infinite loop…
something very bad!
31
Iteration: While Loop (cont.)
Remark
Index update (INSIDE LOOP BODY) MUST change the result
of the bool_exp so we can exit the loop at some point of
execution, otherwise you will go into an infinite loop…
something very bad!
32
Iteration: While Loop (cont.)
• The body of the while statement
cannot be simple. It must be compound.
WHY?
while(bool_expression)
Because there
{ statement1; must always be
statement2; Block of code an extra
statement3; statement to
} change the
bool_expressio
n
33
1
2
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution Output:
1
9 void main() 2
10 { 3
4
11 int number = 1; // initialization 5
6
12 7
13 while (number <= 10 ) { // repetition condition 8
9
14 cout << number << endl; // display number 10
15 ++number; // increment
16
17 } // end while
18
34
19
21 } // end function main
WHILE LOOP: COUNT DOWN
Program counts down from a user input value to 1 then prints FIRE!
int num;
cout << "Enter start number:\n";
cin >> num;
while (num > 0) // while (num >= 1)
{
cout << num << '\t';
num--;
}
cout << "FIRE!!\n";
35
CLASS AVERAGE (5 STD.)
float total;
int gradeCounter;
int grade;
float average;
total = 0;
gradeCounter = 1;
Suppose that the tuition for a university is $10,000 this year and
that the tuition increases 7% every year. In how many years will the
tuition be doubled or more?
39
SOLUTION
int year = 1;
float tuition = 10000; // Year 1
while (tuition < 20000)
{
year++;
tuition *= 1.07; //tuition = tuition + tuition * 0.07
cout << "Tuition will be doubled in " << year << " years" << endl;
cout << "Tuition will be $" << tuition << " in "
<< year << " years" << endl;
40
DO WHILE LOOP
do do
statement; {
while (condition); statement1;
statement2;
} while (condition);
statement2;
} while(bool_expression) ;
body: part
of code
that is
repeated
43
Iteration: Do..While Loop (cont.)
What if i is
initialized
Example1: by 3?
44
1 // Fig. 2.24: fig02_24.cpp
2 // Using the do/while repetition structure.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // function main begins program execution
9 void main()
10 {
11 int counter = 1; // initialize counter
12
13 do {
14 cout << counter << " "; // display counter
15 } while ( ++counter <= 10 ); // end do/while
16
Notice the preincrement
17 cout << endl; in loop-continuation test.
18
19
20
21 } // end function main 45
1 2 3 4 5 6 7 8 9 10
int num;
cout << "Enter start number:\n";
DO
cin >> num;
WHILE:
do
COUNT
{ DOWN
cout << num << '\t';
num--;
} while (num > 0);
cout << "FIRE!!\n"; 46
47
48