You are on page 1of 11

What is loop?

 An instruction or group of instructions.

 Computer executes program repeatedly for specified


number of times. Or until some terminating
conditions are satisfied.

3
TYPES OF LOOP:
 There are two types of loop.

A. Determinant loop or counter loop:

• Extremely powerful loop.

• This loop is used, when the number of


executions or iteration is known.

• Used in programs like print “university of


swat” 5 times. Etc.
2
TYPES OF LOOP:
B. Undeterminant loop or controlled loop:

• Its Execution depends upon the condition


either true or false.

• The number of iteration is not known


before the operation.

• While and do while loops are used here.

3
METHODS TO LOOP A PROGRAMME:

 There are three methods to loop a program.

A. Using For loop or (For statement).

B. Using while loop or (while statement).

C. Using do while loop or (do while statement).

4
FOR LOOP OR FOR STATEMENT:

The popular and mostly used loop.

 It is determinant loop.

 It checks the condition before the


execution. Which means that it is pretest
loop .

5
The “FOR LOOP” specify
the following three statements:
A. INITIALIZATION: Means to assign value for
variable to initialize the loop. i.e (azr=11).

B. CONDITION: Test the condition either it


reach the desire number of execution or not.
i.e (azr<=20).

C. EXPRESSION UPDATE: It increases the loop


counter as much it executes. i.e (azr=11+1).

6
The above three operations are written in program as:

{
for (azr=11,azr<=20,azr=11+1)
expression update

initialization condition

{
Body of the loop
}
}

7
A program in for loop:
#include<stdio.h>
#include<conio.h>
void main ( )
{
int azr;
clrscr ( );
for(azr=5;azr<=45;azr=5+1)
{
printf(“%d\n”,azr);
}
getch();
}
8
To get even integers or odd integers:

Then we put “if” statement after the “for” statement.


i.e
 to get even numbers:
{
For (initialization ;condition ; expression update)

if (azr%2==0);
}

 to get odd numbers the if statement should be

if (azr%2!=0).

9
FOR EXAMPLE:
TO GET EVEN NUMBERS ON THE BLACK SCREEN:
#include<stdio.h>
#include<conio.h>
void main( )
{
int amb;
clrscr( );
{
for (amb=20;amb<=40;amb=amb+1)
if (amb%2==0);
}
printf (“%d\n”,amb);
getch();
}

10
TO GET ODD NUMBERS:

#include<stdio.h>
#include<conio.h>
void main( )
{
int X;
clrscr( );
{
for (X=9;X<=35;X=X+1)
if (X%2!=0);
}
printf (“%d\n”,X);
getch();
}
11

You might also like