C Programming Looping Statements
C Programming Looping Statements
Looping Statements
Loops are very basic and very useful programming facility that facilitates programmer to execute any
number of lines repeatedly and can be controlled as per conditions added by programmer. It saves writing
code several times for same task. Used to repeat the execution of a list of statements depending on the value
of an integer expression.
a) For loop:
for loop in C is the most general looping construct. The loop header contains three parts. Basic syntax to
use ‘for’ loop is:
When for statement is encountered an initialization statement is executed. This process is done only
once during the execution of for statement. When condition is evaluated, if it is true the body of the loop
will be executed otherwise the body of the loop will be skipped. If more than one statement has to be
executed it should end with a pair of braces. The condition of for loop is executed each time at the beginning
of the loop. After executing the body of the loop the increment of variable is executed, again the condition
is executed. If the condition become false it exit from the loop and control transferred to the statement
followed by the loop.
The initialization need not be contained to a single variable. If more than one variable is used for
initialization they are separated by commas. Increment of variable can also applied to more than one
variable separated by commas.
1
Example1: Implement a Program to display numbers from 1 to n.
Or Program to print first ‘n’ natural numbers using for loop
Note: First n natural numbers means it should start from 1 and print up to value stored in n.
}
Output:
Enter value
5
1 2 3 4 5
2. Program to find sum of n natural numbers or Implement a program to add the numbers from 1 to n
#include <stdio.h>
int main()
{
int n, i, sum=0;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1;i<=n;i++) /* for loop terminates if count>n */
{
sum+=i; /* sum=sum+count */
}
printf("Sum = %d",sum);
return 0;
}
Output:
Enter an integer: 9
sum =45
b) While Loop:
It is a looping statement, provides a mechanism to repeat one or more statements while a particular
condition is true. Basic syntax to use ‘while’ loop is:
initialization;
while (condition)
{
statement 1;
statement 2;
..
..
Increment ;
}
2
Here, condition is tested before any of the statements executes.
If condition is true then statements in the block will be executed.
If the condition is false the block of statements within the body of loop is skipped and the loop
execution get terminated with the control passing to the statement immediately following the while
construct.
It is a Pre-test loop
It is also referred as top-checking loop or entry controlled loop
The minimum number of times statements inside the block will be executed is zero (0).
Design and develop a C program to reverse of an integer NUM and check whether it is PALINDROME or
NOT. Or
Answer: A palindrome number is a number such that if we reverse it, it will not change. For example some
palindrome numbers examples are 121, 212, 12321, 454. To check whether a number is palindrome or not first we
reverse it and then compare the number obtained with the original, if both are same then number is palindrome
otherwise not.
PROGRAM:
#include<stdio.h>
main()
{
int n, rev = 0, temp, rem;
printf("Enter a number ");
scanf("%d",&n);
temp = n;
while( n != 0 )
{
rem = n%10;
rev = rev * 10 + rem;
n = n/10;
printf("reverse of a given number is %d \n", rev);
}
if ( temp == rev )
printf("%d is a palindrome number.\n", temp);
else
printf("%d is not a palindrome number.\n", temp);
}
Output
Enter a number 121
reverse of a given number is 121
121 is a palindrome number
3
c) Do while loop:
In C, do...while loop is very similar to while loop. Only difference between these two loops is that,
in while loops, test expression is checked at first but, in do...while loop code is executed at first then the
condition is checked. So, the code are executed at least once in do...while loops.
Syntax of do while:
do
{
Statement 1;
Statement 2;
…………...
Statement n;
}
while(condition);
Here the block of statement following the do is executed without any condition check. After this expression
is evaluated and if it is true the block of statement in the body of the loop is executed again. Thus the block
of statement is repeatedly executed till the expression is evaluated to false.
do-while construct is not used as often as the while loops or for loops in normal case of iteration
but there are situation where a loop is to be executed at least one, in such cases this construction is very
useful.
It is a Post-test loop
It is also referred as bottom-checking loop or exit controlled loop.
The minimum number of times statements inside the block will be executed is one (1).
Here, the entry to the loop is automatic.
Example: Implement a C program to reverse and find sum of digits of the number using Do While Loop.
#include<stdio.h>
main()
{
int n, rev = 0, sum=0,temp, rem;
printf("Enter a number ");
scanf("%d",&n);
temp = n;
do
{
rem = n%10;
rev = rev * 10 + rem;
n = n/10;
sum=sum+rem;
} while( n != 0 );
printf("reverse of a given number is %d \n", rev);
printf("sum of digits is %d", sum);
}
4
Output
Enter a number 121
reverse of a given number is 121
sum of digits is 4
#include <stdio.h>
main()
{
int i, n;
Output:
Print all even numbers till: 25
All even numbers from 1 to 25 are:
2 4 6 8 10 12 14 16 18 20 22 24
5
Explain nested looping
C programming allows to use one loop inside another loop. In many cases we may use loop
statement inside another looping statement. This type of looping is called nested loop. In nested loop the
inner loop is executed first and then outer loop.
The syntax for a nested while loop statement in C programming language is as follows −
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
The syntax for a nested do...while loop statement in C programming language is as follows −
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );
PROGRAM:
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows\n");
scanf("%d",&n);
for ( i = 1 ; i <= n ; i++ )
{
for( j= 1 ; j <= i ; j++ )
printf("*");
printf("\n");
6
}
return 0;
}
PROGRAM:
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows\n");
scanf("%d",&n);
PROGRAM:
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows\n");
scanf("%d",&n);
7
UNCONDITIONAL BRANCHING STATEMENTS / Jumps in Loops
1.BREAK:
Break statement is jump statement which can be used in switch and looping statements.
The break statement in switch statement causes control to terminate switch statements and
statements following switch statement will be executed. Break statement will be the last statement.
If Break statement is used in loops (for, while, do while), the control comes out of the loop and the
statements following the loop will be executed. It is used to terminate the loop when a specific
condition is reached.
Syntax: Break;
If break statement appears in the inner loops of a nested loop, then control will comes out of the
inner loop.
Example:;
main()
{
Int i;
for(i=0;i<=5;i++)
{
if(i==4)
break;
printf(“%d”,i);
}
}
Output: 0 1 2 3
8
2. CONTINUE STATEMENTS
During execution of a loop, it may be necessary to skip a part of the loop based on some conditions.
In such cases, we use continue statement. The continue statement is used only in the loops to terminate the
current iteration.
Ex: during processing of student records, it may be necessary to exclude student names whose marks
are less than or equal 50. In such case, in a program we need to check the marks whether it is less than or
equal to 50. In such case program has to skip such student details by using continue statement.
Example:
main()
{
Int i;
for(i=0;i<=5;i++)
{
if(i==4)
continue;
printf(“%d”,i);
}}
Output: 0 1 2 3 5
3.GOTO STATEMENT:
It is a jump statements that transfers the control to the specified statement in a program. i.e Used to
transfer control to a specified label.
Label: is an identifier that specifies the place where the branch is to be made. Label can be any
valid variable name followed by a colon (:).
Whenever the goto statement is encountered, the control is immediately transferred to statements
following label.
Goto statement is often combined with if statements.
Label:
.
Syntax: .
.
.
.
goto label;
-------- Statements;
-------- --------
--------
Label:
Goto label
Statements;
Forward jump backward jump
9
Basic programming examples using all the looping constructs:
1.Design a C program to find the factorial of a number, where the number is entered by the user.
Note: If the question has design then we have to write the algorithm or flowchart with program.
ALGORITHM:
Step1: Start
Step2: fact1.
Step3: Read the number n.
Step4: For in Repeat step 5 and 6 till i>0.
Step5: Find factfact*i
Step6: Decrement ii-1
Step7: Then print fact.
Step8: Stop
PROGRAM:
#include<stdio.h>
main()
{
int n, i, fact=1;
printf("Enter a number:");
scanf("%d",&n);
for(i=n;i >0;i- -)
{
fact= fact*i;
}
printf("Factorial of %d=%d\n",n,fact);
}
OUTPUT: Enter a number:5
Factorial of 5 = 120
PROGRAM:
#include <stdio.h>
main()
{
int n, i, count=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
count=1;
break;
}
}
if (count==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
}
10
OUTPUT: Enter a positive integer: 29
29 s a prime number.
Step1: Start
Step2: Read n as number of terms
Step3: initialize first0,second1
Step4: for[i=0 to i<n]
If(i<=1)
Next1
End IF
Else
Nextfirst+second
Firstsecond
Second next
Step5: Print Next
Step6: Stop
PROGRAM:
# include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, i;
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
11