1. LOOP CONSTRUCTS
CHAPTER # 12
SUBJECT: COMPUTER ICS (PART II)
Presented By :
Prof. HASEEB ASLAM
M.Phil. (IT)
(ASPIRE GROUP OF COLLEGES WAZIRABAD)
1
2. LOOP
A statement or set of statements that is
executed repeatedly is known as Loop.
The structure that repeats a statement(s) is known as
iterative , repetitive or looping construct.
-: Uses :-
1. To execute statement or number of statements for
specified number of times.
2. To use a sequence of values.
-: Types :-
1. While Loop
2. Do-while Loop
3. For Loop
2
ASPIRE
COLLEGE
WAZIRABAD
3. WHILE LOOP
Is a simplest loop in a C language.
This loop executes one or more statements
while given condition remains true.
It is useful where the number of iterations not known
in advance.
-: Syntax :-
while (condition)
{
statement(s) ;
}
3
1. Condition is given as
relational expression.
2. It controls the iteration
of loop.
3. Statements are
executed only if given
condition is true.
4. Statements are not
executed if given
condition is false.
4. WORKING OF “WHILE” LOOP
First of all the condition is checked.
If it is true
the control enters the body of the loop
and executes all statements in the body.
After executing the statements
it again moves to the start of the loop and
check the condition again.
This process continuous as long as the condition
remains true.
When the condition becomes false
the loop is terminated.
While loop ends only
when the condition becomes false.
If the condition remains true loop never ends .
A loop that has no end point is known as infinite loop. 4
ASPIRE
COLLEGE
WAZIRABAD
7. DO-WHILE LOOP
The do-while is an iterative control in C language.
This loop executes one or more statements
while given condition remains true.
In this loop condition comes after the body of loop.
The loop is important in a situation
where statement must be executed at least once.
-: Syntax :-
do
{
statement(s) ;
}
while (condition);
7
8. FOR LOOP
For loop executes one or more statements for a a
specified number of times this loop is also called
counter controlled loop it is the most flexible loop
this is why the most programmers use this loop in
programs .
Initialization.
It specifies the starting value of counter variable
one are many variables can be initialized in this
part to initialize many variables each variable is
separated by comma. Condition:
8
9. SENTINEL CONTROL LOOP
# include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1;
clrscr();
while( n!= -1)
{
printf(“Enter a number”);
scanf(“%d”, &n);
printf(“you entered %d n”, n);
}
printf(“End of program..”);
getch();
}
9
ASPIRE
COLLEGE
WAZIRABAD
10. CONTINUE STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1 , num ;
clrscr();
while( n <= 5)
{
printf(“Enter a number”);
scanf(“%d”, &num);
n++;
if (num<= 0 )
continue ;
printf(“you entered %d n”, num);
}
printf(“End of program..”);
getch();
}
10
ASPIRE
COLLEGE
WAZIRABAD
11. CONTINUE STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1 , num ;
clrscr();
while( n <= 5)
{
printf(“Enter a number”);
scanf(“%d”, &num);
if (num<= 0 )
continue ;
printf(“you entered %d n”, num);
n++;
}
printf(“End of program..”);
getch();
}
11
ASPIRE
COLLEGE
WAZIRABAD
12. BREAK STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int n = 1 , num ;
clrscr();
while( n <= 5)
{
printf(“Enter a number”);
scanf(“%d”, &num);
n++;
if (num<= 0 )
break ;
printf(“you entered %d n”, num);
}
printf(“End of program..”);
getch();
}
12
ASPIRE
COLLEGE
WAZIRABAD
13. GOTO STATEMENT
#include <stdio.h>
#include <conio.h>
void main( )
{
int num ;
clrscr();
positive:
printf(“Enter a number”);
scanf(“%d”, &num);
if(num < 0)
goto positive:
printf(“You Entered %d” , num);
getch();
}
13
ASPIRE
COLLEGE
WAZIRABAD