Você está na página 1de 21

Programming Fundamentals

Fall 2019
Lecture 6
Dr. Farhan Aadil
farhan.aadil@ciit-attock.edu.pk

Please turn OFF your Mobile Phones!


Iteration Do’s

• 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

• We use braces {} to build compound - statements


• To use braces or not to use braces??
Iteration statements

// compute sum = 1 + 2 + ... + n


// using for loop

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

value1 value2 value3 value4


action 1 action 2 action 3 action 4
Flow Chart
Multiple Selection:
The switch Statement
Syntax:
switch (<selector expression>) {
case <label1> : <sequence of statements>;
break;
case <label2> : <sequence of statements>;
break;
case <labeln> : <sequence of statements>;
break;
default : <sequence of statements>;
}
Multiple Selection:
The switch Statement
Meaning:
• Evaluate selector expression.
• The selector expression can only be: a bool, an integer, an
enum constant, or a char.
• Match case label.
• Execute sequence of statements of matching label.
• If break encountered,
go to end of the switch statement.
• Otherwise continue execution.
Multiple Selection:
The switch Statement

case 1
action

case 2
action

case 3
action
default

action
switch Statement: Example 1

• If you have a 95, what grade will you get?

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

Thanks for listening!


Ready for Questions & Answers

COMSATS University Islamabad, Attock Campus

Você também pode gostar