an instruction or group of instructions. computer executes program repeatedly for specified number...

11
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?

Upload: clarissa-burke

Post on 01-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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?

Page 2: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 3: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 4: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 5: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 6: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 7: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 8: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 9: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 10: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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:

Page 11: An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are

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: