SlideShare a Scribd company logo
1 of 33
LOOPS
PREPARED BY: KRISHNSRAJ MISHRA
SUBJECT: COMPUTRE SCIENCE
STANDARD: 11-A(SCIENCE)
TOPIC: LOOPS
CONTENTS
O LOOP STATEMENTS
O PARTS OF LOOPS
O TYPES OF LOOPS
O WHILE LOOPS
O FOR LOOPS
O DO WHILE LOOPS
O NESTED LOOPS
O JUMP STATEMENTS
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:
PARTS OF A LOOP
OInitialization Expression(s) initialize(s)
the loop variables in the beginning of the
loop.
OTest Expression decides whether the
loop will be executed (if test expression is
true) or not (if test expression is false).
OUpdate Expression(s) update(s) the
values of loop variables after every
iteration of the loop.
OThe Body-of-the-Loop contains
statements to be executed repeatedly.
TYPES OF LOOPS
C++ programming language provides following types
of loop to handle looping requirements:
WHILE LOOP
OThe syntax of while statement :
while (loop repetition condition)
statement
OLoop 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.
Logic of a while Loop
condition
evaluated
false
statement
true
EXAMPLE:
FOR LOOP
A for statement has the following syntax:
The initialization
is executed once
before the loop begins
for ( initialization ; condition ; increment )
{
statement;
}
The statement is
executed until the
condition becomes false
The increment portion is executed at
the end of each iteration
The statement is
executed until the
condition becomes false
The initialization
is executed once
before the loop begins
Logic of a for loop
statement
true
condition
evaluated
false
increment
initialization
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: 3
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
OUTPUT
THE FOR LOOP VARIATIONS
OMultiple initialization and update expressions
A 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;
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
OOptional expressions
In a for loop initialization expression, test
expression and update expression are optional.
OInitialization expression is skipped:
int i = 1, sum = 0 ;
for ( ; i <= 20 ; sum += i, ++i)
cout <<“n” <<i ;
See initialization expression is skipped
OUpdate expression is skipped:
for ( ; j != 242 ; )
cin >> j++ ;
see update expression is skipped
OInfinite loop
An 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”;
OEmpty loop
If 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
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.
DO…WHILE LOOP
• The syntax of do-while statement in C:
do
statement
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.
Logic of a do…while loop
true
condition
evaluated
statement
false
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);
}
1
2
3
4
5
6
7
8
9
10
OUTPUT
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
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
COMPARISION OF
LOOPS
The 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.
JUMP STATEMENTS
1. 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:
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 :
for(initialize;test expression;update)
{
statement1;
if(val>2000)
break;
:
statement2;
}
statement3;
WORKING OF BREAK STATEMENT IN FOR LOOP
while(test expression)
{
statement1;
if(val>2000)
break;
:
statement2;
}
statement3;
WORKING OF BREAK STATEMENT IN WHILE LOOP
do
{
statement1;
if(val>2000)
break;
:
statement2;
} while(test expression)
statement3;
WORKING OF BREAK STATEMENT IN DO-WHILE LOOP
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;
THE EXIT( ) FUNCTION
O 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.
Thank You……..

More Related Content

What's hot

What's hot (20)

Forloop
ForloopForloop
Forloop
 
Conditions In C# C-Sharp
Conditions In C# C-SharpConditions In C# C-Sharp
Conditions In C# C-Sharp
 
SWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN CSWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN C
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
3.looping(iteration statements)
3.looping(iteration statements)3.looping(iteration statements)
3.looping(iteration statements)
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Loops in C
Loops in CLoops in C
Loops in C
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Loops c++
Loops c++Loops c++
Loops c++
 
Control statements
Control statementsControl statements
Control statements
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
 
The Loops
The LoopsThe Loops
The Loops
 
C# Loops
C# LoopsC# Loops
C# Loops
 
If statements in c programming
If statements in c programmingIf statements in c programming
If statements in c programming
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
Constants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya JyothiConstants Variables Datatypes by Mrs. Sowmya Jyothi
Constants Variables Datatypes by Mrs. Sowmya Jyothi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 

Viewers also liked

Paul connell odi leeds open day october 2015
Paul connell odi leeds open day october 2015Paul connell odi leeds open day october 2015
Paul connell odi leeds open day october 2015odileeds
 
Tarea 2 de la Asignatura Fisiologia y Conducta
Tarea 2 de la Asignatura Fisiologia y ConductaTarea 2 de la Asignatura Fisiologia y Conducta
Tarea 2 de la Asignatura Fisiologia y ConductaYasmira Gutierrez
 
доброта нужна всем!
доброта нужна всем!доброта нужна всем!
доброта нужна всем!Irina Lavrinova
 
"Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e...
"Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e..."Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e...
"Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e...Sergio Andres
 
Viti kei rotuma MBA437 marketing presentation
Viti kei rotuma MBA437 marketing presentationViti kei rotuma MBA437 marketing presentation
Viti kei rotuma MBA437 marketing presentationkelera whippy
 
Social responsibility campaigns
Social responsibility campaignsSocial responsibility campaigns
Social responsibility campaignsMarjorie Marr
 
A portal for the lubricant industry in India
A portal for the lubricant industry in IndiaA portal for the lubricant industry in India
A portal for the lubricant industry in IndiaSuraj Shah
 
Best practice in test security
Best practice in test securityBest practice in test security
Best practice in test securitymstock18494
 
Vulkanska erupcija
Vulkanska erupcijaVulkanska erupcija
Vulkanska erupcijamirkovic_m
 
materi TIK bab 1 kelas 9f smp 18 semarang
materi TIK bab 1 kelas 9f smp 18 semarangmateri TIK bab 1 kelas 9f smp 18 semarang
materi TIK bab 1 kelas 9f smp 18 semarangNadiaelvin
 

Viewers also liked (20)

Paul connell odi leeds open day october 2015
Paul connell odi leeds open day october 2015Paul connell odi leeds open day october 2015
Paul connell odi leeds open day october 2015
 
Tarea 2 de la Asignatura Fisiologia y Conducta
Tarea 2 de la Asignatura Fisiologia y ConductaTarea 2 de la Asignatura Fisiologia y Conducta
Tarea 2 de la Asignatura Fisiologia y Conducta
 
доброта нужна всем!
доброта нужна всем!доброта нужна всем!
доброта нужна всем!
 
проект
проектпроект
проект
 
Смешиваем обучение
Смешиваем обучениеСмешиваем обучение
Смешиваем обучение
 
"Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e...
"Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e..."Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e...
"Fundamentos físicos, culturales, artísticos y técnicos de la tipografía en e...
 
MaGIC Startup Academy Launch : Day 1 - MVP Prototyping In An Agile Environmen...
MaGIC Startup Academy Launch : Day 1 - MVP Prototyping In An Agile Environmen...MaGIC Startup Academy Launch : Day 1 - MVP Prototyping In An Agile Environmen...
MaGIC Startup Academy Launch : Day 1 - MVP Prototyping In An Agile Environmen...
 
Viti kei rotuma MBA437 marketing presentation
Viti kei rotuma MBA437 marketing presentationViti kei rotuma MBA437 marketing presentation
Viti kei rotuma MBA437 marketing presentation
 
Tamazunchale
Tamazunchale Tamazunchale
Tamazunchale
 
MaGIC Startup Academy Launch : Day 2 - How Google Analytics Enhance User Enga...
MaGIC Startup Academy Launch : Day 2 - How Google Analytics Enhance User Enga...MaGIC Startup Academy Launch : Day 2 - How Google Analytics Enhance User Enga...
MaGIC Startup Academy Launch : Day 2 - How Google Analytics Enhance User Enga...
 
Social responsibility campaigns
Social responsibility campaignsSocial responsibility campaigns
Social responsibility campaigns
 
Copa
CopaCopa
Copa
 
Tik bab 4
Tik bab 4Tik bab 4
Tik bab 4
 
Tik bab 4
Tik bab 4Tik bab 4
Tik bab 4
 
A portal for the lubricant industry in India
A portal for the lubricant industry in IndiaA portal for the lubricant industry in India
A portal for the lubricant industry in India
 
Best practice in test security
Best practice in test securityBest practice in test security
Best practice in test security
 
Gst Slides - What To Watch Out For Legally
Gst Slides - What To Watch Out For LegallyGst Slides - What To Watch Out For Legally
Gst Slides - What To Watch Out For Legally
 
Vulkanska erupcija
Vulkanska erupcijaVulkanska erupcija
Vulkanska erupcija
 
materi TIK bab 1 kelas 9f smp 18 semarang
materi TIK bab 1 kelas 9f smp 18 semarangmateri TIK bab 1 kelas 9f smp 18 semarang
materi TIK bab 1 kelas 9f smp 18 semarang
 
MozAware
MozAwareMozAware
MozAware
 

Similar to Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

Similar to Loops IN COMPUTER SCIENCE STANDARD 11 BY KR (20)

Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
C language 2
C language 2C language 2
C language 2
 
Loops
LoopsLoops
Loops
 
Iteration
IterationIteration
Iteration
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Loops
LoopsLoops
Loops
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Loop structures
Loop structuresLoop structures
Loop structures
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
c++ Lecture 3
c++ Lecture 3c++ Lecture 3
c++ Lecture 3
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Loops in c
Loops in cLoops in c
Loops in c
 

More from Krishna Raj

Light emitting diode(LED)
Light emitting diode(LED)Light emitting diode(LED)
Light emitting diode(LED)Krishna Raj
 
How to energize and use ice breakers
How to energize and use ice breakersHow to energize and use ice breakers
How to energize and use ice breakersKrishna Raj
 
Magnetic levitation(kr)
Magnetic levitation(kr)Magnetic levitation(kr)
Magnetic levitation(kr)Krishna Raj
 
MAGNETISM,EARTH MAGNETIC FIELD
MAGNETISM,EARTH MAGNETIC FIELDMAGNETISM,EARTH MAGNETIC FIELD
MAGNETISM,EARTH MAGNETIC FIELDKrishna Raj
 
Technical inovation in mechanical field
Technical inovation in mechanical fieldTechnical inovation in mechanical field
Technical inovation in mechanical fieldKrishna Raj
 
Technological disaster
Technological disaster Technological disaster
Technological disaster Krishna Raj
 
Newton's laws of motion (krishnaraj)
Newton's laws of motion (krishnaraj)Newton's laws of motion (krishnaraj)
Newton's laws of motion (krishnaraj)Krishna Raj
 
RAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMRAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMKrishna Raj
 
RAILWAY RESERWATION PROJECT
RAILWAY RESERWATION PROJECTRAILWAY RESERWATION PROJECT
RAILWAY RESERWATION PROJECTKrishna Raj
 
Diversityinlivingorganisms for class 9 by kr
Diversityinlivingorganisms for class 9 by krDiversityinlivingorganisms for class 9 by kr
Diversityinlivingorganisms for class 9 by krKrishna Raj
 
Indian nobel prize winners
Indian nobel prize winnersIndian nobel prize winners
Indian nobel prize winnersKrishna Raj
 
Information technology
Information technologyInformation technology
Information technologyKrishna Raj
 
Introduction to trignometry
Introduction to trignometryIntroduction to trignometry
Introduction to trignometryKrishna Raj
 

More from Krishna Raj (14)

Light emitting diode(LED)
Light emitting diode(LED)Light emitting diode(LED)
Light emitting diode(LED)
 
How to energize and use ice breakers
How to energize and use ice breakersHow to energize and use ice breakers
How to energize and use ice breakers
 
Magnetic levitation(kr)
Magnetic levitation(kr)Magnetic levitation(kr)
Magnetic levitation(kr)
 
MAGNETISM,EARTH MAGNETIC FIELD
MAGNETISM,EARTH MAGNETIC FIELDMAGNETISM,EARTH MAGNETIC FIELD
MAGNETISM,EARTH MAGNETIC FIELD
 
Technical inovation in mechanical field
Technical inovation in mechanical fieldTechnical inovation in mechanical field
Technical inovation in mechanical field
 
Technological disaster
Technological disaster Technological disaster
Technological disaster
 
Newton's laws of motion (krishnaraj)
Newton's laws of motion (krishnaraj)Newton's laws of motion (krishnaraj)
Newton's laws of motion (krishnaraj)
 
RAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAMRAILWAY RESERWATION PROJECT PROGRAM
RAILWAY RESERWATION PROJECT PROGRAM
 
RAILWAY RESERWATION PROJECT
RAILWAY RESERWATION PROJECTRAILWAY RESERWATION PROJECT
RAILWAY RESERWATION PROJECT
 
Diversityinlivingorganisms for class 9 by kr
Diversityinlivingorganisms for class 9 by krDiversityinlivingorganisms for class 9 by kr
Diversityinlivingorganisms for class 9 by kr
 
Indian nobel prize winners
Indian nobel prize winnersIndian nobel prize winners
Indian nobel prize winners
 
Information technology
Information technologyInformation technology
Information technology
 
Transport
TransportTransport
Transport
 
Introduction to trignometry
Introduction to trignometryIntroduction to trignometry
Introduction to trignometry
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 

Loops IN COMPUTER SCIENCE STANDARD 11 BY KR

  • 1. LOOPS PREPARED BY: KRISHNSRAJ MISHRA SUBJECT: COMPUTRE SCIENCE STANDARD: 11-A(SCIENCE) TOPIC: LOOPS
  • 2. CONTENTS O LOOP STATEMENTS O PARTS OF LOOPS O TYPES OF LOOPS O WHILE LOOPS O FOR LOOPS O DO WHILE LOOPS O NESTED LOOPS O JUMP STATEMENTS
  • 3. 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:
  • 4. PARTS OF A LOOP OInitialization Expression(s) initialize(s) the loop variables in the beginning of the loop. OTest Expression decides whether the loop will be executed (if test expression is true) or not (if test expression is false). OUpdate Expression(s) update(s) the values of loop variables after every iteration of the loop. OThe Body-of-the-Loop contains statements to be executed repeatedly.
  • 5. TYPES OF LOOPS C++ programming language provides following types of loop to handle looping requirements:
  • 6. WHILE LOOP OThe syntax of while statement : while (loop repetition condition) statement OLoop 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.
  • 7.
  • 8. Logic of a while Loop condition evaluated false statement true
  • 10. FOR LOOP A for statement has the following syntax: The initialization is executed once before the loop begins for ( initialization ; condition ; increment ) { statement; } The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration The statement is executed until the condition becomes false The initialization is executed once before the loop begins
  • 11.
  • 12. Logic of a for loop statement true condition evaluated false increment initialization
  • 13. 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: 3 3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30 OUTPUT
  • 14. THE FOR LOOP VARIATIONS OMultiple initialization and update expressions A 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;
  • 15. 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
  • 16. OOptional expressions In a for loop initialization expression, test expression and update expression are optional. OInitialization expression is skipped: int i = 1, sum = 0 ; for ( ; i <= 20 ; sum += i, ++i) cout <<“n” <<i ; See initialization expression is skipped OUpdate expression is skipped: for ( ; j != 242 ; ) cin >> j++ ; see update expression is skipped
  • 17. OInfinite loop An 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”;
  • 18. OEmpty loop If 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
  • 19. 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.
  • 20. DO…WHILE LOOP • The syntax of do-while statement in C: do statement 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.
  • 21. Logic of a do…while loop true condition evaluated statement false
  • 22. 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); } 1 2 3 4 5 6 7 8 9 10 OUTPUT
  • 23. 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
  • 24. 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
  • 25. COMPARISION OF LOOPS The 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.
  • 26. JUMP STATEMENTS 1. 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:
  • 27. 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 :
  • 31. 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;
  • 32. THE EXIT( ) FUNCTION O 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.