CSC
LEC20080919

# Not frequently used controlling expression following

• Notes
• 25

This preview shows pages 9–17. Sign up to view the full content.

Not frequently used Controlling expression following switch must evaluate to integer (or char). Example (modify a part of read.c ): /* classify n into four categories */ int classify(int n) { if (n < 0) CSC180 Fall 2008, University of Toronto 8

This preview has intentionally blurred sections. Sign up to view the full version.

return -1; if (n == 0) return 0; if (n > 0 && n <= 1000) return 1; if (n > 1000) return 2; } /* in main */ switch (classify(n)) { case 0: read_ch2_twice(); break; case 1: read_ch2_once(); break; case 2: ignore_ch2(); break; default: printf(‘‘Invalid input\n’’); break; } CSC180 Fall 2008, University of Toronto 9
Exercise: if we redefine classify() , change the switch statement accordingly: char classify(int n) { if (n < 0) return ’E’; /* error */ if (n == 0) return ’L’; /* low */ if (n > 0 && n <= 1000) return ’M’; /* medium */ if (n > 1000) return ’H’; /* high */ } Group related case labels: switch(c) { case ’0’: case ’1’: case ’2’: case ’3’: case ’4’: case ’5’: case ’6’: case ’7’: case ’8’: case ’9’: CSC180 Fall 2008, University of Toronto 10

This preview has intentionally blurred sections. Sign up to view the full version.

printf(‘‘digit\n’’); break; case ’ ’: case ’\n’: case ’\t’: printf(‘‘white space\n’’); break; default: printf(‘‘other\n’’); break; } The break statement let us exit immediately from the switch statement. Do not forget adding break for each case. Fall through if break is omitted: switch(grade) { CSC180 Fall 2008, University of Toronto 11
case 4: case 3: case 2: case 1: npass++; /* FALL THROUGH */ case 0: total++; break; } CSC180 Fall 2008, University of Toronto 12

This preview has intentionally blurred sections. Sign up to view the full version.

for loop Generate an arbitrarily long password: #include <stdio.h> #include <stdlib.h> #include <time.h> #define NUM 4 /* generate password given length len */ void pwd(int len) { int i, j; int pool[][2] = {{ ’*’, 4 }, { ’0’, 10 }, { ’a’, 26 }, { ’A’, 26 }}; srand(time(NULL)); /* set a seed */ CSC180 Fall 2008, University of Toronto 13
for (i = 0; i < len; i++) { j = rand() % NUM; putchar(pool[j][0] + rand() % pool[j][1]); } putchar(’\n’); } int main(int argc, char *argv[]) { pwd(atoi(argv[1])); /* convert string to integer */ return 0; } Main idea: each time I randomly select a character from a character pool. I select len times, where len is the length of password. for (i = 0; i < len; i++) has three parts, separated by two semicolons. The first part is initialization; here we set i to 0. The second part is conditional test; here we test if i is less than len . If no, CSC180 Fall 2008, University of Toronto 14

This preview has intentionally blurred sections. Sign up to view the full version.

the loop terminates; otherwise, the loop body is executed. In the last part we increment i by 1. If the second part is omitted, the
This is the end of the preview. Sign up to access the rest of the document.
• Fall '01
• na

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern