Professional Documents
Culture Documents
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;
return 0;
}
int = 1;
do {
printf(“%d: %d”, i, i*i);
i += 1;
} while (i <= 100);
do {
printf(“Please insert a nonnegative number: “);
scanf (“%i”, &value);
} while (value < 0);
// is equal to
if (expr) {
do {
// statements
} while (expr);
}
// is equal to
// statements
while (expr) {
// statements
}
int a;
do {
scanf(“%i”, &a);
} while (a <= 1);
int a;
scanf(“%i”, &a);
while (a > 0) {
printf(“%d: %d\n”, a, a*a);
a = a-1;
}
Attention!
The postfix operators yield different values than +=1 and -=1!
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
// is equal to
init_expression;
while (loop_condition) {
// statements
loop_expression;
}
for (;;) {
// statements
}
do {
// statements
} while(1);
with break
without break