0% found this document useful (0 votes)
17 views5 pages

Loops in C 1

Uploaded by

Tony stark Ok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Loops in C 1

Uploaded by

Tony stark Ok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LOOPS IN C

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.

It might occur statement(s) is


executed zero times, If condition is At least once the statement(s) is
false. executed.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

while loop is entry controlled loop. do-while loop is exit controlled loop.

while(condition) do { statement(s); } while(condition);


{ statement(s); }

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;

while (i < 10) {

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++;

} while (i < 10);

return 0;

You might also like