1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while...
-
Upload
theodora-burns -
Category
Documents
-
view
224 -
download
0
Transcript of 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while...
![Page 1: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/1.jpg)
1
Loops II
![Page 2: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/2.jpg)
2
Recall the while Loop
int sum = 0;int i = 1;
.../* Sum the integers 1 to 10 */while ( i <= 10){
sum += i;i++;
}
Initialization
Condition
Update
“while” loops typically follow a predictable pattern.
There is a “control variable” such as i in the loop below.
![Page 3: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/3.jpg)
3
The for Loop
A more compact way to express the loop control information.
All control information in a single line at the top of the loop.
for (i = 1; i <= 10; i++){sum += i;
} Most widely used looping construct
in C
Initialization Condition Update
Loop body
![Page 4: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/4.jpg)
4
The for Loop
for (i = 1; i <= 10; i++){sum += i;
}
Initialization is done only once, before the loop body is executed the first time.
Condition is tested before loop body is executed.
(Same as a “while” loop. Loop body is executed if condition is true)
Update is done after the loop body is executed each time, regardless of the condition.
Repeat the loop body if condition is true. (Like “while”.)
Continue after loop body if condition is not true.
![Page 5: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/5.jpg)
5
The for Loop
for (i = 1; i <= 10; i++){sum += i;
}
Things to notice
The three sections of the control unit are separated by semicolons.
Loop body is a block of code, delimited by curly brackets.Same as for while or if.
No semicolon!
![Page 6: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/6.jpg)
6
The for Loop
It is legal to write a single statement as the loop body:
for (i = 1; i <= 10; i++) sum += i;
or even:for (i = 1; i <= 10; i++) sum += i;
Don’t! At least not for this course. (Even though our book
approves.)
Same reasons as for “if” and “while”.
![Page 7: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/7.jpg)
7
The for Loop
Caution: Watch out for this mistake:
for (i = 1, i <= 10, i++){sum += i;
}
Should be semicolons!
![Page 8: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/8.jpg)
8
The for Loop
Programming Style
for (i = 1; i <= 10; i++){ sum += i;}
Align curly brackets with the “for”.
(Differs from book.)
Indent the code four spaces.
The usual “code block”
![Page 9: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/9.jpg)
9
The for Loop
Legally you can modify the loop control variable inside the loop
for (i = 1; i <= 10; i++){ i += 3; sum += i;}
This is always a bad idea!Treat the control variable as a read-only
variable inside the loop.
![Page 10: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/10.jpg)
10
Example: squares.c
#include <stdio.h>
int main()
{
int i = 0;
printf ("This program outputs a table of squares\n");
for (i = 0; i <= 10; i++)
{
printf ("%3d %6d\n", i, i*i);
}
getchar(); // Keep window open
getchar();
return 0;
}
![Page 11: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/11.jpg)
11
Program Running
![Page 12: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/12.jpg)
12
Common Mistakes
See what happens if you put a semicolon after the control
statement
for (i = 0; i <= 10; i++);
{
printf ("%3d %6d\n", i, i*i);
}
![Page 13: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/13.jpg)
13
Program Running
![Page 14: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/14.jpg)
14
Common Mistakes
See what happens if you increment the control variable inside
the loop body
for (i = 0; i <= 10; i++)
{
printf ("%3d %6d\n", i, i*i);
i++;
}
![Page 15: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/15.jpg)
15
Program Running
![Page 16: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/16.jpg)
16
Common Mistakes
See what happens if you use commas rather than semicolons
in the control statement
for (i = 1, i <= 10, i++)
{
printf ("%3d %6d\n", i, i*i);
i++;
}
![Page 17: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/17.jpg)
17
Compile Error
![Page 18: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/18.jpg)
18
Alternative Test
You could test for inequality rather than less than.
Usually NOT a good idea!
#include <stdio.h>
int main()
{
int i = 0;
printf ("This program outputs a table of squares\n");
for (i = 0; i != 10; i++)
{
printf ("%3d %6d\n", i, i*i);
}
getchar(); // Keep window open
getchar();
return 0;
}
![Page 19: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/19.jpg)
19
Works Fine
![Page 20: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/20.jpg)
20
Variations
What if we want squares of just the even numbers?
for (i = 0; i != 10; i+=2)
{
printf ("%3d %6d\n", i, i*i);
}
![Page 21: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/21.jpg)
21
Works Fine!
![Page 22: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/22.jpg)
22
Variations
How about just the odd numbers?
for (i = 1; i != 10; i+=2)
{
printf ("%3d %6d\n", i, i*i);
}
![Page 23: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/23.jpg)
23
Not so good!
Have to stop with Ctrl-C
![Page 24: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/24.jpg)
24
Lesson Learned
On tests for inequality, the limit value must be exactly right.
Prefer tests for less than over tests for not equal to stop a loop. Be sure the loop stops even if the
limiting value is not exactly right!
End of Section
![Page 25: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/25.jpg)
25
Modifying Control Flow
C provides several statements that modify the normal flow of control within a loop: break continue goto
break and continue are OK. Use as appropropriate to break out of a loop
or immediately start the next iteration.
goto is bad. A relic of the olden days.
Kept mainly for compatibility. Forget that it is there.
![Page 26: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/26.jpg)
26
C99
C99 permits the loop control variable to be defined in the control statement:
for (int i = 0; i <= 10; i++)
{
printf ("%3d %6d\n", i, i*i);
}
i will not be visible outside the loop. This construct was invented in C++
Adopted for C in C99
![Page 27: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/27.jpg)
27
Using C99
Circe supports C99
gcc -Wall -std=c99 xxx.c
but Visual Studio does not.
![Page 28: 1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i](https://reader035.fdocuments.in/reader035/viewer/2022081513/5697bf791a28abf838c82455/html5/thumbnails/28.jpg)
28
Compile and Run on Circe