0% found this document useful (0 votes)
22 views8 pages

LM - Unit4 2

Uploaded by

raisinghdaksh2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
22 views8 pages

LM - Unit4 2

Uploaded by

raisinghdaksh2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

UNIT-4: Iterative statements

Use of goto statement for iteration


The goto statement is used to alter the normal sequence of program instructions by transferring the
control to some other portion of the program.
syntax:
label:
//some part of the code;
goto label;

It may be Forward jump and backward jump

Although goto statement is used to alter the normal sequence of program execution but its usage in
the program should be avoided.
The most common applications are:
1. To branch around statements under certain conditions in place of use of ifelse statement,
2. To jump to the end of the loop under certain conditions bypassing the rest of statements inside
the loop in place of continue statement,
3. To jump out of the loop avoiding the use of break statement. goto can never be used to jump
into the loop from outside and it should be preferably used for forward jump.

Example
Write a program to print first 10 even numbers
/* Program to print 10 even numbers */
#include <stdio.h>
void main()
{
int i=2;
while(1)
{
printf(“%d ”,i);
i=i+2;
if (i>=20)
goto outside;
}
outside : printf(“over”);
}
OUTPUT
2 4 6 8 10 12 14 16 18 20 over
Note: here while(1), it generate infinite loop because 1 means always true.

while loop
When in a program a single statement or a certain group of statements are to be executed repeatedly
depending upon certain test condition, then while statement is used.
syntax:
while (test condition)
{
body_of_the_loop;
}
Here, test condition is an expression that controls how long the loop keeps running.

Body of the loop is a statement or group of statements enclosed in braces and are repeatedly executed
till the value of test condition evaluates to true. As soon as the condition evaluates to false, the control
jumps to the first statement following the while statement. If condition initially itself is false, the body
of the loop will never be executed. While loop is sometimes called as entry-control loop, as it controls
the execution of the body of the loop depending upon the value of the test condition.
Entry

Condition?

True false

Body of loop
Example
exit
Write a program to calculate the factorial of a given input natural number.
/* Program to calculate factorial of given number */
#include <stdio.h>
#include <math.h>
#include <stdio.h>
main( )
{
int x;
long int fact = 1;
printf(“Enter any number to find factorial:\n”); /*read the number*/
scanf(“%d”,&x);
while (x > 0)
{
fact = fact * x; /* factorial calculation*/
x=x-1;
}
printf(“Factorial is %ld”,fact);
}
OUTPUT
Enter any number to find factorial:
4
Factorial is 24

Here, condition in while loop is evaluated and body of loop is repeated until condition
evaluates to false i.e., when x becomes zero. Then the control is jumped to first statement following
while loop and print the value of factorial.

do..while loop
There is another loop control structure which is very similar to the while statement called as the do..
while statement. The only difference is that the expression which determines whether to carry on
looping is evaluated at the end of each loop.
Syntax:
do
{
statement(s);
} while(test condition);

In do-while loop, the body of loop is executed at least once before the condition is evaluated.
Then the loop repeats body as long as condition is true. However, in while loop, the statement doesn’t
execute the body of the loop even once, if condition is false. That is why do-while loop is also called
exit-control loop.

entry
Body of the
loop

true Conditio
false n?

exit

Example
Write a program to print first ten even natural numbers.
/* Program to print first ten even natural numbers */
#include <stdio.h>
main()
{
int i=0;
int j=2;
do
{
printf(“%d”,j);
j =j+2;
i=i+1;
} while (i<10);
}
OUTPUT
2 4 6 8 10 12 14 16 18 20
Difference between while and do…while
While Do…while
It is known as entry control loop It is known as exit control loop
It checks condition first and if condition is Body of loop executes once (whether
true it executes the body of loop condition is true or false) and then after
condition is checked.
It not ends with semicolon (;) It ends with semicolon (;)

for loop
for statement makes it more convenient to count iterations of a loop and works well where the number
of iterations of the loop is known before the loop is entered.
syntax:
for (initialization; test condition; increment or decrement)
{
Statement(s);
}

The main purpose is to repeat statement while condition remains true, like the while loop. But
in addition, for provides places to specify an initialization instruction and an increment or decrement
of the control variable instruction. So this loop is specially designed to perform a repetitive action with
a counter.

The for loop as shown in figure, works in the following manner:


1. Initialization is executed. Generally it is an initial value setting for a counter variable. This is executed
only once.
2. Condition is checked, if it is true the loop continues, otherwise the loop finishes and statement is
skipped.
3. Statement(s) is/are executed. As usual, it can be either a single instruction or a block of instructions
enclosed within curly brackets { }.
4. Finally, whatever is specified in the increment or decrement of the control variable field is executed
and the loop gets back to step 2.
Entry

Initialization

Condition?

true

Body of the loop false

Increment/decrement exit

Example
Write a program to print first n natural numbers.
/* Program to print first n natural numbers */
#include<stdio.h>
void main( )
{
int i,n;
printf(“Enter value of n \n”);
scanf(“%d”,&n);
printf(“\nThe first %d natural numbers are :\n”, n);
for (i=1;i<=n;++i)
{
printf(“%d”,i);
}
}

OUTPUT
Enter value of n
6
The first 6 natural numbers are:
123456

The three statements inside the braces of a for loop usually meant for one activity each, however any
of them can be left blank also. More than one control variables can be initialized but should be
separated by comma.

Various cases of loop statements can be:


(a) for(;condition;increment/decrement)
body;
Blank first statement will mean no initialization.
(b) for (initialization;condition;)
body;
Blank last statement will mean no running increment/decrement.
(c) for (initialization;;increment/decrement)
body;
Blank second conditional statement means no test condition to control the exit from the loop.
So, in the absence of second statement, it is required to test the condition inside the loop
otherwise it results in an infinite loop where the control never exits from the loop.
(d) for (;;increment/decrement)
body;
Initialization is required to be done before the loop and test condition is checked inside the
loop.
(e) for (initialization;;)
body;
Test condition and control variable increment/decrement is to be done inside the body of the
loop.
(f) for (;condition;)
body;
Initialization is required to be done before the loop and control variable increment/decrement
is to be done inside the body of the loop.
(g) for ( ; ; )(infinite for loop)
Body;
Initialization is required to be done before the loop, test condition and control variable
increment/decrement is to be done inside the body of the loop.

Nested while, do..while and for loops


C allows loops to be nested, that is, one loop may be inside another. The program given below
illustrates the nesting of loops.

Example
Write a program to generate the following pattern given below:
1
12
123
1234
/* Program to print the pattern */
#include<stdio.h>
void main( )
{
int i,j;
for (i=1;i<=4;++i)
{
printf("%d\n",i);
for(j=1;j<=i;++j)
printf("%d\t",j);
}
}
Here, an inner for loop is written inside the outer for loop. For every value of i, j takes the value from
1 to i and then value of i is incremented and next iteration of outer loop starts ranging j value from 1
to i.

Jumping statement: (break and continue)


Sometimes, it is required to jump out of a loop irrespective of the conditional test value. Break
statement is used inside any loop to allow the control jump to the immediate statement following the
loop.
syntax
break;
When nested loops are used, then break jumps the control from the loop where it has been
used. Break statement can be used inside any loop i.e., while, do-while, for and also in switch
statement.

Example
Write a program to calculate the first smallest divisor of a number.
/*Program to calculate smallest divisor of a number */
#include <stdio.h>
main( )
{
int div,num,i;
printf(“Enter any number:\n”);
scanf(“%d”,&num);
for (i=2;i<=num;++i)
{
if ((num % i) == 0)
{
printf(“Smallest divisor for number %d is %d”,num,i);
break;
}
}
}
OUTPUT
Enter any number:
9
Smallest divisor for number 9 is 3
In the above program, we divide the input number with the integer starting from 2 onwards,
and print the smallest divisor as soon as remainder comes out to be zero.
Since we are only interested in first smallest divisor and not all divisors of a given number, so
jump out of the for loop using break statement without further going for the next iteration of for loop.
Break is different from exit. Former jumps the control out of the loop while exit stops the
execution of the entire program.

Continue statement
Unlike break statement, which is used to jump the control out of the loop, it is sometimes required to
skip some part of the loop and to continue the execution with next loop iteration. Continue statement
used inside the loop helps to bypass the section of a loop and passes the control to the beginning of
the loop to continue the execution with the next loop iteration.
syntax
continue;

Example
Write a program to print first 20 natural numbers skipping the numbers divisible by 5.
/* Program to print first 20 natural numbers skipping the numbers divisible by 5 */
#include <stdio.h>
main( )
{
int i;
for (i=1;i<=20;++i)
{
if ((i % 5) == 0)
continue;
printf(“%d ”,i);
}
}
OUTPUT
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19
Here, the printf statement is bypassed each time when value stored in i is divisible by 5.

Null statement
A null statement consisting of only a semicolon and performs no operations.
Example:
# include<conio.h>
#include <stdio.h>
void main( )
{
int i
clrscr();
for (i=1;i<=5;i++)
{
if (i%2==0)
;
else
printf(“%d\n”,i);
}
getch();
}

You might also like