Decision Making and Looping
Decision Making and Looping
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
2 segments:
output:
The loop control statement
sum
end The loop body
3/9/22
Chapter 6
do...while statement
for statement
while Statement
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
{loop
if
body
sum = sum+i;
(sum==50)
;printf("sum=%d", sum=sum+i);
i =printf("i=%d",i);
i+1; }
void statement ;
loop body
compound statement.
do...while Statement
true
test
The loop body is executed condition
do...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
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
loop body
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 Statement
for Statement
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
✗
{ 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.
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
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
Loops – Program 1
sequence.
Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, …… F1 = 1 (n = 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
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 );
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
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
another in a program.
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.
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
goto statements:
Making the compiler generate less efficient code;
Homework
Programming Exercises