Loops in C 1
Loops in C 1
Sometimes there is a need to perform some operation more than once or (say) n
number of times. Loops come into use when we need to repeatedly execute a
block of statements.
a loop is a sequence of instructions that is repeated until a certain condition is
reached.
There are mainly two types of loops:
1. Entry Controlled loops: In this type of loop, the test condition is tested
before entering the loop body. For Loop and While Loop is entry-
controlled loops.
2. Exit Controlled Loops: In this type of loop the test condition is tested
or evaluated at the end of the loop body. Therefore, the loop body will
execute at least once, irrespective of whether the test condition is true
or false. the do-while loop is exit controlled loop.
For Loop
A for loop is a repetition control structure that allows us to write a loop that is executed
a specific number of times. The loop enables us to perform n number of steps together
in one line.
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
Elements of LOOP
• Initialization Expression: In this expression, we have to initialize the
loop counter to some value. for example: int i=1;
• 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
loop. For example: i <= 10;
• Update Expression: After executing the loop body this expression
increments/decrements the loop variable by some value. for example:
i++;
While Loop
initialization expression;
while (test_expression)
{
// statements
update_expression;
}
do-while loop
initialization expression;
do
{
// statements
update_expression; }
while (test_expression);
//In a do-while loop, the loop body will execute at least once
irrespective of the test condition.
For While Do-While
int main()
int main() { int i = 1; while (i < 6)
{
{ cout << "Hello
for (int i = 1; i <= 5; i++) int main()
{ World\n"; i++;
printf("Hello World)"; { int i = 2; do { cout
}
} << "Hello World\n"; i++;
}
return 0;
} while (i < 1);
}
return 0;
}
Palindrome program in C
#include<stdio.h> int
main()
{
int n, r, sum=0, temp;
printf("enter the number=");
scanf("%d",&n); temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else printf("not
palindrome"); return 0;
}
while do-while
Statement(s) is executed atleast once,
Condition is checked first then
thereafter condition is checked.
statement(s) is executed.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while loop:
A while loop is a control flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The while loop can be
thought of as a repeating if statement. Syntax :
while (boolean condition)
{ loop
statements...
}
#include <stdio.h>
int main()
int i = 5;
printf("GFG\n");
i++;
return 0;
do-while loop:
do while loop is similar to while loop with the only difference that it checks for
the condition after executing the statements, and therefore is an example of
Exit Control Loop.
do
{ statements
..
}
while (condition);
#include <stdio.h>
int main()
int i = 5;
do {
printf("GFG\n");
i++;
return 0;