Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger & Dr. Lin Li Spring 2019 Lecture 10: Switch statements; while loops
2
Announcements/reminders
Text exercises due 3 days after each lecture Lecture Tuesday, not Monday next week Program 1 resubmissions due Tuesday, 2/19 Program 3 due Monday, 2/25 Exam 1 on Friday, 2/22 Allowed one double-sided 8.5” x 11” note sheet No other notes, no electronic devices Old exams at link on course home page ( 4/24/2019 ECE Application Programming: Lecture 9
3
ECE Application Programming: Lecture 9
Today’s lecture Review Range checking Switch statements Switch example While/do-while loops 4/24/2019 ECE Application Programming: Lecture 9
4
Review: switch statements
When checking multiple exact values for expression, more sense to use switch statement switch (<expr>) { case <val1> : ... break; case <val2> : default: } break allows you to exit switch statement after completing code Otherwise, program will continue to run through cases until finding break default covers any values without specific case 4/24/2019 ECE Application Programming: Exam 1 Preview
5
ECE Application Programming: Lecture 9
Switch example int x; … switch (x) { case 0: y = y + 1; break; case 1: case 2: y = y – 1; default: y = 0; } 4/24/2019 ECE Application Programming: Lecture 9
6
Switch statements and break
Use break to exit at end of case You may not always want to use break—will see examples later Rewriting previous example: switch (x) { case 0: x = 3; break; case 1: x = x * 4; default: x = x – 1; } 4/24/2019 ECE Application Programming: Lecture 9
7
switch/case statement - example
#include <stdio.h> int main() { char grd; printf("Enter Letter Grade: "); scanf("%c",&grd); printf(“You are "); // continued next slide 4/24/2019 ECE Application Programming: Lecture 9
8
switch/case statement - example
switch (grd) { case 'A' : printf("excellent"); break; case 'B' : printf("good"); break; case 'C' : printf("average"); break; case 'D' : printf("poor"); break; case 'F' : printf("failing"); break; default : printf(“incapable of reading directions"); break; } return 0; } 4/24/2019 ECE Application Programming: Lecture 9
9
Example: switch statement
What does the program on the previous slides print if the user enters: A B+ c X Recognize, of course, that it always prints: Enter Letter Grade: 4/24/2019 ECE Application Programming: Lecture 9
10
ECE Application Programming: Lecture 9
Example solution What does the program on the previous slides print if the user enters: A You are excellent B+ Only first character is read—’B’ You are good c This program is case-sensitive—’C’ and ‘c’ are two different characters! Will go to default case You are incapable of reading directions X No case for ‘X’—goes to default case 4/24/2019 ECE Application Programming: Lecture 9
11
switch/case statement - Alt example
switch (grd) { case 'A' : case 'a': case 'B' : case 'b': printf("doing very well"); break; case 'C' : case 'c': case 'D' : case 'd': printf("not doing too well"); break; case 'F' : case ‘f': printf("failing"); break; default : printf("incapable of reading directions"); break; } return 0; } 4/24/2019 ECE Application Programming: Lecture 9
12
ECE Application Programming: Lecture 9
Repetition Say we have a program to print squares of numbers between 0 and 10: void main() { int i; // Number to square int iSquared; // Square of the number printf(" i i^2\n"); // Column headings // Compute and display the squares of numbers 0 through 10 i = 0; iSquared = i * i; printf("%2d%10d\n", i, iSquared); ... // Code for i = 1, 2, ... 8, 9 i = 10; } 4/24/2019 ECE Application Programming: Lecture 9
13
ECE Application Programming: Lecture 9
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 will repeat as long as <expression> is true Loop body must therefore change expression <statement> may be one or more lines If multiple lines, need { } to denote block 4/24/2019 ECE Application Programming: Lecture 9
14
ECE Application Programming: Lecture 9
4/24/2019 ECE Application Programming: Lecture 9
15
ECE Application Programming: Lecture 9
while loops - example x = 7; while ( x < 10 ) { printf("%d ",x); x = x + 1; } OUTPUT: 7 8 9 4/24/2019 ECE Application Programming: Lecture 9
16
ECE Application Programming: Lecture 9
while loops - example x = 7; while ( x < 3 ) { printf("%d ",x); x = x + 1; } OUTPUT: (no output) Possible to have while loop body that never executes! 4/24/2019 ECE Application Programming: Lecture 9
17
Repetition with while loop
Rewriting previous program with loop int main() { int i; // Number to square int iSquared; // Square of the number printf(" i i^2\n"); // Column headings // 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; 4/24/2019 ECE Application Programming: Lecture 9
18
Application: sentinel value
Common to read input until a certain value(sentinel) is entered 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 See while3.c for an example (on website) Refined version of average grade program Core of program: // 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 } 4/24/2019 ECE Application Programming: Lecture 11
19
ECE Application Programming: Lecture 11
do-while loops while loop is pre-tested Check condition at start; if false, don’t enter loop To guarantee at least one iteration, use post-tested loop: do-while Checks condition at end of loop do { <statements> } while ( <expression> ); Don’t forget semicolon! 4/24/2019 ECE Application Programming: Lecture 11
20
comparison while vs do-while
x = 7; do { printf("%d ",x); x = x + 1; } while ( x < 10 ); OUTPUT: 7 8 9 x = 7; while ( x < 10 ) { printf("%d",x); x = x + 1; } OUTPUT: 7 8 9 4/24/2019 ECE Application Programming: Lecture 11
21
comparison while vs do-while
x = 7; do { printf("%d",x); x = x + 1; } while ( x < 3 ); OUTPUT: 7 x = 7; while ( x < 3 ) { printf("%d",x); x = x + 1; } OUTPUT: (no output) 4/24/2019 ECE Application Programming: Lecture 11
22
ECE Application Programming: Lecture 9
Final notes Next time Loop examples Reminders: Text exercises due 3 days after each lecture Lecture Tuesday, not Monday next week Program 1 resubmissions due Tuesday, 2/19 Program 3 due Monday, 2/25 Exam 1 on Friday, 2/22 Allowed one double-sided 8.5” x 11” note sheet No other notes, no electronic devices Old exams at link on course home page ( 4/24/2019 ECE Application Programming: Lecture 9
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.