1 / 19

CSCI 130

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

zazu
Download Presentation

CSCI 130

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 130 while and do…while Chapter 7 - B

  2. 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

  3. 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

  4. Sample While Statement • int main ( ) { • int count; • count = 1; • while (count <=20) { • printf (“\n%d”, count); • count ++; • } • return 0; • }

  5. 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++; • }

  6. 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

  7. 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; • } • }

  8. 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

  9. 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); }

  10. 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

  11. 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 ++; }

  12. 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

  13. 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

  14. 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); }

  15. 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

  16. 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); }

  17. 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

  18. Example of while within for int count1; int count2; for (count1 = 1; count1 < 10; count1++) { count2 = 0; while (count2 <3) { printf (“%d”, count2); count2 ++; } }

  19. Illegal nested loops int count1, count2; for (count1 = 1; count1 < 10; count1 ++) { count2 = 1; do { printf (“%d”, count2); count2 ++; } } while (count2 < 3);

More Related