104 - Computer Programming & Programming Methodology (CPPM)
UNIT-4: Iterative statements :
Use of goto statement for iteration
The goto statement is known as jump statement in C. As the name suggests, goto is
used to transfer the program control to a predefined label. The goto statement can be
used to repeat some part of the code for a particular condition. It can also be used to
break the multiple loops which can't be done by using a single break statement.
However, using goto is avoided these days since it makes the program less readable and
complicated.
Syntax:
label:
//some part of the code;
goto label;
Example goto statements
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
getch();
}
Loops
The looping can be defined as repeating the same process multiple times until a specific
condition satisfies. There are three types of loops used in the C language. In this part of
the tutorial, we are going to learn all the aspects of C loops.
Why use loops in C language?
The looping simplifies the complex problems into the easy ones. It enables us to alter
the flow of the program so that instead of writing the same code again and again, we
can repeat the same code for a finite number of times. For example, if we need to print
the first 10 natural numbers then, instead of using the printf statement 10 times, we can
print inside a loop which runs up to 10 iterations.
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked
lists).
Types of C Loops
There are three types of loops in C language that is given below:
1) while loop
2) do….while loop
3) for loop
PROF. AMARDEEP R MAURYA 1
104 - Computer Programming & Programming Methodology (CPPM)
while loop
While loop is also known as a pre-tested loop. In general, a while loop allows a part of
the code to be executed multiple times depending upon a given boolean condition. It can
be viewed as a repeating if statement. The while loop is mostly used in the case where
the number of iterations is not known in advance.
The syntax of while loop in c language is given below:
while (testExpression)
{
// statements inside the body of the loop
// Increment (++) or Decrement (--) Operation
}
Flowchart of while loop in C How while loop works?
1. The while loop evaluates the test
expression inside the parenthesis ().
2. If the test expression is true,
statements inside the body of while
loop are executed. Then, the test
expression is evaluated again.
3. The process goes on until the test
expression is evaluated to false.
4. If the test expression is false, the
loop terminates (ends).
Example1 - while loop in C language
#include<stdio.h> Output:
#include<conio.h> 1
void main() 2
{ 3
int i=1; clrscr(); 4
while(i<=10) 5
{ 6
7
printf("%d \n",i);
8
i++;
9
} 10
getch();
}
Example 2
#include<stdio.h> Output:
#include<conio.h> 11
void main() 22
{ int i=1, j=1; clrscr(); 33
while (i <= 4 || j <= 3) 44
{ printf("%d %d\n",i, j);
i++;
j++;
}
getch();
}
PROF. AMARDEEP R MAURYA 2
104 - Computer Programming & Programming Methodology (CPPM)
do...while loop
The do while loop is a post tested loop. Using the do-while loop, we can repeat the
execution of several parts of the statements. The do-while loop is mainly used in the
case where we need to execute the loop at least once. The do-while loop is mostly used
in menu-driven programs where the termination condition depends upon the end user.
The syntax of the C language do-while loop is given below:
do
{
// statements inside the body of the loop
// Increment (++) or Decrement (--) Operation
}
while (testExpression);
Flowchart for do….while loop How do...while loop works?
1. The body of do...while loop is executed
once. Only then, the test expression is
evaluated.
2. If the test expression is true, the body
of the loop is executed again and the
test expression is evaluated.
3. This process goes on until the test
expression becomes false.
4. If the test expression is false, the loop
ends.
Example 1
#include<stdio.h> Output:
#include<conio.h> 1
void main() 2
{ 3
int i=1; clrscr(); 4
do 5
{ 6
printf("%d \n",i); 7
i++; 8
}while(i<=10); 9
getch(); 10
}
Example 2
#include<stdio.h> Output:
#include<conio.h> 5 10 15 20 25 30 35 40 45 50
void main()
{ int n=5, i=1; clrscr();
do
{
printf("%d\t", n*i);
i++;
}while(i <= 10);
getch();
}
PROF. AMARDEEP R MAURYA 3
104 - Computer Programming & Programming Methodology (CPPM)
for loop
The for loop in C language is used to iterate the statements or a part of the program
several times. It is frequently used to traverse the data structures like the array and
linked list. The syntax of for loop in c language is given below:
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}
How for loop works? Flowchart of for loop
1. The initialization statement is
executed only once.
2. Then, the test expression is
evaluated. If the test expression is
evaluated to false, the for loop is
terminated.
3. However, if the test expression is
evaluated to true, statements inside
the body of for loop are executed,
and the update expression is
updated(Increment/Decrement).
4. Again the test expression is
evaluated.
5. This process goes on until the test
expression is false. When the test
expression is false, the loop
terminates.
Example:
#include<stdio.h> Output:
#include<conio.h> 1
void main() 2
{ 3
int i; clrscr(); 4
for(i=1;i<=10;i++) 5
{ 6
printf("%d \n",i); 7
} 8
getch(); 9
} 10
Infinite loop with no conditions
Likewise, when the condition part is left blank, the C compiler will also treat it as true
and the loop will run infinite times.
for (i=0; ; i++)
{
printf("This is infinite loop");
}
for (;;)
{
printf("This is infinite loop");
}
PROF. AMARDEEP R MAURYA 4
104 - Computer Programming & Programming Methodology (CPPM)
Break statements in Infinite loop
We can use break statement to exit from an infinite loop. For example,
for (;;)
{
printf("This loop will run only once");
break;
}
Program: C program to find the sum of first n natural numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,sum=0; clrscr();
printf("Enter value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Sum = %d",sum);
getch(); return 0;
}
Nested while, do…while and for loops
➢ Nested loop means a loop statement inside another loop statement. That is why nested
loops are also called as “loop inside loop“.
➢ Any number of loops can be defined inside another loop, i.e., there is no restriction for
defining any number of loops.
➢ The nesting level can be defined at n times. You can define any type of loop inside
another loop; for example, you can define 'while' loop inside a 'for' loop.
Syntax of Nested loop
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-
while' loop.
Nested for loop :The nested for loop means any type of loop which is defined inside
the 'for' loop. Syntax:
for (initialization; condition; update)
{
for(initialization; condition; update)
{
// inner loop statements.
}
// outer loop statements.
}
PROF. AMARDEEP R MAURYA 5
104 - Computer Programming & Programming Methodology (CPPM)
Example of Nested for loop :
#include<stdio.h>
#include<conio.h>
int main()
{ int n=5;
clrscr();
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%d\t",(i*j));
}
printf("\n");
}
getch(); return 0;
}
Nested while loop
The nested while loop means any type of loop which is defined inside the 'while' loop.
while(condition)
{
while(condition)
{
// inner loop statements.
}
// outer loop statements.
}
Syntax for Nested Do-While loop:
The nested do…while loop means any type of loop which is defined inside the 'do...while'
loop.
do
{
do
{
// inner loop statements.
}while(condition);
// outer loop statements.
}while(condition);
Syntax: There can be any type of loop nested inside any type and to any level.
do
{
while(condition)
{
for ( initialization; condition; increment )
{
// statement of inside for loop
}
// statement of inside while loop
}
// statement of outer do-while loop
}while(condition);
PROF. AMARDEEP R MAURYA 6
104 - Computer Programming & Programming Methodology (CPPM)
Jumping statement: (break and continue)
C break : The break is a keyword in C which is used to bring the program control out
of the loop. The break statement is used inside loops or switch statement. The break
statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the
inner loop first and then proceeds to outer loops. The break statement in C can be
used in the following two scenarios:
1. With switch case
2. With loop
Its syntax is:
break;
The break statement is almost always used with if...else statement inside the loop.
Example of break Statement :
#include<stdio.h> Output:
#include<conio.h> 0 1 2 3 4 5 came outside of loop i = 5
void main ()
{
int i; clrscr();
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
getch();
}
Example : break statement with while loop
#include<stdio.h> Output :
#include<conio.h>
0 1 2 3 4 5 6 7 8 9 came out of
void main ()
{ int i = 0; clrscr(); while loop
while(1)
{
printf("%d ",i);
i++;
if(i == 10)
break;
}
printf("came out of while loop");
}
PROF. AMARDEEP R MAURYA 7
104 - Computer Programming & Programming Methodology (CPPM)
C continue : The continue statement in C language is used to bring the program
control to the beginning of the loop. The continue statement skips some lines of code
inside the loop and continues with the next iteration. It is mainly used for a condition
so that we can skip some code for a particular condition.
Its syntax is:
continue;
The continue statement is almost always used with the if...else statement.
How continue statement works?
Continue statement example1
#include<stdio.h> Output:
#include<conio.h> 1
void main() 2
{ int i=1;//initializing a local variable 3
clrscr();
4
//starting a loop from 1 to 10
for(i=1;i<=10;i++) 6
{ 7
if(i==5) 8
{ 9
continue; 10
}
printf("%d \n",i);
}//end of for loop
getch(); return 0;
}
Continue statement example2
#include<stdio.h> getch();
#include<conio.h> }
void main()
{ int k = 0; clrscr();
Output:
while( k !=10)
{ infinite loop
printf("%d", k); (Press ctrl+Break)
continue;
k++;
}
PROF. AMARDEEP R MAURYA 8