Sunteți pe pagina 1din 18

Computer Programming:

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

body of the loop; Body of the i;


}
while statement example
#include<stdio.h> Output
void main() Enter a number: 275
{ Sum of digits of a number=14
int n,x,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("Sum of digits of a number=%d",sum);
}
while statement example
#include<stdio.h> if(sum==temp)
void main() printf("%d is an Armstrong number“
{ ,temp);
int num,r,sum=0,temp; else
printf("Enter a number: "); printf("%d is not an Armstrong number“
scanf("%d",&num); ,temp);
temp=num; }
while(num!=0)
{ Output
r=num%10; Enter a number: 275
sum=sum+(r*r*r); 275 is an Armstrong number
num=num/10;
} Enter a number: 153
153 is an Armstrong number
do..while statement
• Since the body of the i is executed first and then the i condition is checked
we can be assured that the body of the i is executed at least once.
Syntax
do
{
body of the loop;
}
while (test condition);
do..while statement
do

Body of the loop

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,

■ Initializing the value for the i

■ Condition in the i counter to determine whether the loop should continue or


not

■ Incrementing or decrementing the value of i counter each time the program


segment has been executed.
Syntax
for(initialization; test condition;increment/decrement)
{
body of the loop;
}
for statement
Initialization;

Increment/Decrement;

Body of the loop

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.

■ For example consider searching a particular number in a set of 100


numbers. As soon as the search number is found it is desirable to terminate
the loop.

■ C language permits a jump from one statement to another within a loop as


well as to jump out of the loop.

■ The break statement allows us to accomplish this task. A break statement


provides an early exit from for, while, do and switch constructs.

■ A break causes the innermost enclosing loop or switch to be exited


immediately.
break statement
#include<stdio.h> Output
void main()
{ Enter the marks, -1 to end:
int mark, i=0,sum=0;
float avg; 55
printf(“Enter the marks, -1 to end:”); 22
while(1) 11
{ 66
scanf(“%d”, &mark);
if(mark == -1) -1
break; The average marks is:38.500000
sum+=mark;
i++;
}
avg=(float)sum/i;
printf(“\nThe average marks is: %f”, avg);
}
continue statement
■During loop operations it may be necessary to skip
a part of the body of the loop under certain
conditions.

■Like the break statement C supports similar


statement called continue statement.

■The continue statement causes the loop to be


continued with the next iteration after skipping any
statement in between.
continue statement
#include < stdio.h > Output
void main() Enter the integer:11 22 33 -1
{ You have entered a negative number
int i, num, sum=0;
printf(“Enter the integer:”); 44
for (i = 0; i < 5; i++) Sum of positive numbers entered =110
{
scanf(“%d”, &num);
if(num < 0)
{
printf(“You have entered a negative number\n”);
continue;
}
sum+=num;
}
printf(“Sum of positive numbers entered = %d”,sum);
}
break and continue
comparison Break Continue

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

S-ar putea să vă placă și