Transcript Class 7
CSCI 161
Lecture 7
Martin van Bommel
Control Statements
• Statements that affect the sequence of
execution of other statements
• Normal is sequential
• May want to repeat statements - iteration
• May or may not want particular statement
executed at all - conditional
Control Statement Structure
• Control line - first line (e.g. while)
• Body - statements enclosed in { } that are
repeated
• Body typically indented to show range of
control statement
Conditional Execution
• May want to execute a statement only if
some condition applies
• May wish to choose between two
alternative courses of actions depending on
some test
• Simplest form - if statement
if Statement
if (conditional-test)
{
…statements executed if test true...
} else {
…statements executed if test false...
}
Relational & Logical Operators
==
>
>=
<=
Equal
!= Not equal
Greater
<
Less
Greater or equal
Less or equal
• Logical operate on results of comparisons
! Not (true if operand false – one operand)
&& And (true if both true)
|| Or (true if either true)
Example 1
• “x is not equal to either 2 or 3”
if (x != 2 || x != 3)
• No, it should be
if (!(x == 2 || x == 3))
• or
if (x != 2 && x != 3)
Example 2
• “x is in the range from 0 to 10 exclusive”
if (0 < x < 10)
• No, it should be
if (0 < x && x < 10)
Example
• Leap year every fourth year, except
centuries, then just every fourth century
– year is divisible by 4 but not by 100, or
– year is divisible by 400
• Try
((y % 4 == 0) && (y % 100 != 0)) ||
(y % 400 == 0)
Bad Example
if (x <
cout <<
if (x >
cout <<
else
cout <<
5)
”A”;
5)
”B”;
”C”;
If Blocking Rule
• For any if statement that requires
(1) more than a single line, or
(2) an else clause,
always use braces to enclose in a separate
block the statements under the control of the
if statement .
Single-line if statement
if (condition) statement
where:
condition is the Boolean value to test
statement is a single statement to be
executed if condition is true
Multiline if statement
if (condition)
{
statements;
}
where
condition is the Boolean value to test
statements is a block of statements to be
executed if condition is true
if-else statements
if (condition)
{
statementsT ;
} else {
statementsF ;
}
Cascading if statement
if (condition1) {
statements1 ;
} else if (condition2) {
statements2;
...
} else {
statementsnone
}
Iteration and Loops
• Iteration - process of repeating an operation
• Essential for large amounts of data repeating statements shortens programs
• Loop - any portion of program repeated via
control statement
• e.g. while loop
The while statement
while (condition) {
statements
}
where
condition tested before first and every iteration
statements executed each time condition true
while operation
• Condition test performed before every cycle,
including first.
• If condition is initially false,
body of loop not executed at all.
• Condition test performed only at beginning of
each loop cycle.
• Even if condition becomes false during cycle,
cycle is completed.
while example
• Sum up digits of a number
sum =
while
sum
n =
}
0;
(n > 0) {
= sum + n % 10;
n / 10;
Infinite loops
• A loop cycle that never stops – infinite loop
• Condition of while statement never false
• e.g.
while (n >= 0) {
sum += n % 10;
n /= 10;
}
Repeat-N-Times Idiom
•
for (i = 0; i < N; i++)
{
statements to be repeated
}
• May want to add up 10 input numbers
for (i = 0; i < 10; i++)
{
cin >> value;
sum = sum + value;
}
Index Variable in Counting Loop
for (i = 0; i < N; i++)
• Variable i is called the index variable
• Any variable may be used - traditionally i
• Must be declared at beginning of function
• Initial value of i in loop is 0 - first cycle
• Next cycles value is 1, 2, etc.
• On last cycle, value is N – 1
• After loop, value of i is N
Non-Zero Counting Loop
• It is possible to modify for loop to begin at
another number
for (i = first; i <= last; i++)
• Note the <= operator instead of <
• Value of i ranges from first to last
• Used if value of i required in body of loop
for statement
for (init; test; step) {
statements
}
where
init is an expression evaluated at beginning
test is a condition for continuing the loop
step is an expression to prepare next cycle
for example
• Countdown from 10 to 0
for (t=10; t >= 0; t--) {
cout << t;
}
cout << ”Liftoff!\n”;
for more
• Note that expressions init, test, and step are
optional, but the semicolons must appear
for (;;)
- infinite loop
for (;t<10;) same as while (t<10)
Nested for loops
• Create a 10 x 10 multiplication table
for (row=1; row<=10; row++) {
for (col=1; col<=10; col++) {
cout << (row * col);
}
cout << endl;
}