Академический Документы
Профессиональный Документы
Культура Документы
Counter Sentinel
Controlled Controlled
Condition
Controlled
1
01/24/2020
• Counter-controlled repetition
– Definite repetition – know how many times loop will execute.
– Control variable used to count repetitions.
• Condition-controlled repetition
– Loop executes as long as some specified condition is true.
• Sentinel-controlled repetition
– Indefinite repetition.
– Used when number of repetitions not known.
– Sentinel value indicates “end of data”.
Counter-controlled Repetition
2
01/24/2020
35
3
01/24/2020
while Statement
false
C Single-entry /
single-exit
true structure
statement(s)
4
01/24/2020
while :: Examples
int digit = 0; int weight=100;
NO IS YES
COUNT > N? OUTPUT SUM
STOP
5
01/24/2020
do-while Statement
do do {
statement_to_repeat; statement-1;
while (condition ); statement-2;
statement-n;
} while (condition );
6
01/24/2020
statement(s)
Single-entry /
single-exit
structure
false
C
true
do-while :: Examples
int digit = 0;
do
printf (%d \n, digit++);
while (digit <= 9);
int weight;
do {
printf (Go, exercise, );
printf (then come back. \n);
printf (Enter your weight:);
scanf (%d, &weight);
} while (weight > 65 );
7
01/24/2020
for Statement
• How it works?
– “expression1” is used to initialize some variable (called
index) that controls the looping action.
– “expression2” represents a condition that must be true
for the loop to continue.
– “expression3” is used to alter the value of the index
initially assigned by “expression1”.
8
01/24/2020
expression1
false Single-entry /
expression2
single-exit
structure
true
statement(s)
expression3
for :: Examples
int fact = 1, i, N; int sum = 0, N, i;
9
01/24/2020
2-D Figure
Print
***** #define ROWS 3
#define COLS 5
*****
....
***** for (row=1; row<=ROWS; row++) {
for (col=1; col<=COLS; col++) {
printf(*);
}
printf(\n);
}
10
01/24/2020
statement(s)
expression3
• Arithmetic expressions
– Initialization, loop-continuation, and increment can
contain arithmetic expressions.
for (k=x; k <= 4*x*y; k += y/x)
• "Increment" may be negative (decrement)
for (digit=9; digit>=0; digit--)
• If loop continuation condition initially false:
– Body of for structure not performed.
– Control proceeds with statement after for structure.
11
01/24/2020
for (; ;)
while (1) {
{
statements
statements
}
}
do {
statements
} while (1);
12
01/24/2020
fact = 1; i = 1;
13
01/24/2020
14
01/24/2020
Some Examples
Example: SUM = 12 + 22 + 32 + N2
int main () {
START int N, count, sum;
scanf (%d, &N) ;
sum = 0;
READ N
count = 1;
while (count <= N) {
SUM = 0 sum = sum + countcount;
COUNT = 1 count = count + 1;
}
SUM = SUM + COUNT COUNT printf (Sum = %d\n, sum) ;
return 0;
}
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT SUM
STOP
15
01/24/2020
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT PROD
STOP
READ X, N
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT SUM
STOP
16
01/24/2020
READ X
TERM = 1
SUM = 0
COUNT = 1
COUNT = COUNT + 1
NO IS YES
TERM < 0.0001? OUTPUT SUM
STOP
#include <stdio.h>
main()
{
int n, i=2;
scanf (”%d”, &n);
while (i < n) {
if (n % i == 0) {
printf (”%d is not a prime \n”, n);
exit;
}
i++;
}
printf (”%d is a prime \n”, n);
}
17
01/24/2020
More efficient??
#include <stdio.h>
#include <math.h>
main()
{
int n, i=3;
scanf (”%d”, &n);
while (i < sqrt(n)) {
if (n % i == 0) {
printf (”%d is not a prime \n”, n);
exit(0);
}
i = i + 2;
}
printf (”%d is a prime \n”, n);
}
Programming and Data Structure 65
#include <stdio.h>
main()
{
int n, sum=0;
scanf (”%d”, &n);
while (n != 0) {
sum = sum + (n % 10);
n = n / 10;
}
printf (”The sum of digits of the number is %d \n”, sum);
}
18
01/24/2020
#include <stdio.h>
main()
{
int dec;
scanf (”%d”, &dec);
do
{
printf (”%2d”, (dec % 2));
dec = dec / 2;
} while (dec != 0);
printf (”\n”);
}
#include <stdio.h> 12 ) 45 ( 3
main()
36
{
9 ) 12 ( 1
int A, B, temp;
9
scanf (”%d %d”, &A, &B);
3 ) 9 ( 3
if (A > B)
{temp = A; A = B; B = temp;} 9
while ((B % A) != 0) { 0
temp = B % A;
B = A; Initial: A=12, B=45
A = temp; Iteration 1: temp=9, B=12,A=9
Iteration 2: temp=3, B=9, A=3
}
printf (”The GCD is %d”, A); B % A = 0 GCD is 3
}
Programming and Data Structure 68
19
01/24/2020
Shortcuts in Assignments
a += b is equivalent to a = a + b
a *= (b+10) is equivalent to a = a * (b + 10)
and so on.
20
01/24/2020
• General syntax:
scanf (control string, arg1, arg2, …, argn);
– “control string refers to a string typically containing data types of
the arguments to be read in;
– the arguments arg1, arg2, … represent pointers to data items in
memory.
Example: scanf (%d %f %c, &a, &average, &type);
• The control string consists of individual groups of
characters, with one character group for each input data
item.
– ‘%’ sign, followed by a conversion character.
21
01/24/2020
• General syntax:
printf (control string, arg1, arg2, …, argn);
– “control string refers to a string containing formatting
information and data types of the arguments to be output;
– the arguments arg1, arg2, … represent the individual output
data items.
• The conversion characters are same as in scanf.
• Can specify the width of the data fields.
– %5d, %7.2f, etc.
• Examples:
printf (The average of %d and %d is %f, a, b, avg);
printf (Hello \nGood \nMorning \n);
printf (%3d %3d %5d, a, b, a*b+2);
printf (%7.2f %5.1f, x, y);
22
01/24/2020
An example
#include <stdio.h>
main() 0 -17.778
{ 20 -6.667
int fahr; 40 4.444
60 15.556
for (fahr=0; fahr<=100; fahr+=20) 80 26.667
printf (%3d %6.3f\n”, 100 37.778
fahr, (5.0/9.0)*(fahr-32));
}
#include <stdio.h>
main() 000 -17.778
{
020 -6.667
int fahr;
040 4.444
060 15.556
for (fahr=0; fahr<=100; fahr+=20)
printf (“%03d %6.3f\n”, 080 26.667
fahr, (5.0/9.0)*(fahr-32)); 100 37.778
}
23