Week 4 Slides.

Transkript

Week 4 Slides.
Senem Kumova Metin & Ilker Korkmaz
1



A loop is a block of code that can be performed
repeatedly.
A loop is controlled by a condition that is checked
each time through the loop.
C supports two categories of loops, based on
where the program tests the condition:
 Pretest Loops : while, for
 Post-test Loops : do-while
Senem Kumova Metin & Ilker Korkmaz
2



With each iteration, the program tests the condition
first before executing the loop’s block.
If the condition tests to true, the loop continues
and executes the block; if the condition tests to
false, the loop terminates.
With a pretest loop, there is a chance the loop may
never execute in the run of a program!
3



With each iteration, the program executes the
loop’s block first and tests against a condition.
If the condition tests to true, the loop continues
and executes another iteration; if the condition
tests to false, the loop terminates.
With a post-test loop, the loop will always execute
at least once!
4
from Figure 6-2 in Forouzan & Gilberg, p. 305
5
from Figure 6-5 in Forouzan & Gilberg, p. 307
6
from Figure 6-11 in Forouzan & Gilberg, p. 311
7
while( expression )
statement
/* loop body */
OR try to use like this:
while(expression )
{
/* loop body*/
statement1;
statement2;
// … ;
}
Senem Kumova Metin & Ilker Korkmaz
8
#include <stdio.h>
int main() {
int i;
printf("Enter -1 to quit:\n");
scanf("%d", &i);
while (i != -1) {
printf("%d was read\n", i);
scanf("%d", &i);
}
return 0;
}
Senem Kumova Metin & Ilker Korkmaz
9
Enter -1 to quit:
1
1 was read
2
2 was read
3
3 was read
4
4 was read
5
5 was read
-1
Press any key to continue . . .
Senem Kumova Metin & Ilker Korkmaz
10
from Figure 6-15 in Forouzan & Gilberg, p. 320
11
Loop Body is executed at LEAST ONCE
do {
/* loop body*/
statements;
...
} while (expression);
Senem Kumova Metin & Ilker Korkmaz
12
#include <stdio.h>
int main() {
int i = 0;
do {
printf("%d\n", i);
i++; /* increment i */
} while(i <= 10);
return 0;
}
Senem Kumova Metin & Ilker Korkmaz
13
0
1
2
3
4
5
6
7
8
9
10
Press any key to continue . . .
Senem Kumova Metin & Ilker Korkmaz
14
from Figure 6-13 in Forouzan & Gilberg, p. 321

A for loop is a pretest loop that includes three
expressions in its header:
 Loop initialization statement
 Limit test expression
 Loop update statement


Although not recommended, C allows us to update
the loop control statement from within the loop
body.
The for loop is often used as a counter-controlled
loop since we can accurately predict the maximum
number of iterations
16
from Figure 6-13 in Forouzan & Gilberg, p. 316
17
for (initialize; check; update) {
statements;
/* loop body */
}
Senem Kumova Metin & Ilker Korkmaz
18
from Figure 6-14 in Forouzan & Gilberg, p. 317
19
#include <stdio.h>
int main() {
int i = 0;
while(i <= 10) {
printf("%d\n",i);
i++;
}
return 0;
}
The while loop
#include <stdio.h>
int main() {
int i;
for(i = 0; i <= 10;i++) {
printf("%d\n", i);
}
printf("%d\n",i);
return 0;
}
The for loop
Senem Kumova Metin & Ilker Korkmaz
20


We can nest any statement, even another for loop,
inside the body of a parent for loop.
When we nest a child for loop, it iterates all of it’s
cycles for each iteration of the parent.
#include <stdio.h>
int main(){
int i,j;
for (i=0 ; i < 10 ; i++) {
for (j=0; j < 10; j++)
printf("%d %d\n", i, j);
}
return 0;
}
21
#include <stdio.h>
int main(){
int i;
for (i=0 ; ; i++) { // while(1)
printf("%d\n", i);
}
return 0;
}
Senem Kumova Metin & Ilker Korkmaz
22
expression1, expression2 e
, expression3
Expression 1 is evaluated at first, then expression2 is
evaluated
for(sum=0, i=1; i<=n; ++i) {
sum+=i;
}
can be written as
for(sum=0, i=1; i<=n; sum+=i, ++i)
but not as
for(sum=0, i=1; i<=n; ++i, sum+=i)
Senem Kumova Metin & Ilker Korkmaz
23


We can use the break statement to preemptively
end a loop (ending a for loop early, for example,
based on some nested if structure).
However, this is poor program design and NOT
recommended.
24
Break causes an exit from the innermost enclosing
loop or switch statement.
double x;
while(1) {
// an infinite loop
scanf(“%lf”,&x);
if(x<0.0)
break;
// exit loop if x is negative
printf(“%f\n”,sqrt(x));
}
// break jumps to here
Senem Kumova Metin & Ilker Korkmaz
25
#include <stdio.h>
int main(){
int i;
for(i = 0; i < 10000; i++) {
if(getchar() == 's')
break;
printf("%d\n", i);
}
return 0;
}
Senem Kumova Metin & Ilker Korkmaz
26
from Program 6-16 in Forouzan & Gilberg, p. 339
27


Unlike break, the continue statement doesn’t
terminate a loop, but transfers control to the
testing expression (in while and do … while
loops) or the update expression (in for loops).
Like the break statement, avoid using the
continue statement.
28
Continue by-passes the related loop iteration.
double x;
while(1) { // an infinite loop
scanf(“%lf”,&x);
if(x<0.0)
continue ;
//by-pass below statement
printf(“%f\n”,sqrt(x));
}
Senem Kumova Metin & Ilker Korkmaz
29
from Program 6-18 in Forouzan & Gilberg, p. 341
30


goto keyword is followed by a label, which is
basically some identifier placed elsewhere in the
program
Do not use goto in your programs!
Senem Kumova Metin & Ilker Korkmaz
31
int main() {
int attempt, number = 46;
loopTag:
/* a label */
printf("Guess a number from 0-100\n");
scanf("%d", &attempt);
if (number == attempt) {
printf("You guessed correctly!\n");
} else {
printf("Let me ask again...\n");
goto loopTag; /* Jump to the label*/
}
return 0;
}
Senem Kumova Metin & Ilker Korkmaz
32
33

Benzer belgeler