Transcript LECT17.PPT

More Loops
‘for’ loops
and
‘do-while’ loops
CMSC 104
1
Counter-Controlled Repetition with a
while loop
#include <stdio.h>
main ()
{
int i = 1;
/* count from 1 to 10 */
while ( i < 11 )
{
printf (“%d “, i);
i++;
}
initialization of loop
control variable
test condition that terminate loop
modification of loop control
variable
}
CMSC 104
2
The for loop Repetitive Structure


The for loop handles details of the counter-controlled
loop automatically
The initialization of the the loop control variable,
termination conditional test and modification are
handled in for loop structure
for ( i = 1; i < 11; i++)
{
initialization
modification
}
test
CMSC 104
3
When does the for loop initialize, test
and modify ?

Just as in the while loop that counted, the for
loop
o Initializes the loop control variable before
beginning
o modified the loop control variable at the
very end of each iteration of the loop
o performs the conditional termination test
before each iteration of the loop

The for loop is easier to write
CMSC 104
4
A for loop that counts
from 0 to 9
for (i = 0; i < 10; i++)
{
printf (“%d”, i);
}
printf (“\n”);
CMSC 104
5
We can count backwards, too
for (i = 10; i > 0; i--)
{
printf (“%d”, i);
}
printf (“\n”);
CMSC 104
6
We can count by 2’s
... or 7’s ... or whatever
for (i = 0; i < 10; i += 2)
{
printf (“%d”, i);
}
printf (“\n”);
CMSC 104
7
The do-while repetitive structure
do
{
statement(s)
} while (condition);

CMSC 104
The body of the do-while is ALWAYS
executed at least once
8
do-while example
do
{
printf (“Enter a positive number: “);
scanf (“%d”, &num);
if (num <= 0)
{
printf (“\nThat is not positive, try again\n”);
}
} while (num <= 0);
CMSC 104
9
A while that tests input
Compare with do-while
printf (“Enter a positive number: “);
scanf (“%d”, &num);
while (num <= 0)
{
printf (“\nThat is not positive, try again\n”);
printf (“Enter a positive number: “);
scanf (“%d”, &num);
}
CMSC 104
10
for vs while

use a for loop when your program
“knows” exactly how many times to loop

use a while loop when there is a
condition that will terminate your loop
CMSC 104
11
for loops for specified number of
iterations
printf (“Enter the number of students: “);
scanf (“%d”, &numStudents);
/* we now “know” how many times to loop */
for (student = 1; student < numStudents; student++)
{
printf (“Enter grade: “);
etc.
}
CMSC 104
12
use while when a condition
terminates your loop

the use of a sentinel is a good example...your
program doesn’t “know” when the SENTINEL
will be encountered
printf (“Enter grade: “);
scanf (“%d”, &grade);
while (grade != SENTINEL)
{
etc.
}
CMSC 104
13
while vs do-while

while required “priming read”

do-while required extra test

use do-while when body must be
executed at least once
CMSC 104
14
break

break can be used in while, do-while and
for loops to cause premature exit of the loop.

THIS IS
A RECOMMENDED
CODING TECHNIQUE
CMSC 104
NOT
15
Example break in a loop
#include <stdio.h>
main ( )
{
int i;
for (i = 1; i < 10; i++)
{
if (i == 5)
{
break;
}
printf (“%d “, i);
}
printf (“\nbroke out of loop at i = %d\n”, i);
}
CMSC 104
OUTPUT:
1234
Broke out of loop at i = 5
16
continue

continue can be used in a for, while,
or do-while loop

It causes the remaining statements in the
body of the loop to be skipped for the current
iteration of the loop. The loop continues with
the next iteration
CMSC 104
17
Example of continue in a loop
#include <stdio.h>
main ( )
{
int i;
for (i = 1; i < 10; i++)
{
if (i == 5)
{
continue;
}
printf (“%d”, i);
}
printf (“\n”);
}
CMSC 104
OUTPUT:
12346789
18
Nested for loops
CMSC 104
for (i = 1; i < 5; i++)
{
for (j = 1; j < 3; j++)
{
if (j % 2 == 0)
{
printf (“O”);
}
else
{
printf (“X”);
}
}
printf (“\n”);
}
How many times is the
‘if’ statement executed?
What is the output ??
19
The char data type
The char data type holds a single
character
char ch;
 The char is held as a one-byte integer in
memory. The ASCII code is what is
actually stored, so we can use them as
characters or integers, depending on
our purpose
 Use scanf (“%c”, &ch); to input 1 char

CMSC 104
20
Character Example
#include <stdio.h>
main ( )
{
char ch;
printf (“Enter a character: “);
scanf (“%c”, &ch);
printf (“The value of %c is %d.\n”, ch, ch);
}
If the user entered an A the output would be
The value of A is 65.
CMSC 104
21
The getchar ( ) function

We can also use the getchar() function that is
found in the stdio library

The getchar ( ) function reads one character
from stdin and returns that character (value)

The value can then be stored in either a char
variable or an integer variable
CMSC 104
22
getchar () example
#include <stdio.h>
main ( )
{
char grade;
printf (“Enter a letter grade: “);
grade = getchar ( );
printf (“\nThe grade you entered was %c.\n”, grade);
}
CMSC 104
23