control flow in c
TRANSCRIPT
![Page 1: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/1.jpg)
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
![Page 2: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/2.jpg)
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;
}
![Page 3: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/3.jpg)
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
![Page 4: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/4.jpg)
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;
}
![Page 5: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/5.jpg)
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
![Page 6: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/6.jpg)
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;
}
![Page 7: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/7.jpg)
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
![Page 8: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/8.jpg)
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”);}
![Page 9: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/9.jpg)
• 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
![Page 10: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/10.jpg)
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
![Page 11: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/11.jpg)
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
![Page 12: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/12.jpg)
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
![Page 13: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/13.jpg)
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
![Page 14: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/14.jpg)
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!
![Page 15: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/15.jpg)
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;
}
![Page 16: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/16.jpg)
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
![Page 17: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/17.jpg)
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
![Page 18: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/18.jpg)
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
![Page 19: Control flow in c](https://reader031.vdocument.in/reader031/viewer/2022030314/58864e901a28ab32768b6c4b/html5/thumbnails/19.jpg)
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
!