Uploaded by Dr. Chaitanya D L

6 FLOW CONTROL LOOPING & JUMPING

advertisement
Looping Control Statements
(Repetitive or Iterative Statements)
 WHILE
 DO.. WHILE
 FOR
 EXITING
FROM A LOOP
(BREAK, CONTINUE, GOTO)
Looping Control Statements
• Whenever we need to execute some
statements multiple times, we need to use
a loop statement.
• Loop control statements in C are used to
execute a block of code several times until
the given condition is satisfied(true).
• The loop is defined as a block of
statements that are repeatedly executed
for certain number of times.
Types of Looping Control Statements
• Basically, the type of looping control statements
depends on the condition checking mode.
• Entry controlled loop: In such type of loop, the
test condition is checked first before the loop is
executed.
Ex: while and for
• Exit controlled loop: In such type of loop, the
loop is executed first, then condition is checked,
the loop executed at least one time.
Ex: do-while.
Entry & Exit Controlled Loops
While Loop
While Loop
• The while is an entry controlled loop statement.
• The test condition is evaluated and if the
condition is true, then the body of the loop will be
executed.
• After execution of the body, the test-condition is
once again evaluated and if it is true, the body
will be executed once again.
• This process of repeated execution of the body
continues until the test condition finally becomes
false and the control is transferred out of the loop.
• On exit, the program continues with the statement
immediately after the body of the loop.
While Example
void main()
{
int i=0;
//Initialization
while(i<5)
//Testing
{
printf(“%d\n”, i);
i=i+1;
//Incrementing
}
}
Output:
0
1
2
3
4
While Example
void main()
{
int a=10;
while(a != 0)
{
printf(“%d\t”,a);
a- -;
}
}
Output:
10 9 8 7 6 5 4 3 2 1
Program to find sum of first N natural numbers
void main()
{
int sum=0,n,i=1; //Initialization
printf(“Enter number of terms:”);
scanf(“%d”,&n);
while(i<=n)
//Testing
{
sum=sum+i;
i++;
//Incrementing
}
printf(“sum=%d\n”, sum);
}
Output:
Enter number of terms:5
Sum= 15
Program to evaluate the equation y=xn
(Where n is a non-negative integer)
void main()
{
int n,count=1;
//Initialization
float x, y=1.0;
printf(“\nEnter the value of x & n:”);
scanf(“%f%d”,&x,&n);
while(count<=n)
//Testing
{
y=y*x;
count++;
//Incrementing
}
printf(“\n %f to power %d=%f”, x,n,sum);
}
Output:
Enter the value of x & n:2.0 5
2.0 to power 5= 32.0
While
• The body of the loop may have one or more
statements.
• The braces are needed only if the body contains
two or more statements.
• However, it is a good practice to use braces even
if the body has only one statement.
• Initialization,
Test
condition
and
increment/decrement steps are on different line.
• While(1) is used for infinite loop.
do-while
• It is exit controlled loop.
• On some occasions it might be
necessary to execute the body of the
loop before the test is performed.
• Such situations can be handled with
the help of the do-while statement.
• It is also called as bottom tested
loop(Condition is tested at bottom
and body has to execute atleast once)
Do…While
do-while Example
void main()
{
int i=0;
//Initialization
do
{
printf(“%d\n”, i);
i=i+1;
//Incrementing
} while(i<5);
//Testing
}
Output:
0
1
2
3
4
Program to find gcd of two numbers
void main()
{
int a,b,t;
printf(“\nEnter two numbers:”);
scanf(“%d%d”,&a,&b);
t=a%b;
while(t!=0)
{
a=b;
b=t;
t=a%b;
}
printf(“The gcd is %d”,b);
}
Output:
Enter two numbers:60 24
The gcd is 12
Program to generate required number of terms of fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, _ _ _ _ _
void main()
{
int t1=0,t2=1,t3,n,count=3;
printf(“Enter number of terms:”);
scanf(“%d”,&n);
printf(“%d %d”, t1,t2);
do
{
t3=t1+t2;
printf(“%d”,t3);
t1=t2;
t2=t3;
count++;
} while(count<n);
}
Output:
Enter number of terms:5
0 1 1 2 3
while loop
1. Condition is tested at the
beginning of the loop
do-while loop
1.Condition is tested at the
end of the loop
2. Top tested loop
2. Bottom tested loop
3. Minimum iteration is zero 3.Minimum iteration is one
4. Not flexible when
4.Not flexible when
compared with for loop
compared with for loop
5. Entry controlled loop
5. Exit controlled loop
For Loop
For Loop
For Loop
For Loop
For Loop
Additional features of for loop
 The for loop in C has several capabilities that are not found in other loop
constructs.
1. More than one variable can be initialized at a time in the for statement
for(p=1,n=0;n<17; n++)
2. Like the initialization section, the increment section may also have more
than one part.
for(n=1,m=50;n<=m;n=n+1,m=m+1)
{
p=m/n;
printf(“%d %d %d\n”,n,m,p);
}
3. The test condition may have any compound relation.
sum=0;
for(i=1;i<20 && sum<100; i++)
{
sum=sum+I;
printf(“%d %d\n”,i,sum);
}
Additional features of for loop
4.
It is also permissible to use expressions in the assignment statements
of initialization and increment section.
for(x=(m+n)/2: x>0; x=x/2)
5.
6.
7.
One or more sections can be omitted, if necessary.
m=5;
for( ;m!=100;)
{
printf(“%d”,m);
m=m+5;
}
If the test condition is not present, the for loop sets up an infinite loop.
We can set up time delay loops using the null statement as follows:
for(j=1000;j>0; j=j-1)
;
For Loop
void main()
{
int i=0;
for(i=0;i<5;i++)
{
printf(“%d\n”,i);
}
}
Output:
0
1
2
3
4
Program to find Factorial of a given number
void main()
{
int i, fact=1,n;
printf(“Enter a number:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf(“Factorial of %d=%d”,n,fact);
}
Output:
Enter a number: 5
Factorial of 5=120
Program to find factors of an integer using for loop
void main()
{
int no, x;
printf("Enter one number:");
scanf("%d",&no);
printf("\nThe factors are:");
for(x=1; x<=no; x++)
{
if(no%x==0)
printf("\n%d",x);
}
}
Output:
Enter one number:15
1 3 5 15
Program to find multiplication table up to 10
void main()
{
int n, i;
printf(“\nEnter one integer:");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf(“\n%d*%d=%d", n,i,n*i);
}
}
Output:
Enter one number:15
15*1=15
15*2=30
15*3=45
15*4=60
15*5=75
15*6=90
15*7=105
15*8=120
15*9=135
15*10=150
Unconditional Control Statements
(Jumping Statements)
• Loops perform a set of operations repeatedly until
the control variable fails to satisfy the testcondition.
• Sometimes, when executing a loop, it becomes
desirable to skip a part of the loop or to leave the
loop as soon as certain condition occur.
• C permits a jump from one statement to another
within a loop as well as a jump out of a loop.
Break Statement
 It is a jump instruction and can be used inside the
switch and loop statements(while, do-while & for).
 The execution of the break statements causes the
control transfer to the statement immediately after
the loop.
 When the loops are nested, the break would only
exit from the loop in which it is placed.
 The break will exit only a single loop.
Break Statement
Break Statement
Break statement
void main()
{
int i=0;
for(i=0;i<5;i++)
{
printf(“%d\n”,i);
if(i==2)
break;
}
}
Output:
0
1
2
Continue Statement
 It is a jump statement.
 It is used only inside the loop.
 Its execution does not exit from the loop but escape
the loop for that iteration and transfer the control
back to the loop for the new iteration.
 It is used for skipping a part of loop.
 Continue causes the remaining code inside a loop
to be skipped and causes execution to jump to the
top of the loop.
Continue Statement
Continue Statement
Continue statement
void main()
{
int i=0;
for(i=0;i<5;i++)
{
if(i==2)
continue;
printf(“%d\n”,i);
}
}
Output:
0
1
3
4
Program to print only even numbers upto a given range using Continue
void main ()
{
int i,n;
printf ("Enter the value of n :");
scanf ("%d",&n);
for (i=1; i<=n; i++)
{
if (i%2! = 0)
continue;
printf ("%d \n",i);
}
}
Output:
Enter the value of n:10
2
4
6
8
10
Goto Statement
 It is used to alter the normal sequence of flow by
transferring the control to some other part of the
program unconditionally.
 It is followed by the label statement which determine
the instruction to be handled next after the execution
of the goto statement.
Goto Statement
• goto statement can
transfer the control to
any place in a program.
• It is useful to provide
branching within a loop.
• It is also used to exit
from deeply nested
loops when an error
occurs.
Goto Statement
Forward jump
Backward jump
goto statement
void main()
{
int i=0;
for(i=0;i<5;i++)
{
if(i==2)
goto next;
printf(“%d\n”,i);
}
next:
printf(“We are in the label”);
}
Output:
0
1
We are in the label
Avoiding goto
• It is a good practice to avoid using goto.
• When goto is used, many compilers generate a less
efficient code.
• In addition, using many goto statements makes a
program logic complicated and renders the program
unreadable.
• It is possible to avoid using goto by careful program
design.
• In case any goto is absolutely necessary, it should be
documented.
BREAK
CONTINUE
1. Exits from the current block of loop.
1. Loop takes next iteration.
2. Control passes to the next
statement.
3. Terminates the program.
2. Control passes to the beginning of
the loop.
3. Never terminates the program.
4. Keyword is break.
4. Keyword is continue.
5. Without checking loop condition, it
terminates the entire loop.
5. After checking the loop condition, it
goes to next iteration.
6. break statement is used in both loops 6. continue statement is used in loops
and switch case statement.
only. It can’t be used in switch case
statement.
Thank you!!!
Download