Control
Statements are used to execute the statements no. of times based on the
condition.
There
are two types of control statements
I.
Unconditional Control statements
goto
II.
Conditional Control statements
There
are 3 types of Conditional control statements.
1.
while [pretest loop]
2.
do-while [posttest loop]
3.for
[pretest loop]
goto statement:-
Goto
statement which enable you to locate or transfer the flow of the program from
one location to another location with in the program. Goto statement is used to
jumping to label location. Label is user defined. With the help of goto label
it can repeat the statements no. of times. Suppose if you specify the condition
statements are executed till the condition fail.
Syntax:-
<label>:
Statements;
goto
<label>;
While:-
While
contains the condition. It checks the condition, if the condition is true it
executes the body of the loop (statements) repeatedly till the condition fail.
If the condition is fail initially it won’t executes the body of the loop.
Hence While is an entry controlled loop statement.
Syntax:-
while(<condition>)
{
<body
of the loop>; (or) <Statements>;
}

Execution Process:
i=1;
while(1<=5)
true
{
Welcome
i=2;
}
While(2<=5)
true
{
Welcome
i=3;
}
while(3<=5)
true
{
Welcome
i=4;
}
While(4<=5)
true
{
Welcome
i=5;
}
while(5<=5)
true
{
Welcome
i=6;
}
While(6<=5)
false



Execution Process:-
n=153
arm=153
s=0
while(153>0)true
{
r=153%10;
=> r=3
s=o+3*3*3;
=> s=27
n=153/10;
=>n=15
}
while(15>0)true
{
r=15%10;
=> r=5
s=27+5*5*5;
=> s=152
n=15/10;
=>n=1
}
while(1>0)
true
{
r=1%10;
=> r=1
s=152+1*1*1;
=> s=153
n=1/10;
=>n=0
}
while(0>0)
fail
if(153==153)
153 is
Armstrong.

Execution Process:-
i=1
n=6
while(1<=6)
true
{
if(6%1==0)
true
c=1;
i=2
}
while(2<=6)
true
{
if(6%2==0)
true
c=2;
i=3;
}
while(3<=6)
true
{
if(6%3==0)
true
c=3;
i=4;
}
while(4<=6)
true
{
if(6%4==0)
false
i=5;
}
while(5<=6)
true
{
if(6%5==0)
false
i=6;
}
while(6<=6)
true
{
if(6%6==0)
true
c=4;
i=7;
}
while(7<=6)
false
if(4==2)
false
6 is
not prime.


Execution Process:
n=153
if(153<10>elses=153%10; => s=3
while(153>=10) true
n=153/10; => n=15
while(15>=10) true
n=15/10; => n=1
while(1>=10) false
The sum of first and last digit is 4.
10>
<10>
10>
Nested While

Do While loop:-
This loop executes a body of the loop (or) set of statements as long as the condition is true.
syntax:
do
{
<body of the loop;> (or) <Statements>;
}
while(<condition>);
This is similar to
while loop, but the difference is this loop will execute the statements at
least once. Do while ends with semicolon(;). Do while is an exit controlled
loop statement.

Execution Process:
stn=1;
etn=10;
while(1<=10)true
{
i=1;
while(1<=10)
{
1 X 1= 1
i=2
}
while(2<=10)true
{
1 X 2= 2
i=3
}
while(3<=10)true
{
1 X 3= 3
i=4
}
… i=11
While(11<=10) fail
stn=2
}
while(2<=10)true
{
i=1
while(2<=10)true
{
2 X 1= 2
i=2
}
while(2<=10)true
{
2 X 2= 4
i=3
}
while(3<=10)true
{
2 X 3= 6
i=4
}
… i=11
till it reaches the 10.
stn=1;
etn=10;
while(1<=10)true
{
i=1;
while(1<=10)
{
1 X 1= 1
i=2
}
while(2<=10)true
{
1 X 2= 2
i=3
}
while(3<=10)true
{
1 X 3= 3
i=4
}
… i=11
While(11<=10) fail
stn=2
}
while(2<=10)true
{
i=1
while(2<=10)true
{
2 X 1= 2
i=2
}
while(2<=10)true
{
2 X 2= 4
i=3
}
while(3<=10)true
{
2 X 3= 6
i=4
}
… i=11
till it reaches the 10.
Do While loop:-
This loop executes a body of the loop (or) set of statements as long as the condition is true.
syntax:
do
{
<body of the loop;> (or) <Statements>;
}
while(<condition>);

Execution Process:
i=1;
do
{
Welcome
i=2
}
while(2<=5); true
do
{
Welcome
i=3;
}
While(3<=5); true
Do
{
Welcome
i=4;
}
while(4<=5); true
do
{
Welcome
i=5;
}
While(5<=5) true
do
{
Welcome
i=6;
}
while(6<=5) falsei=1;
do
{
Welcome
i=2
}
while(2<=5); true
do
{
Welcome
i=3;
}
While(3<=5); true
Do
{
Welcome
i=4;
}
while(4<=5); true
do
{
Welcome
i=5;
}
While(5<=5) true
do
{
Welcome
i=6;
}


Execution Process:
f1=0;
f2=1;
print 0
print 1
f3=0+1=1;
do
{
print 1
f1=1
f2=1
f3=1+1=2
}
while(2<=10);
do
{
print 2
f1=1
f2=2
f3=1+2=3
}
while(3<=10);
do
{
print 3
f1=2
f2=3
f3=2+3=5
}
While(5<=10);
do
{
print 5
f1=3
f2=5
f3=3+5=8
}
while(8<=10);
do
{
print 8
f1=5
f2=8
f3=5+8=13
}
while(13<=10); false
Nested do-While loopf1=0;
f2=1;
print 0
print 1
f3=0+1=1;
do
{
print 1
f1=1
f2=1
f3=1+1=2
}
while(2<=10);
do
{
print 2
f1=1
f2=2
f3=1+2=3
}
while(3<=10);
do
{
print 3
f1=2
f2=3
f3=2+3=5
}
While(5<=10);
do
{
print 5
f1=3
f2=5
f3=3+5=8
}
while(8<=10);
do
{
print 8
f1=5
f2=8
f3=5+8=13
}
while(13<=10); false

for loop
for(initialization ; condition ; increment / decrement)
{
statements; (or) body of for loop;
}
Step 1:- First it enter into the initialization. After initialization it goes to the condition.
Step 2:- If the condition is true it enter into the body of the loop.
Step 3:- After executing the body of the loop it goes to the increment / decrement.
Step 4:-After increment/ decrement If step2 condition is true it repeats the step 3,step 4,step 2 till condition fail.
If the condition
fail it comes out of the loop.for(initialization ; condition ; increment / decrement)
{
statements; (or) body of for loop;
}
Step 1:- First it enter into the initialization. After initialization it goes to the condition.
Step 2:- If the condition is true it enter into the body of the loop.
Step 3:- After executing the body of the loop it goes to the increment / decrement.
Step 4:-After increment/ decrement If step2 condition is true it repeats the step 3,step 4,step 2 till condition fail.

Execution Process:
i=1;
;1<=5;true
Welcome
;;i=2
;2<=5; true
Welcome
;;i=3
;3<=5; true
Welcome
;;i=4
;4<=5; true
Welcome
;;i=5
;5<=5; true
Welcome
;;i=6
;6<=5; false
While style For: Syntax:-
for(;<condition>;)
{
<body of the loop>; (or) <Statements>;
}i=1;
;1<=5;true
Welcome
;;i=2
;2<=5; true
Welcome
;;i=3
;3<=5; true
Welcome
;;i=4
;4<=5; true
Welcome
;;i=5
;5<=5; true
Welcome
;;i=6
;6<=5; false
While style For: Syntax:-
for(;<condition>;)
{
<body of the loop>; (or) <Statements>;



Nested For
Syntax:-
for(initialization;<condition1>;increment/decrement)
{
for(initialization;<condition2>;increment/decrement)
{
<body of the loop>; (or) <Statements>;
}
}
Step 1:- First it enter into the initialization. After initialization it goes to the condition.
Step 2:- If the condition is true it enter into the 2nd for loop initialization.
Step 3:- After initialization it enter into the 2nd for loop condition.
Step 4:- If the 2nd for loop condition is true it enter into the body of the loop.
Step 5:-After executing the body of the loop it goes to the 2nd for loop increment decrement.
Step 6:-After increment/ decrement it enter into the 2nd for loop condition. If the condition is true it executes the body of the loop till the 2nd for loop condition fail. If the condition fail it enter into the first for loop increment / decrement.
Step 7:- After increment / decrement it enter into first for loop condition.

Write a Program to print the output in
the following:
* * *
* * *
* * *
* * *
* * *
* * *

Execution Process:
n=3
r=1;1<=3;true
c=1;1<=3;true
*
;;c=2
;2<=3;true
*
;;c=3
;3<=3; true
*
;c=4
;4<=3; fail
;;r=2
;2<=3;true
c=1;1<=3;true
*
;;c=2
;2<=3;true
*
;;c=3
3<=3;true
*
;;c=4
4<=3;fail
;;r=3
;3<=3;true
c=1;1<=3;true
*
;;c=2
;2<=3;true
*
;;c=3
3<=3;true
*
;;c=4
4<=3;fail
;;r=4
;4<=3; fail
Write a Program to print the output in the following:
*
* *
* * *n=3
r=1;1<=3;true
c=1;1<=3;true
*
;;c=2
;2<=3;true
*
;;c=3
;3<=3; true
*
;c=4
;4<=3; fail
;;r=2
;2<=3;true
c=1;1<=3;true
*
;;c=2
;2<=3;true
*
;;c=3
3<=3;true
*
;;c=4
4<=3;fail
;;r=3
;3<=3;true
c=1;1<=3;true
*
;;c=2
;2<=3;true
*
;;c=3
3<=3;true
*
;;c=4
4<=3;fail
;;r=4
;4<=3; fail
Write a Program to print the output in the following:
*
* *

Write a Program to print the output in the
following:
* *
* * *
No comments:
Post a Comment