Introduction To Object-Oriented Programming COMP2011: Program Flow Control
Introduction To Object-Oriented Programming COMP2011: Program Flow Control
Object-Oriented Programming
true
int x;
cin >> x;
statements if (x < 0)
{
x = -x;
}
if (x > y)
{
temp = x; // Save the original value of x
x = y; // Replace x by y
y = temp; // Put the original value of x to y
}
boolean
Example: To find the larger value.
expression
false true
int x, y, larger;
if (<bool-exp>) { <stmts> }
else if (<bool-exp>) { <stmts> }
else if (<bool-exp>) { <stmts> }
..
.
else { <stmts> }
cout << "Your letter grade is " << grade << endl;
return 0;
}
{horner, kccecia, lixin}@cse.ust.hk COMP2011 (Spring 2018) p.8
Relational Operators
p q !p p && q p || q
T T F T T
T F F F T
F T T F T
F F T F F
Example
/* Example: get the larger of two numbers */
larger = (x > y) ? x : y;
if (condition1)
{
if (condition2)
if (condition3)
cout "conditions 1,2,3 are true." endl;
else
cout "conditions 1,2 are true." endl;
else
cout "condition1 true; condition2 false." endl;
}
Loops or Iterations
cout << endl << "The maximum number = " << max << endl;
return 0;
}
After you have written the codes for a loop, try verifying the
following cases:
The first iteration.
The second iteration.
The last iteration.
Do you know exactly how many iterations will be performed?
How can the loop terminate? Otherwise, you have an infinite
loop! And the program runs forever!
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);
}
cout << number << "! = " << factorial << endl;
return 0;
}
if (n < 0)
cerr << "Error: n < 0!" << endl;
else
{
for (int j = 1; j <= n; j++)
result *= x;
cout << x << " to the power of " << n << " = " << result << endl;
}
return 0;
}
{horner, kccecia, lixin}@cse.ust.hk COMP2011 (Spring 2018) p.28
Remarks on for Statement
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 = 8;
continue; // What if it is replaced by break;
}
}
return 0;
}
int sum;
Case 1: while (cin >> x)
sum += x;
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++;
}