an instruction or group of instructions. computer executes program repeatedly for specified number...
TRANSCRIPT
An instruction or group of instructions.
Computer executes program repeatedly for specified number of times. Or until some terminating conditions are satisfied.
3
What is loop?
2
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.
TYPES OF LOOP:
3
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.
TYPES OF LOOP:
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
METHODS TO LOOP A PROGRAMME:
5
The popular and mostly used loop.
It is determinant loop.
It checks the condition before execution. Which means that it is pretest loop .
FOR LOOP OR FOR STATEMENT:
6
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).
The “FOR LOOP” specify the following three statements:
7
{ for (azr=11,azr<=20,azr=11+1) expression update
initialization condition
{ Body of the loop } }
The above three operations are written in program as:
8
#include<stdio.h>#include<conio.h> void main ( ){ int azr; clrscr ( ); for(azr=5;azr<=45;azr=5+1) { printf(“%d\n”,azr); } getch(); }
A program in for loop:
9
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).
To get even integers or odd integers:
10
#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(); }
FOR EXAMPLE: TO GET EVEN NUMBERS ON THE BLACK SCREEN:
11
#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(); }
TO GET ODD NUMBERS: