Sie sind auf Seite 1von 43

While loop

Example: Re-Writing 1-1000


(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}

Output: Counter = 1
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}

Output: Counter = 1
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
}

Output: Counter = 1
Infinite Loops

This loop isnt making a lot of progress!


Loops that repeat forever are called infinite loops
Apparently lock up
Output:

Counter = 1
Counter = 1
Counter = 1
.
.
} Continue
forever

.
Problem Solved

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
Problem Solved

int counter = 1; counter


true
1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
Problem Solved

int counter = 1; counter

1
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}

Output: Counter = 1
Problem Solved

int counter = 1; counter

2
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
// Remember, counter++ is the same as
// counter = counter + 1
Example: Re-Writing 1-1000
(using a while loop)

int counter = 1; counter

2
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
Problem Solved

int counter = 1; counter


true
2
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
Problem Solved

int counter = 1; counter

2
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}

Output: Counter = 2
Problem Solved

int counter = 1; counter

3
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
How does it end?

int counter = 1; counter

999
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
How does it end?

int counter = 1; counter

999
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
How does it end?

int counter = 1; counter


true
999
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
Problem Solved

int counter = 1; counter

999
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}

Output: Counter = 999


How does it end?

int counter = 1; counter

1000
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
How does it end?

int counter = 1; counter

1000
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
How does it end?

int counter = 1; counter


now false
1000
while (counter < 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
// So we never print out
// Counter = 1000
Another Problem Solved

int counter = 1; counter


now true
1000
while (counter <= 1000) {
printf(Counter = %d \n ,counter) ;
counter++;
}
// So we can now finish printing
// Still fails at 1001
do while loop
The do-while loop

Similar to while loop


Must execute at least one time (test is at the bottom)

Has format:
do {

} while (condition) ; // A semi colon is placed after


while statement in do-while
Difference between while and do-
while?
Example
(count from 1 to 3)

int counter = 0; counter

0
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);
Example
(count from 1 to 3)

int counter = 0; counter

0
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);
Example
(count from 1 to 3)

int counter = 0; counter

1
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);
Example
(count from 1 to 3)

int counter = 0; counter

1
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);

Output: 1
Example
(count from 1 to 3)

int counter = 0; counter

1
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);
Example
(count from 1 to 3)

int counter = 0; counter

2
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);
Example
(count from 1 to 3)

int counter = 0; counter

2
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);

Output: 2
Example
(count from 1 to 3)

int counter = 0; counter

2
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);
Example
(count from 1 to 3)

int counter = 0; counter

3
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);

// Note: counter is now 3, but we still have


// to finish out the loop it doesnt skip
Example
(count from 1 to 3)

int counter = 0; counter

3
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);

Output: 3
Example
(count from 1 to 3)

int counter = 0; counter

3
do {
counter++;
printf(%d\n, counter);
} while (counter < 3);
now false, so loop is finished
Simple Rules

while and do-while good for when you


dont how many times you want to
repeat the loop
All loops must finish, or they become
infinite loops
All loops must have a test to continue,
or they become infinite loops
Exercise (do-while)
Exercise: Finding Average grades of multiple
classes
We would like a program to compute the
average grade of each of a set of classes.
For each class, operate just like our previous
grade average program.
Need to be careful that the average grade for
each class is computed independently of other
classes!
After computing the average for a class, need to
see if there is another class.

Draw a flowchart and write the program of the above exercise.

Das könnte Ihnen auch gefallen