Chapter 6Decision Making
and Looping
C PROGRAMMING
KATKAR ATISH R. (Asst. Prof., Dept. of CSE)
3/9/22
Question
Questions:
How do we calculate the sum from 1 to 5?
How do we calculate the sum from 1 to 100?
main()
{ int sum=0; Let's find out the law of such question:
sum=sum+1;
sum=sum+2; When i=1,2,3,…,100
sum=sum+3;
sum=sum+4;
…… do: sum = sum + i;
sum=sum+5;
sum=sum+100;
printf("sum=%d",sum);
Loop construct!
}
3/9/22
Flow chart
In a looping, a sequence of
begin
i=1, sum=0; statements is repetitively
N executed until the loop test
i<=10
0 Y
sum = sum+i;
condition can’t be satisfied.
i=i+1; A loop statement consists of
2 segments:
output:
The loop control statement
sum
end The loop body
3/9/22
Chapter 6
In this chapter, we will learn:
while statement
do...while statement
for statement
Assisted control statements: break and continue
Looping consists of if and goto
3/9/22
while Statement
The form of while statement: while ( test condition )
loop body
fals
test e
The test condition is evaluated condition (0)
first, and if it is true, the loop true
(not
body is executed. loop body 0)
3/9/22
while If
Statement
the loop body consists of more than one statement, we should
use compound statement.
Program to calculate the sum from 1 to 100.
begin
main()
{
i=1, sum=0;
int i=1, sum=0;
N
i<=10 while ( i <= 100 )
0 Y test condition { sum = sum + i;
sum = sum+i; i++;
}
printf("%d", sum);
i=i+1; loop body
}
output:
sum
end
3/9/22
while Statement
We can use ctrl+break to stop the execution
Program to calculate of program.
the sum from 1 to 100.
begin
main()
infinite
{
i=1, sum=0; loop
int i=1, sum=0;
N
i<=10 while ( i <= 100 )
0 Y { sum = sum + i;
sum = sum+i; i++;
}
printf("%d", sum);
i=i+1;
}
output:
sum
end
3/9/22
while Statement – 2 Segments
while ( test condition )
{loop
if
body
sum = sum+i;
(sum==50)
;printf("sum=%d", sum=sum+i);
i =printf("i=%d",i);
i+1; }
statment type examples
expression statement printf("sum=%d", sum=sum+i);
void statement ;
control statement if (sum==50) printf("i=%d",i);
compound statement { sum=sum+i; i=i+1; }
3/9/22
while Statement – 2 Segments
while ( test condition )
loop body
The loop body can be any type statement.
The loop body should be a single statement or a
compound statement.
The loop body must contain some statements making
the loop tend to end. Avoid infinite loop.
3/9/22
do...while Statement
The form of do...while statement: do loop body while
( test condition );
loop body
true
test
The loop body is executed condition
first, and then the test fals
e
condition is evaluated. In do...while statement, the
loop body is always executed at
least once.
3/9/22
do...while Statement
do...while statement can change to while statement:
loop body
loop body
true
fal test
test condition
se
condition fals
true e
loop body
3/9/22
do...while Statement
do...while statement can change to while statement:
do loop body while ( test loop bodywhile ( test condition )
condition ); loop body
3/9/22
do...while Statement
Like while statement, if the loop body consists of more than one statement,
we should use compound statement.
Program to calculate the sum from 1 to 100.
begin
main()
{ int i=1, sum=0;
i=1, sum=0;
N do
i<=10 { sum=sum+i;
0 Y i++; loop body
sum = sum+i; }
while ( i <= 100 );
i=i+1; printf("%d",sum);
test
} condition
output:
sum
end
3/9/22
do...while Statement – 2 Segments
do
loop body
while ( test condition ); Pay attention to this semicolon!
The loop body can be any type statement.
The loop body should be a single statement or a
compound statement.
The loop body must contain some statements makeing
the loop tend to end. Avoid infinite loop.
3/9/22
while & do...while Statement
1↵ with do...while statement.
101↵
Compare while 101↵
1↵
5050
0 5050
101
main() main()
{ int i, sum=0; { int i, sum=0;
scanf ( "%d", &i ); scanf ( "%d", &i );
while ( i <= 100 ) do
{ {
sum = sum + i; sum = sum + i;
i++; i++;
} —— the test condition is evaluated first
while } while ( i <= 100
do...while ——);the loop
printf ( "%d", sum ); printf ( "%d", sum );
body
} is evaluated first }
while —— the times of execution of loop body≥0 do……while —— the
times of execution of loop body≥1
3/9/22
for Statement
exp1
The form of for statement: for ( [exp1]; [exp2]; [exp3] )
loop body
fal
exp2 se
tru
exp1, exp2 and exp3 are any type expressions, andeloop
theybody
all can be
omitted. But the separator semicolon can't be omitted.
for ( ; ; ) is equivalent to while (1) – infinite loop exp3
for statement can be changed to while statement
3/9/22
for Statement
The form of for statement: for ( [exp1]; [exp2]; [exp3]
is equivalent to : )
sum = 0;
loop body i = 1;
while ( i <= 100 )
The general application form of for statement
{ sum = sum: + i;
for ( initialization; test condition; increment ) i ++;
loop body }
e.g. for( sum = 0, i = 1; i <= 100; i ++ ) sum = sum + i;
3/9/22
for Statement
The form of for statement: for ( [exp1]; [exp2]; [exp3] )
loop body
exp1 and exp3 can be comma expression.
e.g. for( sum = 0, i = 1; i <= 100; sum += i, i++ );
is equivalent to :
sum = 0;
for( i = 1; i <= 100; i++ ) sum += i;
3/9/22
for Statement
Program to calculate the sum from 1 to 100.
begin
main()
i=1, sum=0; { int i, sum;
sum = 0;
N
i<=10 for ( i = 1; i <= 100; i++ )
0 Y sum =i sum
for1;(+i =
= i; 1; i <= 100 ;
sum = sum+i; i = 1;
for ( ; i <= 100;
sum +=) i, i++ );
for
{ printf
sum = ( ; i <= 100;
sum +sum
( "%d", i; i++
); )
i=i+1; } i++; }sum = sum + i;
output:
sum
end
3/9/22
Nesting of Loops
external loop
Nesting of loops means: A loop body contains another
for ( ; ; ) for ( ; ; )
{ {
complete
…… loop construct. ……
do do
{ {
Thesewhile()3 loop constructs can nest each…… other, without
✗
{ while
do }(while
while )( ) ( );
…… { { ……
limitation…… of the layers. while()
…… ……
} while ( ); internal loop {while
do ( )
…… { ……
} } ……
{
…... …...
} } while ( );……
} } …...
} while
…... ( );
}
3/9/22
break Statement
One break can exit only one single loop, the
Form: break; nearest loop.
The break can only be used in a loop or a switch
Function: statement.
Exit from the loop containing it, causing this loop to be terminated.
Exit form the switch statement containing it, causing this switch
statement to be terminated.
3/9/22
break Statement
break;
for
while do exp1
false false
exp …… exp
true break; true
2
…… ……
…… break;
break;
……
…… while
tru
e exp
exp3
fals
e
3/9/22
break Statement
Program to calculate the sum from 1 to 100.
begin
main()
{
i=1, sum=0;
int i, sum = 0;
N for ( i = 1; ; i++ )
i<=10 { if ( i > 100 )
i = 1;
0 Y for ( ; ; ) break;
sum = sum+i; {sum if =( isum + i;)
> 100
} break;
sum = sum + ( i++ );
i=i+1; printf
} ( "%d", sum );
}
output:
sum
end
3/9/22
continue Statement
Form: continue;
Function:
Skip the following statements in this loop, causing the test condition
of the next loop to be judged.
The continue can only be used in loops.
3/9/22
continue Statement for
continue; exp1
while fals
do exp2 e
true
fals
exp e ……
true ……
continue;
…… continue;
…... …...
continue;
…… tru whil
exp3
e exp e
false
3/9/22
continue Statement
Output the numbers, between 100 and 200, which
can't be divided exactly by 3.
main()
{
int n;
for ( n = 100; n <= 200; n++ )
{ if ( n % 3 == 0)
continue;
printf ( "%5d", n );
}
}
3/9/22
Loops – Program 1
Output the former 40 numbers of the Fibonacci
sequence.
Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, …… F1 = 1 (n = 1)
F2 = 1 (n = 2) Fn = Fn-1 + Fn-2 (n≥3) f1 = 1, f2 = 1
for i = 1 to 20
Output: f1, f2
f1 f 2 ? ? ? f f f
1 2 1 f1 = f1 + f2
… f2 = f2 + f1
3/9/22
Loops – Program 1
main()
{ long f1 = 1, f2 = 1;
int i;
i=1;for ( i = 1; i <= 20; i++ )
i=1;
do ( i <= 20 )
while{ printf ( "%12ld %12ld", f1, f2 );
{{ printf (( "%12ld
printf "%12ld %12ld",
%12ld", f1,
f1, f2
f2 ););
if ( i if
if ( i %(2i==
% 2 ==%0 2
0 ) == 0 )("\n"
printf "\n" );
) printf( );
f1 = f1 + f2;
f1
f2 == f1 printf
f2 ++ f2;
f1; ( "\n" );
f2
f1 ==f2f1+ +
i ++; f1;f2;
i ++;
} while ( i }<= 20 );
f2 = f2 + f1;
}
}
3/9/22
Loops – Program 2
Judge whether an integer is a prime number or not.
Make m be divided by all the numbers between 2 and
Read in the
m square root
of m. k = (int) sqrt(m)
If m can be divided exactly by a certain number in those, m is not a
i=2
prime number, otherwise it is.
when i ≤ k
m false
tru
%i==0?
e
brea
k; i=i+1
tru i> false
e is a prime k m isn't a prime
m
3/9/22
If m can be divided exactly by the number between 2 and sqrt(m), m is
Loops – Program 2
not a prime number, so the loop will be terminated by break statement,
and at that
#include time, i must be less or equal to k.
<math.h>
Otherwise m is a prime number, and after the last loop, i is equal to k+1.
main()
{ int m, i, k;
scanf ( "%d", &m );
k = sqrt ( m );
for ( i = 2; i <= k; i++ )
if ( m % i == 0 ) break;
if ( i > k )
printf( "%d is a prime number.", m);
else
printf( "%d is not a prime number.", m);
}
3/9/22
Loops – Program 2
#include <math.h>
main()
{ int m, i, k;
scanf ( "%d", &m );
k = sqrt ( m ); Can't judge 1, 2, 3 !
i =( i2;= 2; i <= k; i++ )
for
while
do
if ( m( i%<=
i ==k0))
{ printf ( "%d is not a prime number", m );
{{ if( if(
m %mi == % 0i )== 0}) break;
break;
break;
i ++; } while ( i <= k );
i ++;if }( i == k )
else
if ( i > k ) printf printf(
( "%d is a"%d
primeisnumber",
a prime number.", m);
m );
else printf( "%d is not a prime number.", m);
}
3/9/22
Loops – Program 3
Output all the primes between 100 and 200.
#include <math.h> Try to use while and do…while
main() statement to rewrite this
{ int m, i, k, n = 0; program.
for ( m = 101; m <= 200; m = m + 2 )
{ k = sqrt ( m );
for ( i = 2; i <= k; i++ )
if ( m % i == 0 ) break;
if ( i > k ) { printf ( "%5d", m );
n = n + 1;}
if ( n % 10 == 0) printf ( "\n" );
}
}
3/9/22
Loops – Program 4
Read in a positive integer, and output it in reversed
order. For example: read in 12345, output 54321.
#include <math.h>
main()
{ int n;
printf ( "Input a positive integer:" );
scanf ( "%d", &n );
while ( n != 0 )
{ printf ( "%d", n % 10 );
n = n / 10; /* number is decreased by 10 times */
}
}
3/9/22
Loops – Program 5
Output the multiplication table.
"i" represents the line : ( 1≤i≤9 )
j
"j" represents the column : ( 1≤j≤i )
i 1×1 = 1
2×1 = 2 2×2 = 4
3×1 = 3 3×2 = 6 3×3 = 9
4×1 = 4 4×2 = 8 4×3 = 12 4×4 = 16
5×1 = 5 5×2 = 10 5×3 = 15 5×4 = 20 5×5 = 25
……
3/9/22
Loops – Program 5
main()
{
int i, j;
for ( i = 1; i < 10; i++ )
{
for ( j = 1; j <= i;
i j++ )
{
printf ( "%d*%d=%d\t", j, i, i*j );
}
printf ( "\n" ); Try to use other nesting of loops to rewrite
} this program.
}
3/9/22
Loops – Program 6
Please input a number: 8
5
Read in integer n, and output n factorial (n!).
5!=120
0!=120
8!=-25216
8!=40320
↵
main()
{ int n; int m=1;
lon
printf ( "Please input a number:" );
g &n );
scanf ( "%d",
while ( n >= 1 )
{ m *= n;
printf ( "%d!=", n );
n -- ;
}
printf ( "%d!=%d", n, m );
}
"%ld",mm););
printf ( "%d",
3/9/22
goto Statement
Used to: Jump unconditionally from one point to
another in a program.
General forms: goto label; label: statement;
…… or …… label: statement; goto
label; …… ……
3/9/22
goto Statement
About label:
A label must be a valid identifier, don’t use an integer as a label.
A label can only mark executable statement, and can’t mark declaration
statement.
A label is unique in a function. Don’t use the same label to mark
different statements.
3/9/22
if and goto
Program to calculate the sum from 1 to 100.
begin main()
{
i=1, sum=0;
int i=1, sum=0;
N loop: if ( i <= 100 )
i<=10
{
0 Y
sum = sum+i; sum += i;
i++;
i=i+1; goto loop;
}
printf ( "%d", sum );
output:
}
sum
end
3/9/22
goto Statement
goto Statement can:
Form looping with if statement;
Jump out of looping. while()
{ ……
while()
{
goto label ;
……
}
}
label : statement
3/9/22
goto Statement
It is a good practice to avoid using goto statement!
goto statements:
Making the compiler generate less efficient code;
Making the program logic more complicated;
Making the program more unreadable;
Even breaking down the whole system.
3/9/22
Homework
Programming Exercises