EEE 121 Structured Programming Language: Instructor: Moonmoon Shanta Fall 2013
EEE 121 Structured Programming Language: Instructor: Moonmoon Shanta Fall 2013
Instructor: Moonmoon Shanta Fall 2013 Lecture 4: Conditional statements: switch Loops: while, do-while
12/29/2013
int n;
printf("Enter a number 1 to 10: "); scanf("%d",&n); if (n > 10) printf(Thats not in range!"); else if (n < 1) printf(Thats not in range!"); else printf("Good job!");
12/29/2013
12/29/2013
12/29/2013
12/29/2013
Example solution
12/29/2013
If temp is 90 or higher, print Its too hot! If temp is 32 or lower, print Its freezing! In all other cases, print Its okay
int main() { double temp; printf(Enter temperature: ); scanf(%lf, &temp); if (temp >= 90) printf(Its too hot!\n); else if (temp <= 32) printf(Its too cold!\n); else printf(Its okay\n); return 0; }
12/29/2013
switch statements
Nesting several if/else if statements can get tedious If each condition is simply checking equality of same variable or expression, can use switch
12/29/2013
12/29/2013
switch/case statement
If <expression> == <value1>, execute <statements> in that case If <expression> == <value2>, execute <statements> in that case
If <expression> does not equal any of the values, go to default case (if present)
12/29/2013
Each case is just a starting pointswitch does not automatically skip other cases! Example: switch (x) { case 0: x = 3; case 1: x = x * 4; default: x = x 1; } If x == 0:
12/29/2013
You may not always want to use breakwill see examples later
Rewriting previous example: switch (x) { case 0: x = 3; break; case 1: x = x * 4; break; default: x = x 1; }
12/29/2013
12/29/2013
12/29/2013
What does the program on the previous slides print if the user enters:
A B+ c X
12/29/2013
Example solution
What does the program on the previous slides print if the user enters:
You are excellent Only first character is readB You are good This program is case-sensitiveC and c are two different characters!
B+
You are incapable of reading directions No case for Xgoes to default case You are incapable of reading directions
Structured Programming Language: Lecture 5
12/29/2013
It is capable of jumping to any statement in a function, provided that the statement has a label. Useful when jumping out of nested loops as break only jumps out of a single loop.
12/29/2013
19
Example: goto
for (d = 2; d < n; d++) if(n % d == 0) goto done; .... .... done: if(d < n) printf (%d is divisible by %d\n, n, d); else printf (%d is prime\n, n);
12/29/2013
20
while loops
Previous program does same thing 11 times Repetitive code can be captured in a loop
Much less code to do same amount of work Simplest form: while loop while (<expression>) <statement> loop body
Loop body must therefore change expression If multiple lines, need { } to denote block
12/29/2013
7 8 9
12/29/2013
(no output)
12/29/2013
// Compute and display the squares of numbers 0 to 10 i = 0; // Initialize i while (i <= 10) { // Loop until i > 10 iSquared = i * i; printf("%2d%10d\n", i, iSquared); i = i + 1; // Increment i } return 0; }
12/29/2013
while (gradeCount < numGrades) { scanf("%lf", &grade); // Read grade gradeSum = gradeSum + grade; // Add to sum gradeCount = gradeCount + 1; // Inc. count }
12/29/2013
May be predetermined (i.e., run program until user enters q for quit) Run until invalid value entered In file input, will often run until end of file
// Prompt for and read first grade printf("Enter grade: "); scanf("%lf", &grade);
/* Continue reading/accumulating grades until invalid value entered */ while ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Increment grade count printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read next grade }
12/29/2013
do-while loops
);
A = 0?
TRUE
A = 0?
FALSE
Loop body
All loops characterized by conditional test, backwards arrows indicating repetition of code
Structured Programming Language: Lecture 5
12/29/2013
OUTPUT: 789
12/29/2013
OUTPUT: 7
12/29/2013
// Prompt for and read first grade printf("Enter grade: "); scanf("%lf", &grade); /* Continue reading/accumulating grades until invalid value entered */ while ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Increment grade count printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read next grade }
12/29/2013
Rewrite grade average program to ensure at least one grade is read Change core of program (shown previously):
/* Prompt for and read grades until invalid value entered */ do { printf("Enter grade: "); // Prompt for and scanf("%lf", &grade); // read grade
if ((grade >= 0.0) && (grade <= 100.0)) { gradeSum = gradeSum + grade; // Accumulate grade gradeCount = gradeCount + 1; // Inc. grade count } } while ((grade >= 0.0) && (grade <= 100.0));
12/29/2013