Download presentation
Presentation is loading. Please wait.
Published byHerbert Gilmore Modified over 9 years ago
1
IPC144 - LOOPS while - do while - for
2
Review - SWITCH The switch statement can also be used to select code to be executed and code to be skipped. It does not handle ranges. It does not loop. The switch statement can also be used to select code to be executed and code to be skipped. It does not handle ranges. It does not loop. switch ( expression )/* expression must be integer or single character */ { case (constant): statement(s); break; default: statement(s); /* optional */ }
3
SWITCH used in Banking Pgm Consider the situation of opening a new bank account. Consider the situation of opening a new bank account. char account_type; printf(“ Type of account? c … chequing\n”); printf(“ s … savings\n”); scanf(“ %c”, &account_type); switch (account_type) { case ‘c’: int_rate = 0.005; gift = 50.00; break; case ‘s’: int_rate = 0.025; gift = 100.00; break; default: int_rate = 0.0; gift = 50.00; default: int_rate = 0.0; gift = 50.00;}
4
SWITCH – Two cases with the same outcome - We can join cases together if the code block is the same. Notice that case ‘c’ chequing and case ‘s’ saving have identical code blocks because there is no break statement. The switch statement could be written as: switch (account_type) { case ‘c’: case ‘s’: int_rate = 0.005; gift = 50.00; break;default: int_rate = 0.0; gift = 25.00; }
5
switch – Prog Question 6-4 Use the switch statement to program the following: SmartKeySystems wants to hire a Systems Analyst. They give each resume received a value from 1.5 to 3.5 for education and another value (1.5-3.5) for work experience. The numbers are added to get a total score. The score is rounded to the nearest integer. Here is what they do: Note: switch cannot be used for doubles – only int or char! 3Nothing 4-5 Keep the resume on file 6 Send a thank you letter 7 Schedule an interview
6
switch Prog Ques 4 –ans #include #include main() main() { double total,educ,work; double total,educ,work; int i; int i; printf("Enter the score for education: "); scanf("%lf",&educ); printf("Enter the score for education: "); scanf("%lf",&educ); printf("Enter the score for work experience: "); scanf("%lf",&work); printf("Enter the score for work experience: "); scanf("%lf",&work); total = educ+work; total = educ+work; total = total +.5; /* prepare to round */ total = total +.5; /* prepare to round */ i = total; i = total; switch (i) switch (i) { { case 3: printf("Do not send a response\n"; break; case 3: printf("Do not send a response\n"; break; case 4: case 4: case 5: printf("Keep on file\n"; break; case 5: printf("Keep on file\n"; break; case 6: printf("Send a thank you letter\n", break; case 6: printf("Send a thank you letter\n", break; case 7: printf("Schedule Interview\n"); break; case 7: printf("Schedule Interview\n"); break; default: default: } } }
7
Summary of steps taken by the switch statement. 1) evaluate the expression. 1) evaluate the expression. 2) search to find matching case. 2) search to find matching case. 3) if a match is found, execute the instructions until… 3) if a match is found, execute the instructions until… - a break statement is reached - a break statement is reached - or a default statement is reached - or a default statement is reached - or a } brace ending the switch statement is reached. - or a } brace ending the switch statement is reached. At that point go to the statement following the switch statement. At that point go to the statement following the switch statement. 4) If there is no match and there is a default code block, perform the default code block. 4) If there is no match and there is a default code block, perform the default code block. 5) if there is no match and there is no default code block, exit the switch statement and continue program execution with the first statement following the switch statement. 5) if there is no match and there is no default code block, exit the switch statement and continue program execution with the first statement following the switch statement.
8
Comments on switch Notes: 1) The default section is optional. 2) break is needed to prevent one case from running into an other. 3) no code whatsoever will be performed if the is no match and there is no default section. 4)Discussion Question: Is switch preferable to an if - else if - else if - else? Readability: Switch / Case is usually better. Readability: Switch / Case is usually better. Speed: Switch is faster. Speed: Switch is faster. Flexibility: If / Else / Else If using ANDs/ ORs is usually better. Flexibility: If / Else / Else If using ANDs/ ORs is usually better.
9
Walkthrough 7-1 What is the expected output of the following pgm? int x=5; y=10; z=15; switch (z){ case 2: printf(“case 2 is executing\n”); break case 2: printf(“case 2 is executing\n”); break case 4: printf(“case 4 is executing\n”); case 4: printf(“case 4 is executing\n”); case 5: printf(“case 5 is executing\n”); break; case 5: printf(“case 5 is executing\n”); break; default: printf(“The default is executing\n”); default: printf(“The default is executing\n”);}
10
Walkthrough 7-2 What is the expected output of the following pgm? int x=5; y=10; z=15; switch (y/x){ case 2: printf(“case 2 is executing\n”); break; case 2: printf(“case 2 is executing\n”); break; case 4: printf(“case 4 is executing\n”); case 4: printf(“case 4 is executing\n”); case 5: printf(“case 5 is executing\n”); break; case 5: printf(“case 5 is executing\n”); break; default: printf(“The default is executing\n”); default: printf(“The default is executing\n”);}
11
Walkthrough 7-3 What is the expected output of the following pgm? int x=5; y=10; z=15; switch (x-1){ case 2: printf(“case 2 is executing\n”); break; case 2: printf(“case 2 is executing\n”); break; case 4: printf(“case 4 is executing\n”); case 4: printf(“case 4 is executing\n”); case 5: printf(“case 5 is executing\n”); break; case 5: printf(“case 5 is executing\n”); break; default: printf(“The default is executing\n”); }
12
Prog Question 7-5 The cost of 10 corn depends on the month. In July (month 7) it is $3.75 In August (month 8) it is $4.25 The user enters the month number. We can can use switch or if. Which one is better?
13
Prog Question 7-5- Ans Switch is better because it will run faster. In a simple case like this, it is very readable. printf(“Enter the month number:”); scanf(“%d”, &month); switch (month){ case 7: price = 3.75; break; case 7: price = 3.75; break; case 8: price = 4.25; case 8: price = 4.25;}
14
Prog Question 7-6 The cost of 10 corn depends on the month and the grade. Use a switch to select the correct month and within each case, use an if to select the right price. The user enters the month number and the grade number. MonthNumber Grade A =1 =1 Grade B =2 =2 7(July)$3.50$2.85 8(August)$4.00$3.05
15
Prog Question 7-6 - Ans printf(“Enter the month and grade numbers separated by a space:”); scanf(“%d %d”, &month, &grade); switch (month){ case 7: case 7: if (grade == 1) price = 3.50; else price = 2.85; break; case 8: if (grade == 1) price = 4.00; else else price = 3.05; }
16
While To perform some code repeatedly Write a program to print odd numbers between 50 and 100. The program prints the number, the square of the number and the cube of the number. k=51;while(k<100){ printf(“%d square: %d cube: %d\n”, k, k*k, k*k*k ); k = k + 2; } printf (“ All done\n”);
17
Prog 7-7 – Pie Eating While – Perform statements repeatedly A person is practicing for a pie-eating contest. They start with a pie of 750 grams. Each day they eat 10% more than the previous day. How many days will it take to get to the goal of 2 kilograms?
18
Prog 7-7 – Pie Eating - Ans While – Perform statements repeatedly #include #include main() main(){ int days = 0; int days = 0; double grams = 750; double grams = 750; while(grams < 2000){ while(grams < 2000){ grams = grams * 1.1; /* to add 10 % */ grams = grams * 1.1; /* to add 10 % */ days = days + 1; days = days + 1; printf("On day %d you ate %.2lf grams\n",days+1,grams); printf("On day %d you ate %.2lf grams\n",days+1,grams); } printf("It took you %d days to get to your goal\n", days+1); printf("It took you %d days to get to your goal\n", days+1);}
19
While – Walkthrough 4 What is the expected output? int i=3,k=25; double x = 0.2; while ( k/i > 0){ x=x+0.3;k=k-4;i=i+x-1; printf(“The current value of k/i is: %d \n”, k/i); }}
20
While – Walkthrough 4-Ans What is the expected output? int i=3,k=25; double x = 0.2; while ( k/i > 0){ x=x+0.3;k=k-4;i=i+x-1; printf(“The current value of k/i is: %d \n”, k/i); }} The current value of k/i is: 10 The current value of k/i is: 10 The current value of k/i is: 17 The current value of k/i is: 17 The current value of k/i is: 13 The current value of k/i is: 13 The current value of k/i is: 9 The current value of k/i is: 9 The current value of k/i is: 5 The current value of k/i is: 5 The current value of k/i is: 0 The current value of k/i is: 0
21
Validating Ranges Question 6-2 - Robin Hood Summer Camps swimming instructors must be at least 15 years old and not more than 39 years old. Finish the code… printf(“please enter the age”); scanf(“%d”, &age); while() {}
22
Do / while Do-While Statement (Post-test Loop) Do-While Statement (Post-test Loop) Often want to execute the statement(s) before testing the condition, even for the first time; i.e. the block of code in the do- while loop is always executed unconditionally the first time Often want to execute the statement(s) before testing the condition, even for the first time; i.e. the block of code in the do- while loop is always executed unconditionally the first time Often used for validating input Often used for validating input Do - While statement syntax: Do - While statement syntax:do{statement(s); }while (condition); /* loop is repeated as long as condition is true (i.e. until condition is false) */ Note: Semi-colon at end of while
23
Do /While The difference is tht the do will be executed at least one time k=1051;do{ printf(“%d square: %d cube: %d\n”, k, k*k, k*k*k ); k = k + 2; }while(k<100);=========================================================k=1051;while(k<100){ printf(“%d square: %d cube: %d\n”, k, k*k, k*k*k ); k = k + 2; }========================================================== In this case using “do” will print one line, but using while, no lines are printed.
24
Validate ranges with do/while Validate with do/while … do{ if(rate 22.00){ printf(“Error: pay rate is out of range!”); printf(“please enter the hourly rate: ”); scanf(“%lf”, &rate); }while(rate 22.00);
25
Validating A List of Values Campers at Robin Hood receive a swimming grade of A, B,or, C. All other codes are incorrect. This is handled with an AND and !=. printf(‘Enter grade: ‘); printf(‘Enter grade: ‘); scanf( “ %c”, &grade); scanf( “ %c”, &grade); while(grade != ‘A’ && grade != ‘B’ && grade != ‘C’ ) while(grade != ‘A’ && grade != ‘B’ && grade != ‘C’ ) {printf(“Error: Grade must be A, B, or, C !\n” printf(‘Enter grade: ‘); scanf( “ %c”, &grade); }
26
Validating Input matches a value in a list of values: Question 6-3 Apples are graded as ‘A’, ‘J’, or, ‘X’ printf(“Enter grade: “); scanf(“ %c”, &grade); while( ) {}
27
Compound Conditions Prog Question 6-3-Ans char categ; printf(“Enter the apples category: “); scanf(“% c”, &categ); if (categ != ‘A’ && categ != ‘J’ && categ != ‘X’){ printf(“Error: Category must be A, J, or, X!\n”); printf(“Enter the apples category: “); scanf(“%d”, &categ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.