Lecture 5; control ------------------ Announce ----------- - homeworks1 and 2 due, turnins are timestamped - no lates for homeworks! only for projects, 1/2 credit - hw1/2 solutions posted after class - hw3 is posted - start early, come to office hours tommorrow Today Outline -------- - quick review of variables - type conversion - arrays - C control structures: decisions and loops - expressions - statements/statement blocks - variable scope - functions Review --------- - must be declared before they're used - must have a type - variable is a placeholder in memory - size of placeholder defines range of values - operators: binary, unary, conditional, order of ops - printf and scanf - scanf %f vs. %lf Constants ----------- #define FOO 10 Type Conversion ---------------- - can have expression with several types in expression - C promotion scheme, smaller vars promoted - can impose conversion with casting - demotion dangerous, floats are truncated Arrays --------------------- - variables covered so far hold one value - C gives us a way to define more than one placeholder at once by arrays ** int birthyears[10]; float bodytemps[10]; char firstinitials[10]; char lastinitials[10]; - each element is like a single variable - accessed by integer index starting at 0 birthyears[0] = 1978; birthyears[9] = 2002; printf("%d\n", birthyear[1]); - index can be variable - chunk of contig memory a b c d e f g ... - ddim arrays int foo[3][4]; ** 0 1 2 3 0 a b c d 1 e f g h 2 i j k l - still just contiguous chunk of memory 0 1 2 a b c d e f g h i j k l 0 1 2 3 0 1 2 3 0 1 2 3 - int foo[A][B] - foo[a][b] - a*B + b - careful not to overflow the array! - can cause segmentation fault - try to access memory beyond that referenced by current var declarations Useful Program Constructs --------------------------- ** - we know variables for storing data - we know input and output - we know performing operations on data - what else would we like to do with a program? - make decisions - repeat processes - ex: what does ls do? - ls /tmp - uses a variable to store "/tmp" - performs OP to check whether /tmp exists - makes a decision - if tmp does not exist, error and exit - otherwise, do a loop - while files in /tmp - output files to screen Conditional Control Structures ------------------------------- - used for making decisions in a program - most take a conditional expression (exp one or more with conditional ops) - if/else if () else ex: scanf("%d", &a); b = 10; if (a < 10) printf("a is less than 10\n"); else printf("a is greater than 10\n"); - ex: if (a < 10 || a > 30) if (a < 10) printf("lt 10\n"); else printf("gt 30\n"); - OR else if, when you want more than branch if (a < 10) { } else if (a > 30) { } else { } - switch/case - another way to do multiple branches over one variable scanf("%c", &mychar); switch(mychar) { case 'a': ... break; case 'b': ... break; case 'c': case 'd': ... break; default: ... break; } Loops -------- - powerful use of computers is their ability to repeat processes quickly: loops - do/while/for do ... while( ); while( ) ... for (expr1; expr2; expr3) statement ex: for (i=0; i<100; i++) printf("foo\n"); - break and continue - break in loops or switch - break gets you out of loops without complicating conditional expression - continue useful for skipping code without introducing more conditionals - nested loops - printing/indexing/inputting ddim arrays int manyints[20][10] for (i=0; i < 20; i++) { for (j=0; j < 10; j++) { manyints[i][j] = 0; } }