200 likes | 328 Views
CSCI 130. while and do…while Chapter 7 - B. The while statement. Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2; … statement n; } Not necessarily executed
E N D
CSCI 130 while and do…while Chapter 7 - B
The while statement Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2; … statement n; } Not necessarily executed DOWHILE structure from CIS 101
The While Statement 1) Condition evaluated 2) If condition evaluates to false: while statement terminates 3)If condition evaluates to true: statements are executed 4)Return to step 1
Sample While Statement • int main ( ) { • int count; • count = 1; • while (count <=20) { • printf (“\n%d”, count); • count ++; • } • return 0; • }
Compare the for Loop & while • int count; • for (count=1; count <=20; count++) { • printf (“\n%d”, count); • } • ---------------------------------------------------- • int count = 1; • while (count <=20) { • printf (“\n%d”, count); • count++; • }
Pseudocode for While Statement Write pseudocode to print odd number less than 20 using while loop • Count =1 • DOWHILE Count <20 • PRINT Count • Count = Count + 2 • ENDO
Writing While Statement Write the C code to print the odd numbers less than 20 using a while loop • void main () { • int count = 1; • while (count <20) { • print f (“ln%d”, count); • count + = 2; • } • }
Another while Example Write psuedocode to allow the user to enter test scores. When they are done entering in scores then will enter 0. At that point, print out the average score. Count = 0 Total = 0 Get Score DOWHILE (score not = 0) Total = Total + score count = count +1 Get Score ENDO Avg = Total / count OUTPUT Avg
Corresponding C Code void main () { int count = 0; int total = 0; int score = 0; float average = 0; printf (“Enter a test score (0 to finish)”); printf (“The average will be calculated”); scanf (“%d”, &score); while (score != 0) { total += score; printf (“Enter next score”); scanf (“%d”, &score); count += 1; } average = total / count; printf (“The average score is %f”, average); }
Comparing loops • Tasks accomplished with for loop can be accomplished with while loop. • For loop has initialization condition, & increment in one spot • increased readability • generally easier to work • If # repetitions cannot be determined before iterations begin, must use while
Nested while loops ctr = 0; while (ctr < 5) { nbr = 0; while (nbr < 1 || nbr > 10) { printf (“enter a number between 1 & 10); scanf (“%d”, & nbr); } array [ctr] = nbr; ctr ++; }
The do … while loop Executes a block of code as long as a condition is true Always executes at least once general form: do { statements; } while (condition); Repeat … Until from CIS 101
Execution of do … while loop • 1.) Statements in loop executed • 2.) If condition evaluates to false loop terminates • 3.) If condition evaluates to true - return to step 1
Sample do … while loop void main ( ) { int choice = 0; do { printf (“\n 1 - Add a record”); printf (“\n2 - Delete a record”); printf (“\n3 - Change a record”); printf (“\n\n Enter a selection”); scanf(“%d”, &choice); } while (choice < 1 || choice > 3); printf (“You chose %d”, choice); }
Pseudocode for do … while • Write pseudocode to allow the user to input test scores (there will be at least 3 scores.) The program will calculate & print the average. Stop looping when the user enters 0. • count = 0 • total = 0 • GET score • REPEAT • total = total + score • count = count +1 • GET score • UNTIL score = 0 • average = total / count • PRINT average
Corresponding C Code void main ( ) { float average = 0 int count = 0 int score = 0 int total = 0 printf (“Enter a test score”); scanf (“%d”, &score); do { total + = score; count = 1; printf (“Enter next score (0 to finish)”); scanf (“%d”, &score); } while (score != 0) average = total/count printf (“The average is %f”, average); }
Nested loops • Any loop can be nested within any other loops • for within while • while within for • for within do … while • do … while within for, within while, • etc. • Loops must be completely nested within each other
Example of while within for int count1; int count2; for (count1 = 1; count1 < 10; count1++) { count2 = 0; while (count2 <3) { printf (“%d”, count2); count2 ++; } }
Illegal nested loops int count1, count2; for (count1 = 1; count1 < 10; count1 ++) { count2 = 1; do { printf (“%d”, count2); count2 ++; } } while (count2 < 3);