Control Flow II
Control Flow II
Gary Chan
Albert Chung
Cindy Li
Dimitris Papadopoulos
Pedro Sander
Charles Zhang
Nested Looooooops
int main( )
{
int NUM ASSIGNMENTS = 5; // Uppercase variable doesn’t change
int j; // Assignment counter
int score, sum of scores;
char reply = ’y’; // ’y’ for yes, ’n’ for no; initialized to yes
cout "Enter scores for the first student? (y/n) " endl;
while ((cin reply) && (reply == ’y’ || reply == ’Y’))
{
sum of scores = 0; // Reset the accumulator to zero
j = 1; // Reset the assignment counter to 1
return 0;
}
int main()
{
// To print out products of j*k where j, k = 1,...,10
for (int j = 1; j <= 10; ++j)
{
for (int k = 1; k <= 10; ++k) // Reset k=1 for each j. Why?
cout << setw(4) << j*k; // Set the length of output field to 4
return 0;
}
statements 2 statements 2
false false
true
boolean true boolean
expression 2 statements 3 expression 2
continue
false false
break
int main()
{
int NUM_ASSIGNMENTS = 5; // Uppercase variable doesn't change
int j; // Assignment counter
int score, sum_of_scores;
char reply = 'y'; // 'y' for yes, 'n' for no; initialized to yes
cout << "Enter scores for the first student? (y/n) " << endl;
while ((cin >> reply) && (reply == 'y' || reply == 'Y'))
{
sum_of_scores = 0; // Reset the accumulator to zero
j = 1; // Reset the assignment counter to 1
if (score < 0)
break ;
sum_of_scores += score;
j++;
}
cout << "The average score = " << sum_of_scores/NUM_ASSIGNMENTS << endl;
cout << "Enter scores for another student? (y/n) " ;
}
return 0;
} // Question: What is the output with the input: 4, 5, -6, 7, 8?
int main()
{
int NUM_ASSIGNMENTS = 5; // Uppercase variable doesn't change
int j; // Assignment counter
int score, sum_of_scores;
char reply = 'y'; // 'y' for yes, 'n' for no; initialized to yes
cout << "Enter scores for the first student? (y/n) " << endl;
while ((cin >> reply) && (reply == 'y' || reply == 'Y'))
{
sum_of_scores = 0; // Reset the accumulator to zero
j = 1; // Reset the assignment counter to 1
if (score < 0)
continue ;
sum_of_scores += score;
j++;
}
cout << "The average score = " << sum_of_scores/NUM_ASSIGNMENTS << endl;
cout << "Enter scores for another student? (y/n) " ;
}
return 0;
} // Question: What is the output with the input: 4, 5, -6, 7, 8 ?
if (j == 1) if (j == 1)
break; continue ;
cout << "Leave iteration " cout << "Leave iteration "
<< j << endl; << j << endl;
j++; j++;
} }
return 0; return 0;
} }
int main()
{
for (int j = 1; j <= 10; j++)
{
cout << "j = " << j << endl;
if (j == 3)
{
j = 10;
continue; // What if it is replaced by break;
}
}
return 0;
}
switch (section)
{
case 1:
cout << "Sergey Brin" << endl; break;
case 2:
cout << "Bill Gates" << endl; break;
case 3:
cout << "Steve Jobs" << endl; break;
case 4:
cout << "Jeff Bezos" << endl; break;
default:
cerr << "Error: Invalid lecture section " << section << endl;
break;
}
return 0;
}
COMP2011 (Spring 2023) p.12
Example: switch on Characters
#include <iostream> /* File: switch-char-bloodtype.cpp */
using namespace std;
switch (bloodtype)
{
case 'A':
cout << "Your donor must be of blood type: O or A\n";
break;
case 'B':
cout << "Your donor must be of blood type: O or B\n";
break;
case 'C':
cout << "Your donor must be of blood type: O, A, B, or AB\n";
break;
case 'O':
cout << "Your donor must be of blood type: O";
break;
default: // To catch errors
cerr << "Error: " << bloodtype << " is not a valid blood type!\n";
break;
}
return 0;
}
COMP2011 (Spring 2023) p.13
Example: switch with Sharing Cases
#include <iostream> /* File: switch-int-grade.cpp */
using namespace std;
int main() // To determine your grade (fictitious)
{
char grade; // Letter grade
int mark; // Numerical mark between 0 and 100
cin >> mark;
switch (mark/10)
{
case 10: // Several cases may share the same action
case 9:
grade = 'A'; break; // If mark >= 90
case 8: case 7: case 6: // May write several cases on 1 line
grade = 'B'; break; // If 90 > mark >= 60
case 5:
case 4:
case 3:
case 2:
grade = 'C'; break; // If 60 > mark >= 20
case 1:
grade = 'D'; break; // If 20 > mark >= 10
default:
grade = 'F'; break;
}
cout << "Your letter grade is " << grade << endl;
return 0;
}
COMP2011 (Spring 2023) p.14
Example: switch vs. if-else-if
cout << "Your letter grade is " << grade << endl;
return 0;
}
return 0;
}
COMP2011 (Spring 2023) p.17
New Data Types with enum
Example
enum weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY,
SATURDAY, SUNDAY }; // 0,1,2,3,4,5,6
int main()
{
enum shapes { TEXT, LINE, RECT, CIRCLE };
cout << "supported shapes: "
<< " TEXT = " << TEXT << " LINE = " << LINE
<< " RECT = " << RECT << " CIRCLE = " << CIRCLE << endl;
int myshape; // Why the type of myshape is not shape?
cin >> myshape;
switch (myshape)
{
case TEXT:
cout << "Call a function to print text" << endl; break;
case LINE:
cout << "Call a function to draw a line" << endl; break;
case RECT:
cout << "Call a function to draw a rectangle" << endl; break;
case CIRCLE:
cout << "Call a function to draw a circle" << endl; break;
default:
cerr << "Error: Unsupported shape" << endl; break;
}
return 0;
}
COMP2011 (Spring 2023) p.20
Part III
int main()
{
int factorial = 1, number;
cout << "Enter a non-negative integer: ";
cin >> number;
if (number > 0)
{
do
{
factorial *= number; // Same as: factorial = factorial*number
--number; // Same as: number = number-1
} while (number > 1);
}
int j;
while (j < 10)
{
Case 2:
cout << "hello again!" << endl;
j++;
}
int j = 0;
while (j < 10);
{
Case 3:
cout << "hello again!" << endl;
j++;
}
int main()
{ // Declare color variables immediately after the enum definition
enum color { RED, GREEN, BLUE, YELLOW, CYAN, PURPLE } x, y;
int xint, yint; // Input variables for the color variables
else
cerr << "Error: only support mixing RED/GREEN/BLUE!" << endl;
return 0;
} // Check what is really printed out
COMP2011 (Spring 2023) p.27