1 / 25

Loops in C

Loops in C. ATS 315. Loops. Are used to perform some action multiple times. Are crucial to most C programs that are useful. Two Main Kinds of Loops. Loops that run a specific number of times. “for” loops Loops that run a flexible number of times, if at all. “while” loops. “for” loops.

adolfo
Télécharger la présentation

Loops in C

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. Loops in C ATS 315

  2. Loops • Are used to perform some action multiple times. • Are crucial to most C programs that are useful.

  3. Two Main Kinds of Loops • Loops that run a specific number of times. • “for” loops • Loops that run a flexible number of times, if at all. • “while” loops

  4. “for” loops • Require a “counter” that keeps track of how many times the loop has run. • Typically this is an integer, but it doesn’t have to be.

  5. “for” loop syntax • Sample loop that will run 5 times. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); } }

  6. “for” loop syntax • On the first pass through the loop, i=0. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); } }

  7. “for” loop syntax • “What the loop does” is inside a set of curly braces. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); } }

  8. “for” loop syntax • Traditionally we indent the stuff inside a loop a little farther. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); } }

  9. “for” loop syntax • After each pass through the loop, the “loop control variable” will be changed as shown here. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); } }

  10. “i++” ?!?!?!?!!!!??? • i++ is a shortcut for “i=i+1” • To increase in a different “step”, just be more explicit about it: • i=i+2

  11. “for” loop syntax • As long as the loop control variable passes the “condition”, it will continue to loop. main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); } }

  12. “for” loop syntax • This loop will run 5 times: • i=0,1,2,3,4 main () { int i; for(i=0;i<5;i++) { printf (“%d\n”,i); } }

  13. “for” loop syntax • Traditional format: • There are three “arguments” in the for loop: • for(i=0;i<n;i++) { • }

  14. “for” loop syntax • Traditional format: • 1. The starting value of the loop—usually zero. • for(i=0;i<n;i++) { • }

  15. “for” loop syntax • Traditional format: • 2. The “condition” for the loop—it will run as long as this is true. • for(i=0;i<n;i++) { • }

  16. “for” loop syntax • Traditional format: • 3. The “increment” of the loop—how to modify the value of the loop control variable. • for(i=0;i<n;i++) { • }

  17. Floating Point Loops • for(pressure=1000.0;pressure<1024.0; pressure=pressure+4.0) { • }

  18. Nested Loops • One thing that could be inside of a loop is another loop. • Excellent for working with grids of data…

  19. for(i=0;i<5;i++) { for(j=0;j<3;j++) { printf (“%d %d\n”,i,j); } } 0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2 3 0 3 1 3 2 4 0 4 1 4 2 Nested Loops

  20. The “while” Loop • Is used when you don’t know in advance how many times the loop should run… it just needs to run “while” something is true.

  21. “while” loop syntax • Loop control variable needs to be “initialized”. tempF = 0.; while (tempF > -98.0) { printf (“Enter a temp in F to convert to C.\n”); printf (“Or enter -99 to end.\n”); scanf(“%f”,&tempF); tempC = (tempF-32.)*(5./9.); printf (“The temperature is %4.1fC.\n\n”); }

  22. “while” loop syntax • Loop will repeat “while” condition is true. tempF = 0.; while (tempF > -98.0) { printf (“Enter a temp in F to convert to C.\n”); printf (“Or enter -99 to end.\n”); scanf(“%f”,&tempF); tempC = (tempF-32.)*(5./9.); printf (“The temperature is %4.1fC.\n\n”); }

  23. “while” loop syntax • Loops are generally indented. tempF = 0.; while (tempF > -98.0) { printf (“Enter a temp in F to convert to C.\n”); printf (“Or enter -99 to end.\n”); scanf(“%f”,&tempF); tempC = (tempF-32.)*(5./9.); printf (“The temperature is %4.1fC.\n\n”); }

  24. Uses of “while” • “while” loops are generally used to trap errors.

  25. Your Assignment • Make a table of wind chill temperatures! • Prompt the user for an air temperature • Prompt the user for a lowest wind speed • Prompt the user for a highest wind speed • Prompt the user for an interval

More Related