You are on page 1of 35

CS 111 Computing

Fundamentals

Iteration (2)
Dr. Christina Class
Repetition
switch
switch(studentType) {
case 1:
printf("Bachelor Student\n");
break;
case 2:
printf("Master Student\n");
break;
case 3:
printf("PhD Student\n");
break;
default:
printf("invalid student type\n");
break;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 3
Euclid: greatest common
divisor
#include <stdio.h>

int main(void)
{
int u,v,temp;

printf("Please type in two nonnegative integers.\n");


scanf ("%i%i", &u, &v);

while (v!=0) { // can be replaced with: while (v) {


temp = u%v;
u = v;
v = temp;
}

printf("Their greatest common divisor is %i\n", u);

return 0;
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 4


do {…} while(…);
top driven vs. bottom
driven loops

in a top driven loop, first we evaluate


the condition
if it is true, the statements in the loop
block are carried out
then we evaluate again the condition
until it is false

in the bottom driven loop in C, the


statements in the loop block are
carried out
then we evaluate the condition
if it is true the statements are again
carried out until the condition is false

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 6


bottom driven loop in C
do {
// statements
} while (expr);

int = 1;
do {
printf(“%d: %d”, i, i*i);
i += 1;
} while (i <= 100);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 7


int value;

do {
printf(“Please insert a nonnegative number: “);
scanf (“%i”, &value);
} while (value < 0);

a do while loop is well suited for all statements,


that have to be executed at least once.

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 8


important

 the statements in the top driven


loop might not be executed at
all, as the condition is checked
before the loop statements are
executed
 the statements in the bottom
driven loop will be executed at
least once, as the condition is
checked after the loop statements
are executed
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 9
while (expr){
// statements
}

// is equal to

if (expr) {
do {
// statements
} while (expr);
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 10


do {
// statements
} while (expr);

// is equal to

// statements
while (expr) {
// statements
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 11


Exercises

 Convert the following loop into a


while loop

int a;
do {
scanf(“%i”, &a);
} while (a <= 1);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 12


Exercises

 Convert the following loop into a do


while loop

int a;
scanf(“%i”, &a);
while (a > 0) {
printf(“%d: %d\n”, a, a*a);
a = a-1;
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 13


Increment and decrement
operators
 very often we need statements for
 i += 1 or j = j+1
 j -= 1 or j = j-1
 increment (++) and decrement (--)
operators are short forms for such
statements
 they have a postfix (i++, j--) and a
prefix (++i, --j) notation.

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 15


return values and side
effects
Operator example yielded value side effect
prefix- ++ i i+1 i = i+1
increment
postfix- i ++ i i = i+1
increment
prefix- -- i i–1 i = i-1
decrement
postfix- i -- i i = i-1
decrement

Attention!
The postfix operators yield different values than +=1 and -=1!

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 16


CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 17
Example from the
lecture

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 18


for loop
 many loops are of the following
form:
a variable is initialized

int i = 10;
a condition over the variable
while (i <= 100) { controls the loop
// do something
i += 1;
}
the variable is modified
at the end of the loop

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 20


for loop
for (init_expression; loop_condition;
loop_expression) {
// statements
}

// is equal to

init_expression;
while (loop_condition) {
// statements
loop_expression;
}

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 21


example

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 22


nested for loops

the maximum value of j


(inner for loop)
is dependent from i (outer for loop)

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 23


CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 24
 You do not need to specify every expression in
the for loop, but you need to have all semicolons

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 25


 Attention! The following example is
an endless loop for input 5. Why?

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 26


endless loops
while(1) {
// statements
}

for (;;) {
// statements
}

do {
// statements
} while(1);

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 27


break and continue
testing a number to be
prime

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 29


improvement: all numbers are divisible by 1 and themselves

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 30


 after we have found the first
divisor, we know that a specific
number cannot be prime
 in this case we would like to stop
the for loop
 this can be done with break;
 break stops the execution of a loop
and continues with the first
statement after the loop
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 31
continue with if (1st statement after the loop)

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 32


improvement, input
1234567890

 with break

 without break

there is also execution time for the input required


CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 33
 if we do not want to leave the whole loop but
stop the execution of the loop statements for
this time, we can use continue;
 in while() and do{} while() loops, continue
stops the execution of the loop statements
and jumps to testing the loop condition
 in for() loops, continue stops the execution
of the loop statements, jumps to the
loop_expression and then tests the loop
condition

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 34


Exercises

 Enter a number. Use a for loop to


print 10 multiples of this number.
 Use a for loop to print all even
numbers from 1 to 100.
 Use a for loop to print all even
numbers from 1 to 100. Apply the
continue statement.

CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 6 35

You might also like