You are on page 1of 12

Loops

START

Read n
•  Compute sum of n numbers Initialise sum = 0
•  Take n as user input F
n
•  Read numbers one by one items?
from user T
•  Keep variable sum to
Input x
accumulate the sum sum = sum + x
–  initialise to zero
Print
•  How do we know that n sum
items have been read?
END
Jan 2016 P. R. Panda, I.I.T Delhi 12
Loops
START

Read n
•  How do we know that n Initialise sum = 0, i = 0
items have been read?
F
–  count using new variable i
i < n?

T
Input x
sum = sum + x
i=i+1
Print
sum

END
Jan 2016 P. R. Panda, I.I.T Delhi 13
WHILE Loops
START Error!
main () Always check
{ validity of input!
Read n int sum=0, i=0, x, n; What should we
Initialise sum = 0, i = 0 write here?
cin >> n;
F if (n < 0) ... Loop Body
i < n? while (i < n) {
cin >> x; Loop
T sum = sum + x; Iteration =
Input x i = i + 1; One pass
sum = sum + x } over the Loop
i=i+1 cout << sum << endl; Body
Print }
sum
What does the instruction
END sequence look like?
Jan 2016 P. R. Panda, I.I.T Delhi 14
DO-WHILE (or REPEAT-UNTIL)
START Loops START

Read n Read n
Initialise sum = 0, i = 0 Initialise sum = 0, i = 0
•  Position of
condition check
F
inverted
i < n? Input x
sum = sum + x •  What is the
T i=i+1 difference?
•  Which is
Input x better?
sum = sum + x F
i=i+1 i < n?
Print
sum T Print
sum

END
Jan 2016 P. R. Panda, I.I.T Delhi
END 15
FOR Loops
START

main ()
Read n Loop Header
{
Initialise sum = 0
int sum=0, i=0, x, n;
n iterations cin >> n;... Loop Body
done
for (i = 0; i < n; i++) {
Input x
sum = sum + x cin >> x;
sum = sum + x;
}
Iterate n cout << sum << endl;
times }
Print
sum

END
Jan 2016 P. R. Panda, I.I.T Delhi 16
The FOR Loop
main ()
•  Header {
–  Fields are optional (try it int sum=0, i=0, x, n;
out) cin >> n;...
for (i = 0; i < n; i++) {
•  Increment: i++ or cin >> x;
“i = i + 1” (approx) sum = sum + x;
}
•  General case: same as cout << sum << endl;
WHILE }

Jan 2016 P. R. Panda, I.I.T Delhi 17


Expressions: Operators
•  Arithmetic operators: +, -, *, /
•  Integer division returns quotient (truncates fractional part)
•  Remainder (mod) operator: (a % b)
–  remainder when a is divided by b
•  Relational operators: >, >=, <, <=
•  Equality: ==, !=
== distinguished from ‘=‘ (assignment)
•  Logical operator: && (AND), || (OR), ! (NOT)
•  Bitwise operators: &, |, ^
•  Shift: <<
–  “Overloaded” in C++ to operate with “cout”, etc.
–  “Overloading”: new meaning in certain contexts

Jan 2016 P. R. Panda, I.I.T Delhi 18


Statement

•  <expression> ; // includes assignments


•  if () {...} else {...}
•  while (...) {...}

Jan 2016 P. R. Panda, I.I.T Delhi 19


Control Flow Statements
main ()
{...
•  Default flow of control while (i < n) {
is: execute next cin >> x;
statement sum = sum + x;
i = i + 1;
•  Modifying the control if (i == t)
flow: break;
–  break if (i % 4 == 0)
continue;
–  continue t = t + 2*x;
–  exit }
next = t; ...
–  return
}
Jan 2016 P. R. Panda, I.I.T Delhi 20
Typical Loop Idioms

•  Counting iterations
•  Accumulating info. over loop iterations
–  sum of numbers
•  Boolean flags
–  searching if a condition is valid
–  retrieving the valid iteration

Jan 2016 P. R. Panda, I.I.T Delhi 21


Loop Templates

i = 0; sum = 0;
cin >> n; cin >> n;
while (n > 0) { while (n > 0) {
i = i + 1; sum = sum + n;
cin >> n; cin >> n;
} }
num_pos = i; Accum = sum;
Counting: How Accumulation: sum of
many positive positive number
numbers? sequence

Jan 2016 P. R. Panda, I.I.T Delhi 22


Loop Templates
bool found = false;
bool found = false; cin >> n; i = 0;
cin >> n; while (n > 0) {
while (n > 0) { if (n == 10) {
if (n == 10) found = true;
found = true; located_at = i;
sum = sum + n; }
cin >> n; i = i + 1;
} cin >> n;
if (found)... }
if (found) {
Boolean Flag: // use located_at =
search for 10
}
Boolean Flag: search for 10,
find location

Jan 2016 P. R. Panda, I.I.T Delhi 23

You might also like