Escolar Documentos
Profissional Documentos
Cultura Documentos
Fall 2019
Lecture 6
Dr. Farhan Aadil
farhan.aadil@ciit-attock.edu.pk
• Key Points
– Make sure there is a statement that will eventually
terminate the iteration criterion
• The loop must stop!
– Make sure that initialization of loop counters or
iterators is properly performed
– Have a clear purpose for the loop
• Document the purpose of the loop
• Document how the body of the loop advances the
purpose of the loop
The Do-While Statement
• Syntax
do Action
while (Expression)
• Semantics
Action
– Execute Action
– If Expression is true then
execute Action again
– Repeat this process until
Expression evaluates to true
false
• Action is either a single
statement or a group of Expression
statements within braces
false
Waiting for a Proper Reply
char Reply;
do {
cout << "Decision (y, n): ";
if (cin >> Reply)
Reply = tolower(Reply);
else
Reply = 'n';
} while ((Reply != 'y') && (Reply
!= 'n'));
Compound statement
int sum = 0;
for (int i = 1; i <= n; ++i) {
sum += i;
}
Combining break and for
char ch;
int count = 0;
for ( ; ; ) { //while(1)
std::cin >> ch;
if (ch == ‘\n’) break;
++count;
}
Switch
switch (letter) {
case ‘N’: cout < “New York\n”;
break;
case ‘L’: cout < “London\n”;
break;
case ‘A’: cout < “Amsterdam\n”;
break;
default: cout < “Somewhere else\n”;
break;
}
C++ control structures
• Selection
if
if . . . else
switch
• Repetition
while loop
for loop
do . . . while loop
Multiple Selection:
The switch Statement
multiway
expression
case 1
action
case 2
action
case 3
action
default
action
switch Statement: Example 1
switch(int(score)/10){
case 10:
case 9: cout << "Grade = A" << endl;
case 8: cout << "Grade = B" << endl;
case 7: cout << "Grade = C" << endl;
case 6: cout << "Grade = D" << endl;
default:cout << "Grade = F" << endl;
}
switch Statement: Example 2
switch(int(score)/10){
case 10:
case 9: cout << "Grade = A" << endl;
break;
case 8: cout << "Grade = B" << endl;
break;
case 7: cout << "Grade = C" << endl;
break;
case 6: cout << "Grade = D" << endl;
break;
default:cout << "Grade = F" << endl;
}
switch Statement: Example 2
is equivalent to:
if (score >= 90)
cout << "Grade = A" << endl;
else if (score >= 80)
cout << "Grade = B" << endl;
else if (score >= 70)
cout << "Grade = C" << endl;
else if (score >= 60)
cout << "Grade = D" << endl;
else // score < 59
cout << "Grade = F" << endl;
switch Statement: Example 2
#include <iostream>
using namespace std;
int main()
{ char answer;
cout << "Is csc103 an easy course? (y/n): ";
cin >> answer;
switch (answer){
case 'Y':
case 'y': cout << "I think so too!" << endl;
break;
case 'N':
case 'n': cout << "Are you kidding?" << endl;
break;
default:
cout << "Is that a yes or no?" << endl;
}
return 0;
}
Points to Remember
• The expression followed by each case label must
be a constant expression.
• No two case labels may have the same value.
• Two case labels may be associated with the same
statements.
• The default label is not required.
• There can be only one default label, and it is
usually last.
Switch
switch (letter) {
case ‘N’: cout < “New York\n”;
break;
case ‘L’: cout < “London\n”;
break;
case ‘A’: cout < “Amsterdam\n”;
break;
default: cout < “Somewhere else\n”;
break;
}
Important links
• My email: farhan.aadil@ciit-attock.edu.pk
• My web page:
http://ww3.comsats.edu.pk/faculty/FacultyDetails.aspx?
Uid=18813