1. Decisions – if then else
More decisions – switch
Loops- while, do while, for
Keyword break
Keyword continue
If I’m bigger than him and I’m
hungry….
then it’s mealtime
else, if he’s bigger than me…
hope he doesn’t look hungry
else, if we’re the same else size
Wait to grow bigger
2. Parentheses surround the best
One statement becomes the “then part’
If more are required, braces must be read
scanf(“%i”, &i);
If(i > 0)
printf(“a positive number was enteredn”);
If(i < 0) {
printf(“a negative number was enteredn”);
i = -1;
}
3. A semicolon after the condition, form as “do
nothing’ statement
printf(“input an integer: “);
scanf(“%i”, &j);
If(j > 0);
printf(“a positive number was enteredn”);
input an integer: -6
a positive number was entered
4. An optional else may be added
One statement by default, if more are required,
braces must be used
if(i > 0)
printf(“i is positiven”);
else
printf(“i is negativen”);
if(i > 0)
printf(“i is positiven”);
else {
printf(“i is negativen”);
i = -1;
}
5. Else associated with the nearest if
int i = 100;
if(i > 0)
if (i > 1000)
printf(“i is bign”);
else
printf(“i is reasonablen”);
int i = 100;
if(i > 0) {
if (i > 1000)
printf(“i is bign”);
} else
printf(“i is negativen”);
i is
reasonable
i is negative
6. C supports a switch for multi-way decision
making
switch (c) {
case ’a’ ; case ‘A’:
printf(“area = %.2fn”, r * r * r pi);
break;
case ‘c’: case ‘C’:
printf(“circumference = %.2fn” , 2 * r * pi);
case ‘q’:
printf(“quit option chosenn”);
default:
printf(“unknown option chosenn”);
break;
}
7. Only integral constants may be tested
If no condition matches, the default is executed
If no default, nothing is done (not an error)
The break is important
float f;
switch (f) {
case 2:
…..
switch (i) {
case 2 * j:
…..
i = 3;
switch (i) {
case 3: printf(“i = 3n”);
case 2: printf(“i = 2n”);
case 1: printf(“i= 1n”);
}
i = 3
i = 2
i = 1
8. printf (“on the “) ;
switch (i) {
case 1: printf(“1st”); break;
case 2: printf(“2nd”); break;
case 3: printf(“3rd”); break;
default: printf(“%ith”,i); break;
}
printf (“day of Christmas my true love sent to me”);
switch (i) {
case 12: printf(“twelve lords a leaping,”);
case 11: printf(“eleven ladies dancing,”);
case 10: printf(“ten pipers piping,”);
case 9: printf(“nine drummers drumming,”);
case 8: printf(“eight maids a milking,”);
case 7: printf(“seven swams a swimming,”);
case 6: printf(“six geese a laying,”);
case 5: printf(“five gold rings,”);
case 4: printf(“four calling birds,”);
case 3: printf(“three french hens,”);
case 2: printf(“two turtle doves and ”);
case 1: printf(“a patridge in a pear treen”);
}
9. • The simplest C loop is the while
• Parentheses must surround the condition
• One statement forms the body of the loop
• Braces must be added if more statements
are to be executed
int j = 5;
while (j > 0)
printf (“j = %in”, j--);
while (j > 0) {
printf(“j = %in”, j);
j --;
}
j =
5
j =
4
j =
3
j =
2
j =
1
10. A semicolon placed after the condition forms a
body that does nothing
Sometimes an empty loop body is required
int j = 5;
while (j > 0 );
printf (“j = %in”, j --);
program disappears into an
infinite loop
int c, j;
while (scanf( “%i”, &j) ! = 1
while ( (c = getchar() )! = ‘n’)
;
placing semicolon on the
line below makes the
intention obvious
11. Remember to get the condition the right way
around!
int j = 5;
printf (‘startn”);
while (j == 0)
printf (“j = %in”, j--);
printf (“endn”);
start
end
user probably intends
“until j is equal to zero”,
however this is NOT the
way to write it
12. do while guarantees execution at least once
int j = 5 ;
printf (“startn”);
do
printf (“j = %in”, j--);
while (j > 0);
printf (“stopn”);
start
j = 5
j = 4
j = 3
j = 2
j = 1
Stop
int j = -10 ;
printf (“startn”);
do {
printf (“j = %in”, j);
j--;
} while (j > 0);
printf (“stopn”);
start
j = -10
stop
13. for encapsulates the essential elements of a
loop into one statement
for (initial – part; while- condition; update-
part)
body;
int j;
for (j = 5; j > 0; j--)
printf(“j = %in”, j);
j = 5 odd
j = 4 even
j = 3 odd
j = 2 even
j = 1 odd
for (j = 5; j > 0; j--) {
printf(“j = %i”, j);
printf(“%sn”, ((j%2)==0) ? “even” : “odd”);
}
j = 5
j = 4
j = 3
j = 2
j = 1
14. Remember to get the for condition the right way
around (it is really a while condition)
int j;
printf (“startn”);
for (j = 5; j == 0; j--)
printf(“j = %in”, j);
printf (“endn”);
start
end
user probably
intends “until j is equal
to zero”,
however this is NOT the
way to write it
either!
15. Unlike some languages, the for loop is not
restricted to stepping up or down by 1
#include <math.h.
int main(void)
{
double angle;
for(angle – 0.0; angle < 3.14159; angle += 0.2)
printf(“sine of %.11f is %.21fn”, angle, sin(angle));
return 0;
}
16. The initial and update parts may contain
multiple comma separated statements
the initial, condition and update parts may
contain no statements at all!
int i, j, k;
for (i = 0, j = 5, k = -1; i < 10; i++, j++,
k--)
for (; i < 10; i++, j++, k--)
for (; i < 10;)
for (; ;)
use of a while loop
would be clearer here!
creates an infinite loop
17. The break keyword forces immediate exit from
the nearest enclosing loop
use in moderation!
for (; ;) {
printf (“type an int: “);
if (scanf (“%i”, &j) ==1)
break;
while ( (c = getchar() ) != ‘n’)
;
}
printf (“j = %n”, j);
type an int: an int
type an int: no
type an int: 16
j = 16
if scanf returns 1,
jump out of the loop
18. The continue keyword forces the next iteration
of the nearest enclosing loop
use in moderation!
for (j = 1; j <=10; j++) {
if (j % 3 ==0 )
continue;
printf (“ j = %in”, j);
}
j= 1
j= 2
j =4
j= 5
j= 7
j= 8
j=
10
if j is exactly divisible
by 3, skip
19. If(then) else – watch the semicolons
switch can test integer values
while, do while, for – watch the semicolons
again
break
continue else…….
I want to be a tomato
!