ATS 315: Working With Loops in C Loops in C ATS 315.
-
Upload
piers-blankenship -
Category
Documents
-
view
230 -
download
0
Transcript of ATS 315: Working With Loops in C Loops in C ATS 315.
![Page 1: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/1.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
Loops in C
ATS 315
![Page 2: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/2.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
Loops
• Are used to perform some action multiple times.
• Are crucial to most C programs that are useful.
![Page 3: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/3.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
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
![Page 4: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/4.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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.
![Page 5: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/5.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“for” loop syntax
• Sample loop that will run 5 times.
main () {
int i;
for(i=0;i<5;i++) {
printf (“%d\n”,i);
}
}
![Page 6: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/6.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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);
}
}
![Page 7: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/7.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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);
}
}
![Page 8: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/8.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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);
}
}
![Page 9: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/9.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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);
}
}
![Page 10: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/10.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“i++” ?!?!?!?!!!!???
• i++ is a shortcut for “i=i+1”
• To increase in a different “step”, just be more explicit about it:
• i=i+2
![Page 11: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/11.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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);
}
}
![Page 12: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/12.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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);
}
}
![Page 13: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/13.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“for” loop syntax
• Traditional format:– There are three “arguments” in the for loop:
• for(i=0;i<n;i++) {
• }
![Page 14: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/14.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“for” loop syntax
• Traditional format:– 1. The starting value of the loop—usually
zero.
• for(i=0;i<n;i++) {
• }
![Page 15: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/15.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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++) {
• }
![Page 16: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/16.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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++) {
• }
![Page 17: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/17.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
Floating Point Loops
• for(pressure=1000.0;pressure<1024.0; pressure=pressure+4.0) {
• }
![Page 18: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/18.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
Nested Loops
• One thing that could be inside of a loop is another loop.
• Excellent for working with grids of data…
![Page 19: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/19.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
Nested Loops
for(i=0;i<5;i++) {for(j=0;j<3;j++) {
printf (“%d %d\n”,i,j);
}}
0 00 10 21 01 11 22 02 12 23 03 13 24 04 14 2
![Page 20: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/20.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
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.
![Page 21: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/21.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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”);
}
![Page 22: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/22.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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”);
}
![Page 23: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/23.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
“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”);
}
![Page 24: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/24.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
Uses of “while”
• “while” loops are generally used to trap errors.
![Page 25: ATS 315: Working With Loops in C Loops in C ATS 315.](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bfc61a28abf838ca718b/html5/thumbnails/25.jpg)
ATS 3
15: W
orkin
g W
ith Lo
ops in
C
Your Assignment
• Make a table of wind chill temperatures!
1. Prompt the user for an air temperature
2. Prompt the user for a lowest wind speed
3. Prompt the user for a highest wind speed
4. Prompt the user for an interval