Documente Academic
Documente Profesional
Documente Cultură
C language
Looping statements
• The test may be either to determine whether the i has repeated the specified
number of times or to determine whether the particular condition has been
met.
• Type of Looping Statements are
• while statement
• do-while statement
• for statement
while statement
Syntax
While
False
(test
condition)
while (test condition)
{ True
True While
(test
condition)
False
do..while statement example
#include<stdio.h> Output
void main() Enter the number:275
{ Reversed number is 572
int num=0, rev_num=0;
printf(“Enter the number:”);
scanf(“%d”,&num);
do
{
ld=num%10;
rev_num=rev_num*10+ld;
num=num/10;
} while(num>0);
printf(“\nReversed number is %d”,rev_num);
}
while and do..while
comparison
While Do…while
1) Syntax: 1) Syntax:
while(condition) do
{ {
Body of the loop Body of the loop
} }while(condition);
2) This is decision making and 2) This is also -decision making
looping statement and looping statement
3) This is the top tested loop 3) This is the bottom tested loop
4) Loop will be executed atleast
4)Loop will not be executed if the
once even though the condition is
condition is false in first check
false in first check
for statement
■ The for loop is most commonly and popularly used looping statement in C. The
for loop allows us to specify three things about the loop control variable i in a
single line. They are,
Increment/Decrement;
True
test condition
False
for statement example
// Number 1 to 10 divisible by 2 but not
divisible by 3 and 5 Output
#include<stdio.h> 2
void main() 4
{ 8
int i;
for(i=1;i<=10;i++)
{
if(i%2==0&&i%3!=0&&i%5!=0)
printf("%d\n",i);
}
}
for statement example
//12+22+32+…. n2
Output
#include<stdio.h> //<math.h>
void main() Enter the number:5
{ Sum of series=55
int n, i,sum=0;
printf(“Enter the number:”);
scanf(“%d”, &n);
for(i=1;i <= n;i++)
{
sum = sum + i*i; //pow(i,2)
}
printf(“Sum of series=%d”,sum);
}
break statement
■ Sometimes while executing a loop it becomes desirable to skip a part of the
loop or quit the loop as soon as certain condition occurs.
1) Syntax: 1) Syntax:
break; continue;
2) Takes the control to outside of 2) Takes the control to beginning
the loop of the loop
3) It is used in switch statement 3) It is not used in switch
statement
4) Example: 4) Example:
for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
if(i==3) if(i==3)
break; continue;
} }