Control flow in c
-
Upload
thirumalaikumar3 -
Category
Education
-
view
296 -
download
0
Transcript of Control flow in c
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 mealtimeelse, if he’s bigger than me… hope he doesn’t look hungryelse, if we’re the same else sizeWait to grow bigger
Parentheses surround the best One statement becomes the “then
part’ If more are required, braces must be
readscanf(“%i”, &i);
If(i > 0)printf(“a positive number was entered\n”);
If(i < 0) {printf(“a negative number was entered\n”);i = -1;
}
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 entered\n”);
input an integer: -6a positive number was entered
An optional else may be added One statement by default, if more are
required, braces must be usedif(i > 0)
printf(“i is positive\n”);else
printf(“i is negative\n”);
if(i > 0)printf(“i is positive\n”);
else {printf(“i is negative\n”);i = -1;
}
Else associated with the nearest ifint i = 100;if(i > 0) if (i > 1000)
printf(“i is big\n”); else
printf(“i is reasonable\n”);
int i = 100;if(i > 0) { if (i > 1000)
printf(“i is big\n”); } else
printf(“i is negative\n”);
i is reasonable
i is negative
C supports a switch for multi-way decision making
switch (c) { case ’a’ ; case ‘A’:
printf(“area = %.2f\n”, r * r * r pi);break;
case ‘c’: case ‘C’:printf(“circumference = %.2f\n” , 2 * r * pi);
case ‘q’:printf(“quit option chosen\n”);
default:printf(“unknown option chosen\n”);break;
}
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 = 3\n”); case 2: printf(“i = 2\n”); case 1: printf(“i= 1\n”);}
i = 3i = 2i = 1
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 tree\n”);}
• 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 = %i\n”, j--);
while (j > 0) {printf(“j = %i\n”, j);j --;
}
j = 5j = 4j = 3j = 2j = 1
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 = %i\n”, 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
Remember to get the condition the right way around!
int j = 5;printf (‘start\n”);while (j == 0) printf (“j = %i\n”, j--);printf (“end\n”);
startend
user probably intends “until j is equal to zero”, however this is NOT the way to write it
do while guarantees execution at least once
int j = 5 ;printf (“start\n”);do printf (“j = %i\n”, j--);while (j > 0);printf (“stop\n”);
startj = 5j = 4j = 3j = 2j = 1Stop
int j = -10 ;printf (“start\n”);do { printf (“j = %i\n”, j); j--;} while (j > 0);printf (“stop\n”);
startj = -10
stop
for encapsulates the essential elements of a loop into one statementfor (initial – part; while- condition; update-part) body;
int j;
for (j = 5; j > 0; j--) printf(“j = %i\n”, j);
j = 5 oddj = 4 evenj = 3 oddj = 2 even j = 1 odd
for (j = 5; j > 0; j--) { printf(“j = %i”, j); printf(“%s\n”, ((j%2)==0) ? “even” : “odd”);}
j = 5
j = 4
j = 3
j = 2
j = 1
Remember to get the for condition the right way around (it is really a while condition)
int j;printf (“start\n”);for (j = 5; j == 0; j--) printf(“j = %i\n”, j);printf (“end\n”);
startend
user probably intends “until j is equal
to zero”, however this is NOT the
way to write it either!
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 %.21f\n”, angle, sin(angle));return 0;
}
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
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 inttype an int: notype an int: 16j = 16
if scanf returns 1, jump out of the loop
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 = %i\n”, j);}
j= 1j= 2j =4j= 5j= 7j= 8j= 10
if j is exactly divisible by 3, skip
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
!