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
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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