C++ Lec6 IIT
C++ Lec6 IIT
though C++
Abhiram G. Ranade
Ch. 7: Loops
Mark averaging
“Read marks of students from the keyboard and print the average.”
– Number of students not given explicitly.
– If a negative number is entered as mark, then it is a signal that all marks have
been entered.
– Assume at least one positive number is given.
Examples:
– Input: 98 96 -1, Output: 97
– Input: 90 80 70 60 -1, Output: 75
• Cannot be done using what you know so far.
– repeat repeats fixed number of times. Not useful
• Need new statement e.g. while, do while, or for.
• repeat, while, do while, for : “looping statements”.
Outline
• The while statement
– Some simple examples
– Mark averaging
• The break statement
• The continue statement
• The do while statement
• The for statement
• Loop invariants
– GCD using Euclid’s algorithm
The while statement
Form: while (condition) body
1. Evaluate condition.
2. If false, execution of statement ends.
3. If true, execute body. body can be a single statement or a block, in which case
all the statements in the block will be executed.
4. Go back and execute from step 1.
• The condition must eventually become false, otherwise the program will never
halt. Not halting is not acceptable.
• If condition is true originally, then value of some variable in condition must
change in the execution of body, so that eventually condition becomes false.
• Each execution of the body = iteration.
While flowchart
Previous statement in the program
...previous statement...
while(condition) body
...Next statements... False
Condition
True
Body
to step 4.
4. sum = sum + nextmark True
5. count = count + 1
6. Go to step 2. Body
7. Print sum/count.
Structures do not match.
Next statement in the program
A different flowchart for mark averaging
• Claim: If a certain block is
executed after two paths
merge, then we might as
well execute it separately
on the two paths before
the paths merge, and not
after.
• Blocks B,A can be merged
in that order to get the
body of while.
A different flowchart for mark averaging
main_program{
float nextmark, sum = 0;
int count = 0;
cin >> nextmark; // A
while(nextmark >= 0){ // C
sum = sum + nextmark; // B
count = count + 1; // B
cin >> nextmark; // A copy
}
cout << sum/count << endl;
}
Exercise
Write a turtle controller which receives commands
from the keyboard and acts per the following rules:
• If command = ‘f’ : move turtle forward 100 pixels
• If command = ‘r’ : turn turtle right 90 degrees.
• If command = ‘x’ : finish execution.
What we discussed
• In while loops, the first step is to check whether we
need to execute the next iteration.
• In some loops that we want to write, the first step is not
a condition check. The condition check comes later.
• Such loops can be modified by making a copy of the
steps before the condition check.
• NEXT: loops in which condition checks can happen also
inside the body
🌺
The break statement
Form: The break keyword is a statement by itself.
What happens when control reaches break
statement:
• The execution of the while statement which
contains it is terminated.
• The execution continues from the next statement
following that while statement.
Example of break
main_program{ • The condition of the while statement is
given as true – body will always be
float nextmark, sum = 0; entered.
int count = 0; • If nextmark < 0:
while(true){ – the while loop execution will terminate
– Execution continues from the statement after
cin >> nextmark; while, i.e. cout ...
if(nextmark < 0) break; • Exactly what we wanted!
– No need to copy code.
sum += nextmark;
• Some programmers do not like break
count++; statements because continuation
} condition gets hidden inside body,
instead of being at the top.
cout << sum/count << endl; • Condition for breaking = compliment of
} condition for continuing loop
The continue statement
• The continue is another single word statement.
• If it is encountered in execution:
– The control directly goes to the beginning of the loop
for the next iteration,
– Statements from the continue to the end of the loop
body are skipped.
Example
Mark averaging with an additional condition:
for(int i=1;
i <= 100;
i++)
cout <<i<<‘ ‘<<i*i*i<<endl;
Remarks
• New variables can be defined in initialization. These
variables are accessible inside the loop body, including
condition and update, but not outside.
• Variables defined outside can be used inside, unless
shadowed by new variables.
• Break and continue can be used, with natural interpretation.
• Typical use of for: a single variable is initialized and
updated, and the condition tests whether it has reached a
certain value. Such a variable is called the control variable
of the for statement.
Determining whether a number n is prime