Iteration Control Statement: Looping(while, do while , for nested)
Looping in C
Looping (while, do while and for)
Looping is the process of executing the same program statement or block of program statements repeatedly for a specified number of times or till the given condition is satisfied. Loop structure is used to carry out looping.
For example, if we want to display "C is the best" 10 times, one way to get the desired output is - we type printf("C is the best"); 10 times, which is time consuming hence not preferable. Another way to perform this is - use of loop structure. With loop structure we don't need to type the same program statement again and again. Loop structure is also known as repetitive control structure or iterative control structure
POINTS TO REMEMBER
A control structure that executes the same program statement or block of program statements repeatedly for a specified number of times or till the given condition is satisfied is called Loop.
Mainly there are three types of loop:
while Loop
do while Loop
for Loop
while Loop
It executes the program statements repeatedly until the given condition is true. It checks the condition at first; if it is found true then it executes the statements written in its body part otherwise it just gets out of the loop structure. It is also known as entry control or pre-test loop.
Syntax of while Loop: initialization; while(condition) { statements; increment/decrement; } Where initialization means starting point, control means stopping points and increment/decrement means counter. | Flowchart of while Loop: |
Program 13 Write a program to display “C is the best” 10 times using a while loop.
#include<stdio.h>
int main()
{
int i=0;
while(i<10)
{
printf("C is the best\n");
i++;
}
return 0;
}
Program 14 Write a program to display numbers from 1 to 10.
#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
printf("%d\n",i);
i++;
}
return 0;
}
Program 15 Write a program to calculate and display sum of the numbers from 1 to 10.
#include<stdio.h>
int main()
{
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
}
printf("Sum of numbers between 1 to 10 is %d\n",sum);
return 0;
}
Assignment 16
Write programs using while loop to:
Display your name 10 times on screen
Display the series: 10 9 8 ….. to 1.
Calculate and display sum of odd natural numbers up to n.
do while Loop
It also executes program statements repeatedly until the given condition is true. It executes the program statements once at first then only condition is checked. If a condition is found true then it executes the program statements again, otherwise it gets out from the loop structure. As it checks the condition at last it is also known as the post-test loop or exit control loop.
Syntax of do while Loop: initialization; do { statements; increment/decrement; }while(condition); | Flowchart of do while Loop: |
Program 16
Write a program to display the series: 1 6 11 16…. 101.
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("%d\t",i);
i=i+5;
}while(i<=101);
return 0;
}
Program 17
Write a program to display the series: 5 9 13… up to 10th terms.
#include<stdio.h>
int main()
{
int i=1,a=5;
do
{
printf("%d\t",a);
a=a+4;
i++;
}while(i<=10);
return 0;
}
Program 18
Write a program to display a multiplication table of 6.
#include<stdio.h>
int main()
{
int i=1,ans;
do
{
ans=6*i;
printf("6*%d=%d\n",i,ans);
i++;
}while(i<=10);
return 0;
}
Assignment 17
Write programs using do while loop to:
Calculate and display the series 1 8 27 …. to 10th terms
Display the series: 15 9 ……. up to 20th terms
Display the multiplication table of any number given by the user.
Difference between while and do while loop
while loop | do while loop |
In the while loop, condition is checked in the beginning. | In the do while loop, condition is checked at the end. |
It is also known as a pre-test or entry control loop. | It is also known as post-test or exit control loop. |
It is not terminated with a semicolon. | It is terminated with a semicolon. |
In the while loop, statements are not executed if the condition is false. | In the do while loop, statements are executed once even the condition is false. |
It uses the keyword ‘while’. | It uses two keywords ‘do’ and ‘while’. |
The syntax of while loop is as follows: initialization; while(condition) { statements; increment/decrement; } | The syntax of do-while loop is as follows: initialization; do { statements; increment/decrement; }while(condition); |
The operation of while loop can be represented using flowchart as follows: | The operation of do while loop can be represented using flowchart as follows: |
Example: int main() { int i=1; while(i>=10) { printf("I love my country"); i++; } return 0; } Output: This program displays nothing as output; as condition is evaluated false in the beginning. | Example: #include<stdio.h> int main() { int i=1; do { printf("I love my country"); i++; }while(i>=10); return 0; } Output: This program displays “I love my country” as output at once. |
for loop
It is the most common type of loop which is used to execute a program statement or block of program statements repeatedly for a specified number of times. It is a definite loop. Mainly it consists of three expressions: initialization, condition and increment / decrement. The initialization defines the loop starting point, condition defines the loop stopping points and counter helps to increment and decrement the value of counter variable.
Syntax of for Loop: for(initialization;condition;increment/decrement ) { statements; } | Flowchart of for Loop: |
Program 19 Write a program to calculate and display the value of y raised to power x. (z=yx).
#include<stdio.h>
int main()
{
int x,y,i,z=1;
printf("Enter the value of x and y");
scanf("%d%d",&x,&y);
for(i=1;i<=x;i++)
{
z=z*y;
}
printf("%d raised to %d = %d",y,x,z);
return 0;
}
Program 20 Write a program to calculate and display factorial of 5.
[The product of a given positive integer multiplied by all lesser positive integers: The quantity five factorial (5!) = 5x4 x 3 x 2 x 1 = 120.]
#include<stdio.h>
int main()
{
int i,f=1;
for(i=5;i>=1;i--)
{
f=f*i;
}
printf("Factorial of 5 = %d",f);
return 0;
}
Program 21 Write a program to display 1 to 10 and respective factorials.
#include<stdio.h>
int main()
{
int i,f=1;
for(i=1;i<=10;i++)
{
f=f*i;
printf("Factorial of %d = %d\n",i,f);
}
return 0;
}
Nested Loop
We can write an entire loop structure inside another loop structure. So a loop inside another loop is called a nested loop.
Syntax:
Nested for loop:
for(initialization; condition ; increment/decrement)
{
for(initialization; condition ; increment/decrement)
{
statements;
}
}
Program 22 Write a program to display the following:
1
12
123
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}
Algorithm to solve nested loop
Step 1: Write Outer Loop by making pattern of change value
Step 2: Write inner loop
Step 3: Print matter
3.1 Horizontal change- Inner Loop
3.2 Horizontal not change- Outer Loop
3.3 Everywhere same (*)- Print the repeating item
Step 4: Print New Line - after inner loop
Nested Program-1 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 | Hint: 1-1 1-2 1-3 1-4 1-5 | Answer: #include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(" %d ",j); } printf("\n"); } return 0; } |
Nested Program-2 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 | Hint: 1-1 1-2 1-3 1-4 1-5 | Answer: #include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(" %d ",i); } printf("\n"); } return 0; } |
Nested Program-3 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1 | Hint: 5-5 5-4 5-3 5-2 5-1 | Answer: #include <stdio.h> int main() { int i,j; for(i=5;i>=1;i--) { for( j=5;j>=i;j--) { printf(" %d ",j); } printf("\n"); } return 0; } |
Nested Program-4 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1 | Hint: 5-5 5-4 5-3 5-2 5-1 | Answer: #include <stdio.h> int main() { int i,j; for(i=5;i>=1;i--) { for( j=5;j>=i;j--) { printf(" %d ",i); } printf("\n"); } return 0; } |
Nested Program-5 * * * * * * * * * * * * * * * | Hint: 1-1 1-2 1-3 1-4 1-5 | Answer: #include <stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(" * "); } printf("\n"); } return 0; } |
Nested Program-6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Hint: 1-1 1-2 1-3 1-4 1-5 | Answer: #include <stdio.h> int main() { int i,j,p=1; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(" %3d ",p); p=p+1; } printf("\n"); } return 0; } |
Nested Program-7 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | Hint: 1-1 1-2 1-3 1-4 1-5 | Answer: #include <stdio.h> int main() { int i,j,p=2; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(" %3d ",p); p=p+2; } printf("\n"); } return 0; } |
Nested Program-8 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 | Hint: 1-5 1-4 1-3 1-2 1-1 | Answer: #include<stdio.h> int main() { int i,j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d ",j); } printf("\n"); } return 0; } |
Nested Program-9 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1 | Hint: 1-5 1-4 1-3 1-2 1-1 | Answer: #include<stdio.h> int main() { int i,j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%d ",i); } printf("\n"); } return 0; } |
Nested Program-10 5 4 3 2 1 5 4 3 2 5 4 3 5 4 5 | Hint: 5-1 5-2 5-3 5-4 5-5 | Answer: #include<stdio.h> int main() { int i,j; for(i=1;i<=5;i++) { for(j=5;j>=i;j--) { printf("%d ",j); } printf("\n"); } return 0; } |
Nested Program-11 * * * * * * * * * * * * * * * | Hint: 1-5 1-4 1-3 1-2 1-1 | Answer: #include<stdio.h> int main() { int i,j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf(" * "); } printf("\n"); } return 0; } |
Nested Program-12 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 | Hint: 1-5 1-4 1-3 1-2 1-1 | Answer: #include<stdio.h> int main() { int i,j,p=15; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { printf("%5d",p); p=p-1; } printf("\n"); } return 0; } |
Nested Program-13 1 2 3 2 3 4 3 4 5 4 5 6 5 6 7 | Hint: 1-3 2-4 3-5 4-6 5-7 | Answer: #include <stdio.h> int main() { int i,j,p=0; for(i=1;i<=5;i++) { for(j=0;j<=2;j++) { printf(" %d ",i+j); } printf("\n"); } return 0; } |
Nested Program-14 1 2 3...N 2 4 6...2N 3 6 9 ….3N ----- N 2N 3N…...NN | Hint: 1- N 2-2N 3-3N ---- N-NN | Answer: #include <stdio.h> int main() { int i,j,n; printf("Enter the value for n: "); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf(" %3d ",i*j); } printf("\n"); } return 0; } |
Infinite Loop
A loop which never terminates is called infinite loop that is it executes the program statements repeatedly which doesn’t meet any ending point.
Example of infinite loop:
#include<stdio.h>
int main()
{
int i;
for(i=1;i>=0;i++)
{
printf("%d",i);
}
return 0;
}
Jumping Statement
Jumping statements are particularly used to jump execution of program statements from one place to another place inside a program. These statements may execute the same program statement repeatedly or skip some program statements. Following are the jumping statements defined in C programming language.
break
continue
goto
break statement
As its name implies, it is used to break the normal flow of program statement execution in loop and switch case statement. It allows us to exit from the innermost enclosing loop or switch statement as soon as certain condition is satisfied.
When ‘break’ is encountered in the program (loop body/switch statement), the remaining part of the loop/switch statement is skipped and control will be passed to the next statement after loop; terminating the loop/switch statement. For example, while searching a number in a set of 100 numbers; when the required number is found it is obvious to get terminated from the loop. In such cases a break statement is used to do so.
Example of break statement:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==4)
break;
printf(" %d\t ",i);
}
return 0;
}
When the value of i becomes 4 then break is encountered and control is passed to outside the loop structure.
The output of this program is 1 2 3
continue statement
As its name implies, it is used to continue the normal flow of program statement execution in loop; skipping particular iteration in the loop as soon as certain condition is satisfied. When 'continue' is encountered in the program (loop body) then that particular iteration is skipped and the loop will be continued with the next iteration.
Example of continue statement:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==4)
continue;
printf(" %d\t ",i);
}
return 0;
}
When the value of i becomes 4, 'continue' is encountered then that iteration for which i equals 4 is skipped and it continues with i equals 5.
Hence, the output of this program is 1 2 3 5
Differences between break and continue statement break statement
break statement | continue statement |
(i) When a break statement is encountered the entire loop or switch statement is terminated | (i) When continue statement is encountered the entire loop is not terminated; only that particular iteration is skipped |
(ii) It is used with loop and switch case statements. | (ii) It is used only with loop structure. |
(iii) It uses a keyword break. | (iii) It uses a keyword continue. |
(iv) Example: #include <stdio.h> int main() { int i; for(i=1;i<=5;i++) { if(i==4) break; printf(" %d\t ",i); } return 0; } Output: 1 2 3 | (iv) Example: #include <stdio.h> int main() { int i; for(i=1;i<=5;i++) { if(i==4) continue; printf(" %d\t ",i); } return 0; } Output: 1 2 3 5 |
goto statement
When a goto statement is encountered in a program then it transfers the control of the program statements execution unconditionally to the location specified by the goto statement within a current function.
Syntax
goto label;
where the label is an identifier that is used to label the target statement to which the control is transferred. Control may be transferred to anywhere within the current function. The target statement must be labeled, and a colon must follow the label. Thus the target statement will appear as
label: statement;
Each labeled statement within the function must have a unique label, that is, no two statements can have the same label.
Program 24 Write a program to display numbers from 1 to 10 using goto statement.
#include <stdio.h>
int main()
{
int i=1;
label1:
printf(" %d ",i);
i++;
if(i<=10)
goto label1;
return 0;
}
Assignment 18
Explain break, continue and goto statement with appropriate examples.
Write a program to display the following output:
3 3 3
2 2
1
Worked out Examples
Program 25 Write a program to display the following Fibonacci series:
1 1 2 3 5 ……. To nth terms
[An integer in the infinite sequence 0,1, 1, 2, 3, 5, 8, 13, … of which the first two terms are 0 and 1 and each succeeding term is the sum of the two immediately preceding is called Fibonacci Series.]
#include<stdio.h>
int main()
{
int a=1,b=1,c,n,i;
printf("Enter the value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\t",a);
c=a+b;
a=b;
b=c;
}
return 0;
}
Program 26 Write a program to calculate and display sum of digits present in the given number.[123=1+2+3=6]
#include<stdio.h>
int main()
{
int num,sum=0,r;
printf("Enter the value of num ");
scanf("%d",&num);
while(num!=0)
{
r=num%10;
sum=sum+r;
num=num/10;
}
printf("Sum of digits is %d",sum);
return 0;
}
Program 27 Write a program to check if the given number is palindrome or not. [ If the reverse of the given number is the same as number , then it is said to be palindrome. Example: 121 is palindrome number]
#include<stdio.h>
int main()
{
int num,sum=0,r,n;
printf("Enter the value of num ");
scanf("%d",&num);
n=num;
while(num!=0)
{
r=num%10;
sum=sum*10+r;
num=num/10;
}
if(n==sum)
printf(" %d is Palindrome number",n);
else
printf("%d is not palindrome",n);
return 0;
}
Program 28 Write a program to convert from decimal number system into binary number system.
#include <stdio.h>
int main()
{
int num,r,i=1,sum=0;
printf("Enter decimal number ");
scanf("%d",&num);
while(num!=0)
{
r=num%2;
sum=sum+r*i;
num=num/2;
i=i*10;
}
printf(" Binary Equivalent is %d ",sum);
return 0;
}
Program 29 Write a program to convert the given binary number into equivalent decimal number.
#include <stdio.h>
#include<math.h>
int main()
{
int num,r,i=0,sum=0;
printf("Enter Binary number ");
scanf("%d",&num);
while(num!=0)
{
r=num%10;
sum=sum+pow(2,i)*r;
num=num/10;
i++;
}
printf(" Decimal Equivalent is %d",sum);
return 0;
}
Program 30 Write a program to check whether the given number is prime or composite. [Prime numbers are those whole numbers which have only two factors i.e. one and itself. Example: 19 is prime number because it has only two factors 1 & 19]
#include <stdio.h>
int main()
{
int num,i,count=0;
printf("Enter any number ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf("%d is prime number",num);
else
printf("%d is composite number",num);
return 0;
}
Program 31 Write a program to display all prime numbers from 1 to 100.
#include <stdio.h>
int main()
{
int num,i,count;
for(num=1;num<=100;num++)
{
count=0;
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf(" %d ",num);
}
return 0;
}
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...