SlideShare une entreprise Scribd logo
1  sur  40
Shivani Singh
XI A
CONTENTS
1. Loop Statements
2. Parts of a loop
3. Types of Loops
 While Loop
 For Loop
 Do-while Loop
4. Nested Loops
5. Jump Statements
6. Exercises
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:
LOOP STATEMENTS
PARTS OF A LOOP
• Initialization Expression(s) initialize(s) the loop
variables in the beginning of the loop.
• Test Expression decides whether the loop will be
executed (if test expression is true) or not (if test
expression is false).
• Update Expression(s) update(s) the values of
loop variables after every iteration of the loop.
• The Body-of-the-Loop contains statements to be
executed repeatedly.
TYPES OF LOOPS
Loop Type Description
while loop Repeats a statement or group of statements until a
given condition is true. It tests the condition
before executing the loop body.
for loop Execute a sequence of statements multiple times
and abbreviates the code that manages the loop
variable.
do...while loop Like a while statement, except that it tests the
condition at the end of the loop body
nested loops You can use one or more loop inside any another
while, for or do..while loop.
C++ programming language provides following
types of loop to handle looping requirements:
WHILE LOOP
• The syntax of while statement :
while (loop repetition condition)
statement
• Loop repetition condition is the condition which controls
the loop.
• The statement is repeated as long as the loop repetition
condition is true.
• A loop is called an infinite loop if the loop repetition
condition is always true.
Logic of a while Loop
statement
true false
condition
evaluated
EXAMPLE:
#include<iostream.h>
#include <stdio.h>
int main(void)
{
int j;
j = -5;
// while loop
while(j <= 0)
{
printf("%d ", j);
j = j + 1;
}
return 0;
}
FOR LOOP
A for statement has the following syntax:
for ( initialization ; condition ; increment )
{
statement;
}
The initialization
is executed once
before the loop begins
The statement is
executed until the
condition becomes false
The increment portion is executed at
the end of each iteration
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
 Multiple 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;
 Prefer 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
 Infinite 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”;
 Empty 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
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
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;
EXERCISES:
• MULTIPLE CHOICE QUESTIONS
(MCQs)
• PROGRAM BASED QUESTIONS
MCQs...
1. The statement i++; is equivalent to
[a] i = i + i;
[b] i = i + 1;
[c] i = i - 1;
[d] i --;
NEXT QUESTION
ANS: [b]
2. What's wrong? for (int k = 2, k <=12, k++)
[a] the increment should always be ++k
[b] the variable must always be the letter i
when using a for loop
[c] there should be a semicolon at the end of
the statement
[d] the commas should be semicolons
NEXT QUESTION
ANS: [d]
3. Which looping process checks the test
condition at the end of the loop?
[a] for
[b] while
[c] do-while
[d] no looping process checks the test
condition at the end
NEXT QUESTION
ANS: [c]
4. Which looping process is best used when the
number of iterations is known?
[a] for
[b] while
[c] do-while
[d] all looping processes require that the
iterations be known
NEXT QUESTION
ANS: [a]
5. A continue statement causes execution to
skip to
[a] the return 0; statement
[b] the first statement after the loop
[c] the statement following the continue
statement
[d] the next iteration of the loop
ANS: [d]
PROGRAM BASED
QUESTIONS…
1. Write a program to print first n natural numbers and
their sum.
2. Write a program to calculate the factorial of an
integer.
3. Write a program that prints 1 2 4 8 16 32 64 128.
4. Write a program to check whether the given
number is palindrome or not.
5. Write a program to generate divisors of an integer.
6. Write a program to find whether a given number is
odd or even. The program should continue as long as
the user wants.
7. Write a program to print Fibonacci series i.e.,0 1 1 2
3 5 8….
8. Write a program to print largest even and largest odd
number from a list of numbers entered. The list
terminates as soon as one enters 0(zero).
9. Write a program to check whether the entered
number is Armstrong or not.
10. Write a program to display Pythagorean triplets up to
100.
11. Write a program to calculate average of 10 numbers.
12. Write programs to produce the following designs:
a) A
A B
A B C
A B C D
A B C D E
b) & & & & & & &
& & & & &
& & &
&
c) &
& &
& &
& &
& & & & & & & & &
THANK YOU

Contenu connexe

Tendances (20)

The Loops
The LoopsThe Loops
The Loops
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
While loop
While loopWhile loop
While loop
 
Data Types and Variables In C Programming
Data Types and Variables In C ProgrammingData Types and Variables In C Programming
Data Types and Variables In C Programming
 
C functions
C functionsC functions
C functions
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Function in C program
Function in C programFunction in C program
Function in C program
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
File in C language
File in C languageFile in C language
File in C language
 
C if else
C if elseC if else
C if else
 
Nested loops
Nested loopsNested loops
Nested loops
 
structured programming
structured programmingstructured programming
structured programming
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
String functions in C
String functions in CString functions in C
String functions in C
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Strings in C
Strings in CStrings in C
Strings in C
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 

En vedette (20)

Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
C++ loop
C++ loop C++ loop
C++ loop
 
Loops in C
Loops in CLoops in C
Loops in C
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Loops
LoopsLoops
Loops
 
Loops
LoopsLoops
Loops
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Loop c++
Loop c++Loop c++
Loop c++
 
Do While and While Loop
Do While and While LoopDo While and While Loop
Do While and While Loop
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Loops
LoopsLoops
Loops
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 looping
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statement
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
pseudo code basics
pseudo code basicspseudo code basics
pseudo code basics
 

Similaire à Loops c++

Similaire à Loops c++ (20)

Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
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
 
Loops
LoopsLoops
Loops
 
Loops in c
Loops in cLoops in c
Loops in c
 
Loops in c
Loops in cLoops in c
Loops in c
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
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
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf3. Flow Controls in C (Part II).pdf
3. Flow Controls in C (Part II).pdf
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
 
M C6java6
M C6java6M C6java6
M C6java6
 
Programming loop
Programming loopProgramming loop
Programming loop
 
Ch05
Ch05Ch05
Ch05
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Loop structures
Loop structuresLoop structures
Loop structures
 

Dernier

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 

Dernier (20)

COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

Loops c++

  • 2. CONTENTS 1. Loop Statements 2. Parts of a loop 3. Types of Loops  While Loop  For Loop  Do-while Loop 4. Nested Loops 5. Jump Statements 6. Exercises
  • 3. 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: LOOP STATEMENTS
  • 4. PARTS OF A LOOP • Initialization Expression(s) initialize(s) the loop variables in the beginning of the loop. • Test Expression decides whether the loop will be executed (if test expression is true) or not (if test expression is false). • Update Expression(s) update(s) the values of loop variables after every iteration of the loop. • The Body-of-the-Loop contains statements to be executed repeatedly.
  • 5. TYPES OF LOOPS Loop Type Description while loop Repeats a statement or group of statements until a given condition is true. It tests the condition before executing the loop body. for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. do...while loop Like a while statement, except that it tests the condition at the end of the loop body nested loops You can use one or more loop inside any another while, for or do..while loop. C++ programming language provides following types of loop to handle looping requirements:
  • 6. WHILE LOOP • The syntax of while statement : while (loop repetition condition) statement • Loop repetition condition is the condition which controls the loop. • The statement is repeated as long as the loop repetition condition is true. • A loop is called an infinite loop if the loop repetition condition is always true.
  • 7.
  • 8. Logic of a while Loop statement true false condition evaluated
  • 9. EXAMPLE: #include<iostream.h> #include <stdio.h> int main(void) { int j; j = -5; // while loop while(j <= 0) { printf("%d ", j); j = j + 1; } return 0; }
  • 10. FOR LOOP A for statement has the following syntax: for ( initialization ; condition ; increment ) { statement; } The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration
  • 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  Multiple 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.  Prefer 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.  Infinite 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”;
  • 17.  Empty 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
  • 18. 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.
  • 19. Logic of a do…while loop true condition evaluated statement false
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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:
  • 24. 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 :
  • 28. 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;
  • 29. EXERCISES: • MULTIPLE CHOICE QUESTIONS (MCQs) • PROGRAM BASED QUESTIONS
  • 31. 1. The statement i++; is equivalent to [a] i = i + i; [b] i = i + 1; [c] i = i - 1; [d] i --; NEXT QUESTION ANS: [b]
  • 32. 2. What's wrong? for (int k = 2, k <=12, k++) [a] the increment should always be ++k [b] the variable must always be the letter i when using a for loop [c] there should be a semicolon at the end of the statement [d] the commas should be semicolons NEXT QUESTION ANS: [d]
  • 33. 3. Which looping process checks the test condition at the end of the loop? [a] for [b] while [c] do-while [d] no looping process checks the test condition at the end NEXT QUESTION ANS: [c]
  • 34. 4. Which looping process is best used when the number of iterations is known? [a] for [b] while [c] do-while [d] all looping processes require that the iterations be known NEXT QUESTION ANS: [a]
  • 35. 5. A continue statement causes execution to skip to [a] the return 0; statement [b] the first statement after the loop [c] the statement following the continue statement [d] the next iteration of the loop ANS: [d]
  • 37. 1. Write a program to print first n natural numbers and their sum. 2. Write a program to calculate the factorial of an integer. 3. Write a program that prints 1 2 4 8 16 32 64 128. 4. Write a program to check whether the given number is palindrome or not. 5. Write a program to generate divisors of an integer. 6. Write a program to find whether a given number is odd or even. The program should continue as long as the user wants. 7. Write a program to print Fibonacci series i.e.,0 1 1 2 3 5 8….
  • 38. 8. Write a program to print largest even and largest odd number from a list of numbers entered. The list terminates as soon as one enters 0(zero). 9. Write a program to check whether the entered number is Armstrong or not. 10. Write a program to display Pythagorean triplets up to 100. 11. Write a program to calculate average of 10 numbers. 12. Write programs to produce the following designs: a) A A B A B C A B C D A B C D E
  • 39. b) & & & & & & & & & & & & & & & & c) & & & & & & & & & & & & & & & &