Loops in C
Loops in programming come into use when we need to
repeatedly execute a block of statements.
Example:
Suppose we want to print “Hello World” 10 times.
This can be done in two ways as shown below:
Iterative Method
An iterative method to do this is to write the printf() statement
10 times.
Using Loops
In Loop, the statement needs to be written only once and the loop
will be executed 10 times as shown below.
In computer programming, a loop is a sequence of instructions that is
repeated until a certain condition is reached.
An operation is done, such as getting an item of data and changing it,
and then some condition is checked such as whether a counter has
reached a prescribed number.
Using Loops
Counter not Reached :
If the counter has not reached the desired number, the
next instruction in the sequence returns to the first instruction in
the sequence and repeat it.
Counter reached :
If the condition has been reached, the next instruction
“falls through” to the next sequential instruction or branches outside
the loop..
Types of Loops
Entry Controlled loops :
In this type of loops the test condition is tested before entering
the loop body. For Loop and While Loop are entry controlled loops.
Exit Controlled Loops:
In this type of loops the test condition is tested or evaluated at
the end of loop body. Therefore, the loop body will execute atleast once,
irrespective of whether the test condition is true or false. do – while loop
is exit controlled loop.
While loop in C
Syntax :
while (test_expression)
{
// statements
update_expression;
}
The various parts of the While loop are :
Test Expression :
In this expression we have to test the condition.
If the condition evaluates to true then we will execute the body
of the loop and go to update expression.
Otherwise, we will exit from the while loop.
Iterative loop statements in C
while
do ... while
for
while :
The while loop in C is used in situations where we do not
know the exact number of iterations of loop beforehand.
The loop execution is terminated on the basis of the test condition.
Suppose, we want to print "He is the best" 10 times. One way to
get the desired output. We write 10 printf statements, which is not
preferable.
Other way out is - use loop.
While loop example
#include<stdio.h>
int main()
{
int i=0;
while(i<10)
{
printf("Hello world\n");
i++;
}
}
7
While loop example
#include<stdio.h>
int main()
{
int i=0;
/*
while(i++ < 5)
printf("i = %d\n",i);
*/
while(i++ < 5);
printf("i = %d\n",i);
}
8
While loop example
#include<stdio.h>
int main()
{
int i=0;
/*
while(++i < 5)
printf("i = %d\n",i);
*/
while(++i < 5);
printf("i = %d\n",i);
}
9
While loop example
#include<stdio.h>
int main()
{
int i=1,n;
printf("Enter the n value\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d ",i);
i++;
}
printf("\n");
}
10
[Link] digits in a given number
#include<stdio.h>
int main()
{
int count = 0,n;
printf("Enter the n value\n");
scanf("%d",&n);
/* while(n != 0)
{
n /= 10;
count++;
} */
while(n /= 10, count++, n != 0);
printf("[Link] digits : %d\n",count);
}
11
[Link] even digits of a given number
#include<stdio.h>
int main()
{
int even = 0,n,r;
printf("Enter the n value\n");
scanf("%d",&n);
/*
while(n != 0)
{
r = n%10;
if(r%2 == 0)
even++;
n /= 10;
}
*/
// while(r = n%10,(r%2==0)?even++:0,n /= 10, n != 0);
12
Sum of digits of a given number
#include<stdio.h>
int main()
{
int n,r,s=0;
printf("Enter the n value\n");
scanf("%d",&n);
/*
while(n != 0)
{
r = n%10;
s += r;
n /= 10;
}
*/
13
Sum of digits of a given number
/*
while(n != 0)
{
s += (n%10);
n /= 10;
}
*/
while(s += (n%10), n /= 10, n != 0);
printf("sum of digits : %d\n",s);
}
14
Reversing the number
#include<stdio.h>
int main()
{
int num,rem,rev=0;
printf("Enter the number\n");
scanf("%d",&num);
/*
while(num != 0)
{
rem = num%10;
rev = rev*10+rem;
num = num/10;
}
*/
15
Reversing the number
/*
while(num != 0)
{
rev = rev*10+(num%10);
num /= 10;
}
*/
while(rev = rev*10+(num%10),num /= 10,num != 0);
printf("reversing number : %d\n",rev);
}
16
Palindrome number or not.
#include<stdio.h>
int main()
{
int temp,num,rev=0;
printf("Enter the number\n");
scanf("%d",&num);
temp = num;
while(rev = rev*10+(num%10),num /= 10,num != 0);
if(rev == temp)
printf("Palindrome number\n");
else
printf("Not Palindrome number\n");
}
17
Factorial of a given number
#include<stdio.h>
int main()
{
int n,temp,f=1;
printf("Enter the n value\n");
scanf("%d",&n);
temp = n;
if(n < 0) {
printf("Invalid number for factorial\n");
return 0;
}
18
Factorial of a given number
/*
while(n != 0)
{
f *= n;
n--;
}
*/
//while((n==0)||(f *= n), (n==0)||(n--), n != 0);
while((n==0)||(f *= n,n--), n != 0);
printf("%d! = %d\n",temp,f);
}
19