loops in computer science standard 11 by kr

33
LOOPS PREPARED BY : KRISHNSRAJ MISHRA SUBJECT : COMPUTRE SCIENCE STANDARD : 11-A(SCIENCE) TOPIC : LOOPS

Upload: krishna-raj

Post on 21-Mar-2017

31 views

Category:

Education


1 download

TRANSCRIPT

Page 1: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

LOOPS

PREPARED BY: KRISHNSRAJ MISHRASUBJECT: COMPUTRE SCIENCE

STANDARD: 11-A(SCIENCE)TOPIC: LOOPS

Page 2: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

CONTENTSO LOOP STATEMENTSO PARTS OF LOOPSO TYPES OF LOOPSO WHILE LOOPSO FOR LOOPS O DO WHILE LOOPSO NESTED LOOPSO JUMP STATEMENTS

Page 3: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

LOOP STATEMENTS

The loop statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled. Following is the general from of a loop statement in most of the programming languages:

Page 4: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

PARTS OF A LOOPO Initialization Expression(s) initialize(s)

the loop variables in the beginning of the loop.

O Test Expression decides whether the loop will be executed (if test expression is true) or not (if test expression is false).

O Update Expression(s) update(s) the values of loop variables after every iteration of the loop.

O The Body-of-the-Loop contains statements to be executed repeatedly.

Page 5: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

TYPES OF LOOPSC++ programming language provides following types of loop to handle looping requirements:

Page 6: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

WHILE LOOPOThe syntax of while statement :while (loop repetition condition)statementOLoop repetition condition is the condition

which controls the loop.OThe statement is repeated as long as the loop

repetition condition is true.OA loop is called an infinite loop if the loop

repetition condition is always true.

Page 7: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Page 8: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

Logic of a while Loop

conditionevaluated

false

statement

true

Page 9: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

EXAMPLE:

Page 10: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

FOR LOOPA for statement has the following syntax:The initializationis executed once

before the loop begins

for ( initialization ; condition ; increment ){ statement;}

The statement isexecuted until the

condition becomes false

The increment portion is executed at the end of each

iteration

The statement isexecuted until the

condition becomes false

The initializationis executed once

before the loop begins

Page 11: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Page 12: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

Logic of a for loop

statement

true

conditionevaluated

false

increment

initialization

Page 13: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

EXAMPLE://program to display table of a given number using for loop.#include<iostream.h>void main(){int n;cout<<“\n Enter number:”;cin>>n;//for loop for(int i=1;i<11;++i) cout<<“\n”<<n<<“*”<<i<<“=“<<n*i;}

Enter number: 33*1=33*2=63*3=93*4=123*5=153*6=183*7=213*8=243*9=273*10=30

OUTPUT

Page 14: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

THE FOR LOOP VARIATIONS

OMultiple initialization and update expressionsA for loop may contain multiple initialization and/or multiple update expressions. These multiple expressions must be separated by commas.e.g.

for( i=1, sum=0; i<=n; sum+=i, ++i)cout<<“\n”<<i;

Page 15: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

OPrefer prefix increment/decrement over postfix when to be used alone.

for( i=1;i<n;++i):

rather than,

for( i=1;i<5;i++):

Reason being that when used alone ,prefix operators are faster executed than postfix.

Prefer this over this

Page 16: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

OOptional expressionsIn a for loop initialization expression, test expression and update expression are optional.O Initialization expression is skipped: int i = 1, sum = 0 ;for ( ; i <= 20 ; sum += i, ++i) cout <<“\n” <<i ;

See initialization expression is skippedOUpdate expression is skipped:for ( ; j != 242 ; ) cin >> j++ ; see update expression is skipped

Page 17: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

OInfinite loopAn infinite loop can be created by omitting the test expression as shown:

for(j=25; ;--j)cout<<“an infinite for loop”;

An infinite loop can also be created as:for( ; ; )cout<<“endless for loop”;

Page 18: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

OEmpty loopIf a loop does not contain any statement in its loop-body, it is said to be an empty loop:for(j=25; (j) ;--j) //(j) tests for non zero value of j.

If we put a semicolon after for’s parenthesis it repeats only for counting the control variable. And if we put a block of statements after such a loop, it is not a part of for loop.

e.g. for(i=0;i<10;++i);{

cout<<“i=“<<i<<endl;}

The semicolon ends the loop here only

This is not the body of the for loop. For loop is

an empty loop

Page 19: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

Declaration of variables in the loop

for ( int I = 1 ; 1 < n ; ; ++i ) { ………. }A variable can be accessed only in the block where it has been declared into.

Page 20: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

DO…WHILE LOOP• The syntax of do-while statement in C:dostatement while (loop repetition condition);• The statement is first executed.• If the loop repetition condition is true,

the statement is repeated.• Otherwise, the loop is exited.

Page 21: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

Logic of a do…while loop

true

conditionevaluated

statement

false

Page 22: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

EXAMPLE://program to display counting from 1 to 10 using do-while loop.#include<iostream.h>void main(){int i=1; //do-while loop do{cout<<“\n”<<i;i++;}while(i<=10);}

123456789

10

OUTPUT

Page 23: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

NESTED LOOPS• Nested loops consist of an outer loop with one

or more inner loops. e.g.,

for (i=1;i<=100;i++){for(j=1;j<=50;j++){

…}

}• The above loop will run for 100*50 iterations.

Inner loop

Outer loop

Page 24: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

EXAMPLE://program to display a pattern of a given character using nested loop.#include<iostream.h>void main(){int i,j; for( i=1;i<5;++i) {cout<<“\n”;for(j=1;j<=i;++j)cout<<“*”;}}

** ** * ** * * *

OUTPUT

Page 25: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

COMPARISION OF LOOPSThe for loop is appropriate when you know in advance how many times the loop will be executed. The other two loops while and do-while loops are more suitable in the situations where it is not known before-hand when the loop will terminate.

Page 26: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

JUMP STATEMENTS1. The goto statement• A goto statement can transfer the program control

anywhere in the program.• The target destination is marked by a label.• The target label and goto must appear in the same

statement.• The syntax of goto statement is:

goto label;…label:

Page 27: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

2. The break statement• The break statement enables a program to skip

over part of the code.• A break statement terminates the smallest

enclosing while, do-while and for statements.• A break statement skips the rest of the loop and

jumps over to the statement following the loop. The following figures explains the working of a break statement :

Page 28: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

for(initialize;test expression;update){

statement1; if(val>2000)

break; :

statement2;}

statement3;

WORKING OF BREAK STATEMENT IN FOR LOOP

Page 29: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

while(test expression) {

statement1; if(val>2000) break;

: statement2;

} statement3;

WORKING OF BREAK STATEMENT IN WHILE LOOP

Page 30: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

do{

statement1; if(val>2000)

break; :

statement2;} while(test expression)statement3;

WORKING OF BREAK STATEMENT IN DO-WHILE LOOP

Page 31: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

3. The continue statement• The continue statement works somewhat like the

break statement. • For the for loop, continue causes the conditional

test and increment portions of the loop to execute. For the while and do...while loops, program control passes to the conditional tests.

• Syntax: The syntax of a continue statement in C++ is:

continue;

Page 32: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

THE EXIT( ) FUNCTIONO This function cause the program to terminate

as soon as it is encountered, no matter where it appears in the program listing.

O The exit() function as such does not have any return value. Its argument, is returned to the operating system.

O This value can be tested in batch files ERROR LEVEL gives you the return value provided by exit() function.

O The exit() function has been defined under a header file process.h which must be included in a program that uses exit() function.

Page 33: Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

Thank You……..