Repetition - PowerPoint PPT Presentation

About This Presentation
Title:

Repetition

Description:

1 Write a program the computes and displays a multiplication table for numbers 1 to 12. ... A program that prints the multiplication table. for the numbers 1 to ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 15
Provided by: Nam9
Category:

less

Transcript and Presenter's Notes

Title: Repetition


1
Repetition
  • Loops are common in programming (computers can do
    boring things over and over again)
  • Type of loop When used C-structures (our tools)
  • Counting We know how many loops while and for
  • Sentinel- Input of data with a special while
    and for
  • Controlled ending symbol
  • Endfile-controlled Input of data until the file
    is ended while and for
  • Input validation Repeted input if data is out of
    range do-while
  • General Process data until desired condition
    while and for

2

while-statement
while (expr) statement next statement
  • Conditional repetition the condition (expr) is
    evaluated before the statement (often a compound
    statement) is either executed or not
  • If the value of (expr) is true then the statement
    is executed, otherwise control is tranfered to
    next statement
  • I.e. the statement inside the while-loop might
    not be executed
  • The value of (expr) should be false at some
    point otherwise we have created an infinit loop

3
Ex. 1 and Ex. 2
factorial 1.0 i1 while (i lt n)
factorial i ----------------------------------
-- lowercase-letter.cnt 0 total_cnt
0 while ((c getchar()) ! EOF) if (c gt
a c ltz) lowercase_letter_cnt
total_cnt
4
cnt_char.c
5
do-statement
do statement while (expr) next statement
  • The condition (expr) is tested after the
    statement is executed -gt the statement is
    executed at least once

6
Compute average
Write a program that reads real numbers from the
key-board until the value zero is read and
computes the average of the read
numbers. include ltstdio.hgt int main(void)
double sum0.0, number int n0 / counts the
number of values read / printf(Type a number of
real numbers. End by zero(0.0)\n) do
scanf(lf, number) sum sum number
n while (number ! 0.0) if (n ! 0)
printf(\nThe average is .3f\n, sum/n) else
printf(\nNo values are typed\n) return (0)
7
for-satsen
for (expr1 expr2 expr3) statement next
statement
i
  • Fixed number of loops
  • Semantically the same as
  • expr1
  • while (expr2)
  • statement
  • expr3
  • next statement

for (i1 ilt5 ii1) printf(2dd ,2i,i)
8
Examples
i 1 sum 0 for ( i lt 10 i) sum
i Does the same as sum0 for (i1 i lt 10
i) sum sum i As will this code do i
1 sum 0 for ( i lt 10 ) sum i
9
The comma-operator
  • Lowest priority of all operators
  • It is left-right associative
  • x (expr1 , expr2)
  • Means that expr1 is evaluated and than expr2. The
    value of whole expresion is the value of expr2
  • Comma-operator is usually used in for-loops

for (sum 0, i 1 i lt n i) sum
i for (sum 0, i 1 i lt n sum i, i)
/ Note the empty statement / for (sum 0, i
1 i lt n i, sum i) / not same result /
10
break continue
  • Changes flow of control
  • break ends the current loop or
    switch-statement
  • continue ends the current iteration and
    continues with the next iteration
  • while (1) / 1 is always true /
  • scanf(lf, x)
  • if (x lt 0.0)
  • break
  • printf(f\n, sqrt(x))
  • / break jumps to here /

11
Figure 5.11 Program Showing a Sentinel-Controlled
Loop / Compute the sum of a list of exam
scores. / include ltstdio.hgt define SENTINEL
-99 int main(void) int sum 0, /
sum of scores input so far /
score / current score /
printf("Enter first score (or d to quit)gt ",
SENTINEL) for (scanf("d", score)
score ! SENTINEL
scanf("d", score)) sum score
printf("Enter next score (d to quit)gt
", SENTINEL) printf("\nSum of
exam scores is d\n", sum) return (0)
12
Figure 5.12 Batch Version of Sum of Exam Scores
Program / Compute the sum of the list of
exam scores stored in the file scores.dat
/ include ltstdio.hgt / defines fopen,
fclose, fscanf,
fprintf, and EOF / int main(void)
FILE inp / input file pointer /
int sum 0, / sum of scores input so
far / score, / current score
/ input_status / status value
returned by fscanf / inp
fopen("scores.dat", "r")
printf("Scores\n") for (input_status
fscanf(inp, "d", score)
input_status ! EOF input_status
fscanf(inp, "d", score))
printf("5d\n", score) sum score
printf("\nSum of exam scores is
d\n", sum) fclose(inp) return
(0)
13
Nested loops
  • Inside a loop there might be another loop and an
    other..
  • Ex. 1 Write a program the computes and displays a
    multiplication table for numbers 1 to 12.
  • The output should look like
  • 1 2 3 4 5 6 7 8 9 10 11 12
  • -------------------------------------
  • 1 1 2 3 4 5 6 7 8 9 10 11 12
  • 2 2 4 6 8 10 12 14 16 18 20 22 24
  • 3 3 6 9 12 15 18 21 24 27 30 33 36
  • And so on

14
peppar/c/Ckodgt cat multplic.c include
ltstdio.hgt int main(void) / A program that
prints the multiplication table for the numbers 1
to 12 / int i, j / Print the heading
/ printf("\n ") for (i1 i lt 13 i)
printf("3d", i) putchar('\n') / underline
/ for (i1 ilt40 i) putchar('-')
putchar('\n') / Print the table / for (i1
ilt12 i) printf("3d", i) for
(j1 jlt12 j) printf("3d", ij)
putchar('\n') / end of for i-loop / return
0
Write a Comment
User Comments (0)
About PowerShow.com