CS 111 Computing
Fundamentals
Selection (2)
_Bool
Iteration (1)
Dr. Christina Class
Repetition
nested if
printing the maximum of three values
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 3
dangling else
#include <stdio.h>
int main()
{
int a = 5;
if (a==5) {
printf("a is 5\n");
}
if (a==10) {
printf("a is 10\n");
}
else {
printf("else case");
}
return 0;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 4
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 5
Decision making
selection with switch
conditional operator
example program
#include <stdio.h>
int main()
{
int studentType;
printf("Please input student type (1,2 or 3)");
scanf("%i", &studentType);
if(studentType == 1) {
printf("Bachelor Student\n");
} else if (studentType == 2) {
printf("Master Student\n");
} else if(studentType == 3) {
printf("PhD Student\n");
} else {
printf("invalid student type\n");
}
return 0;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 7
The former program has some
special characteristics:
the if condition checks on equality of
one expression of type int with a
specific value
this is done for several specific values
In this case we have a specific
construct, select.
The next slides only concentrate on
the decision making (if) part of the 8
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5
version with if
if(studentType == 1) {
printf("Bachelor Student\n");
} else if (studentType == 2) {
printf("Master Student\n");
} else if(studentType == 3) {
printf("PhD Student\n");
} else {
printf("invalid student type\n");
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 9
version with switch
switch(studentType) { expression to be evaluated
everything after
case 1:
case until break; printf("Bachelor Student\n");
or the end of
switch is executed break; possible values for
if case covers the case 2: the expression
value of the
expression printf("Master Student\n");
break;
case 3:
printf("PhD Student\n");
break; specification of default
default: if no other value holds
printf("invalid student type\n");
break;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 10
default can be
anywhere
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 11
fall through
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 12
fall trough (2)
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 13
Menus
switch is very often
used to implement
menu structures
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 14
Exercise
Given is a program that reads two
integer variables a and b and one
symbol s.
Write a switch statement that does
the following:
it checks the value of s
if s is ‘+’ it prints the sum of a and b
if s is ‘-’ it prints the difference of a
and b
in other cases it prints ‘unsupported
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 15
_Bool
_Bool is a data type introduced with
C99
it can store the values 0 and 1
if you include stdbool.h, you can
use the following
bool as synonym for _Bool
true (constant 1)
false (constant 0)
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 17
Looping (1)
Consider a program to compute and print
the square of any number input by the user.
If the user wants to compute the square
value of different numbers, he/she must
start the program several times.
It would be nice that the user can repeat
the computation of a square number for as
many number as he likes without restarting
the program.
This is done by loops, the construct is
called iteration.
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 19
In C there are 3 different kind of
loops
the while loop: basic top driven loop
the for loop: top driven loop with more
loop logic contained in the statement
itself
the do … while loop: bottom driven
loop
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 20
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 5 21
top driven loop in C
while (exp) {
// statements
}
int i = 1;
while (i <= 100) {
printf(“%d: %d”, i, i*i);
i += 1;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 22
top driven loop in C
// endless loop
while (1) {
// statements
}
// also an endless loop
int = 1;
while (i <= 100) {
printf(“%d: %d”, i, i*i);
//i += 1;
}
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 23
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 5 24
Exercises
Make a walk through former
program with the following values:
12,18
18,12
1001,1331
277,123
9888,6060
100,101
CS 116 – Summer 2014/15 - Dr. Christina Class - Lecture 5 25