1 / 24

Program Control

Program Control. Selection Repetition Altering the flow of control Unconditional branch More operators. Selection – 3 Ways. If structure (single selection) If/else structure (double, multiple selection) if(condition1) statement1; else if(condition2) statement2; . . .

teneil
Télécharger la présentation

Program Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Program Control • Selection • Repetition • Altering the flow of control • Unconditional branch • More operators

  2. Selection – 3 Ways • If structure (single selection) • If/else structure (double, multiple selection) if(condition1) statement1; else if(condition2) statement2; . . . . . .; else statementN; • Switch structure (multiple selection)

  3. If Structure • If no parenthesis, then only the 1st statement following the if statement is associated with it • if(a>b) c=3; d=4; • So d=4; will always be evaluated

  4. If Structure • To make your code clear (to yourself and others), you should always use brackets after your if-statements • if(a>b){ c=3;} d=4; • if(a>b){ c=3; d=4;}

  5. If/else Ambiguity • If one if statement doesn’t have an ending else & another one does, may be ambiguous • Resolved by associating the else with the closest if if(a>b) if(c>d) e=3; else d=4; • Must use braces to associate with the outside if if(a>b){ if(c>d) e=3; }else d=4;

  6. Switch Structure • Handles a series of decisions • Must include a break statement • If not, each case statement will be executed until encounter another break statement or reach end • Several cases can execute the same statements • By listing case statements together • Can only test constant integer expressions

  7. /* Counting A,B,C or a,b,c (counting.c)*/ #include <stdio.h> int main(){ int a=0,b=0,c=0,e=0,ch=0; while((ch=getchar()) != EOF){ switch(ch){ case 'A': case 'a': a++; break; case 'B': case 'b': b++; break; case 'C': case 'c': c++; break; default: e++; break;/*defensive programming*/ } } printf("\na=%d\nb=%d\nc=%d\nextra=%d",a,b,c,e); return 0; }

  8. Character I/O • I/O = Input/Output • Text input or output = stream of characters • Standard library functions for char I/O char c; c = getchar(); /*input*/ putchar(c); /*output*/ • Will get or put one character at a time from the input or output text stream

  9. End-of-File Marker • EOF is a character used to mark the end of a file or the end of input • EOF key combinations on different systems: • UNIX & Mac: Ctrl-d • PCs: Ctrl-z

  10. I/O Redirection in UNIX • Instead of typing input for a program, you can use I/O redirection in UNIX to use a file as input or output to your program • < (less-than sign) is for input • > (greater-than sign) is for output • For example: • File input: ./program < input.txt • File output: ./program > output.txt • Both: ./program < input.txt > output.txt • Use EOF (end of file) to end the program • Try it: counting < electricity.txt

  11. Repetition – 3 Ways • while (condition) {statements;} • while(1+2*5){ printf(“infinite loop ”); } • do{statements} while(condition) • do{printf(“one”); } while(1 < 0); • for(initialize, condition, increment) • for(i=0; i<5; i=i+1) {printf(“five”);} • Note:For conditions in C, • false = 0 & true = nonzero (anything else)

  12. for = = while • for(expr1;expr2;expr3) statement; • Is equivalent to: • expr1; while(expr2){ statement; expr3; } • This will create an infinite loop: • for(;;){ . . .}

  13. Comma in For Loops • Can put commas in for loops • Evaluated left to right #include <stdio.h> int main() { int a=0,b=0; for(a=0, b=10; a<b; a++, b--) printf("a=%d b=%d\n",a,b); return 0; }/*see comma.c*/

  14. Alter Flow of Control • break; • Immediately exit from while, for, do/while, or switch statements • continue; • Skips the remaining statements • Performs the next iteration of while, for, or do/while loop

  15. Unconditional Branch #include <stdio.h> int main(){ start: printf("hello"); gotostart; return 0; } /*see goto.c*/

  16. Goto • As a general rule, not a good idea to use goto • Might use when need to escape from a deeply nested structure (since break will only exit from innermost loop) for(. . .) { for(. . .){ if(need_to_stop) goto stop_loop; }} stop_loop: Continue with the program. . .

  17. ASCII Character Codes Character Decimal Hex Binary • '0' 48 dec 0x30 0011 0000 • '9' 57 dec 0x39 0011 1001 • 'A' 65 dec 0x41 0100 0001 • 'Z' 90 dec 0x5A 0101 1010 • 'a' 97 dec 0x61 0110 0001 • 'z' 122 dec 0x7A 0111 1010

  18. Formatted Output • Conversion specifiers • %c (char), %d (dec), %x (hex), %f (float) char c = 'A'; int i = 66; printf("c=%c i=%c\n", c, i); //c=A i=B printf("c=%d i=%d\n", c, i); //c=65 i=66 printf("c=%x i=%x\n", c, i); //c=41 i=42 • What’s the output if we set i = 66 + 1024? • See format.c

  19. Relational & Logical Operators • Relational: >, >=, <, <=, ==, != • Logical: &&, ||, ! • Evaluated left to right • Evaluation stops as soon as true or false is determined • Ex: if(a && b && c) • Will stop if “a && b” is false • Probably should write as: if((a && b) && (b && c)) • Often see if(!x), which means if(x==0)

  20. Type Conversions • Implicit conversion – if an operation has operands of different types, the “narrower” one will be converted to the “wider” one • 2.0 / 5 evaluates to 0.4 (a float) • Explicit conversion – can force a conversion with a cast • (type-name) expression • ((int)2.0) / 5 evaluates to 0 (an integer) • a = sqrt((double) x); (a = double, x = integer)

  21. Increment & Decrement Operators • Preincrement • Add 1, then assign new value • For example, if a = 3 b = ++a; /*(b=4, a=4)*/ • Postincrement • Assign old value, then add 1 • For example, if a = 3 b = a++; /*(b=3, a=4)*/ • Example: increment.c

  22. Assignment Operators x = x + 5; • Can also be written as x += 5; a *= b + 5; • Evaluates to a = a *(b + 5); • And not a = a * b + 5;

  23. Conditional Expressions if (x>y) a=0; else a =1; • Can also be written as x>y ? a = 0 : a = 1;

  24. Precedence • Precedence & of operators • See table in Dietel & Dietel, Appendix A • If you are not sure of operator precedence, or if you want to make your code more readable • Use parenthesis ()

More Related