SlideShare ist ein Scribd-Unternehmen logo
1 von 17
C++ Control Statements
ITERATION STATEMENTS
Parts of a loop
• Initialization expression – Control / Counter Variable has to
be initialized before entering inside the loop. This
expression is executed only once.
• Test expression – The truth value of this expression decides
whether the loop has to be further executed or not
• Update expression – This changes the value of the control /
counter variable. This is executed at the end of loop
statements
• Body of the loop – Set of loop statements are executed
based on the condition.
ITERATION STATEMENTS -
FOR• Syntax
• Example
This program prints 1 to 10
for (initialization expr ; test expr ; update expr)
body of the loop;
for (i = 1; i <= 10; i=i+1)
{
cout << i ;
}
ITERATION STATEMENTS -
FOR
• This will print -2
• This will loop for i values 0, 1, 2, 3, 4
for (a= 10; a >= 0; a=a-3);
cout << a;
for (i = 0; i < 5; i=i+1)
cout << i * i;
Note the semicolon here.
This means empty loop
The loop has empty body
Misc
Declaration of variables in the loop –
Variables declared inside the loop are
accessible inside the loop only. They
cannot be accessed outside. This is called
local scope. (This applies for the selection
statements also)
int c;
for (c = 0; c <=10; c = c+1)
{
int j = c;
cout << j << “ “ << c ;
}
cout << j << “ “<< c;
Valid
Invalid
ITERATION STATEMENTS -
WHILE
• The syntax is
• In the while loop, the control variable
should be initialized outside the loop and it
should be updated inside the loop
while (expression)
Loop body
int a = 0;
while (a <= 10)
{
cout << a;
a = a + 1;
}
This program will print 0 to 10
Nested Loops• A loop may contain another loop inside its body. It is called
Nested Loop.
It is used when one variable has to change values for each value
of another variable.
Output will be
1 table starts
1 x 1 = 1
1 x 2 = 2
….
1 x 20 = 20
1 table ends
2 table starts
2 x 1 = 2
2 x 2 = 4
….
2 x 20 = 40
2 table ends
upto
10 table ends
for (int i = 1 ; i <= 10; i=i+1)
{
cout << i << “ table starts ”;
for (int j = 1 ; j <= 20; j=j+1)
cout << i << “ x “ << j << “ = “ << i * j ;
cout << i << “table ends” ;
cout << endl;
}
Nested Loops
Same program with while loop
int i = j = 1;
while (i <= 10)
{
cout << i << “table starts” ;
while (j <= 20)
{
cout << i << “ x “ << j << “ = “ << i * j ;
j = j + 1;
}
cout << i << “table ends” ;
cout << endl;
i = i + 1;
}
Nested Loop concept
• Identify the varying factors
• Design a loop for each varying factor
• For example, for a pattern like
*******
*****
***
*
• The varying factors are the line
numbers, the spaces in each line and
the number of stars in each line.
• In each line, the number of spaces
varies from 1 to m
• In each line, the number of stars varies
from 1 to n
• So the loop would look one given in
the next slide
Line
No
Total
No of
Spaces
(m)
Total
No of
stars
(n)
1 0 7
2 1 5
3 2 3
4 3 1
int m = 0;
int n = 7;
for (int i = 1 ; i <= 4; i=i+1)
{
for (int j = 0 ; j < m; j=j+1)
{
cout << “ ” ;
}
m = m + 1;
for (int k = 1 ; k <= n; k=k+1)
{
cout << “*” ;
}
n = n – 2;
cout << endl ;
}
Loop starts for each line
Loop starts for spaces
Loop starts for stars
For going to the next line
Nested Loop Example
• For example, for a pattern like
1
121
12321
1234321
• The varying factors are the line
numbers, the spaces in each line, the
numbers increasing and the numbers
decreasing.
• In each line, the number of spaces
varies from 1 to m
• In each line, the number increases upto
the line no
• In each line, the number increases from
the line no
Line No Total No
of
Spaces
(m)
Number
increasin
g till
Number
decreasi
ng from
1 3 1 -
2 2 2 1
3 1 3 2
4 0 4 3
int m = 3;
for (int i = 1 ; i <= 4; i=i+1)
{
for (int j = 1 ; j <= m; j=j+1)
{
cout << “ ” ;
}
m = m - 1;
for (int k = 1 ; k <= i; k=k+1)
{
cout << k ;
}
for (int z = i-1 ; z >= 1; z=z-1)
{
cout << z ;
}
cout << endl ;
}
Loop starts for each line
Loop starts for spaces
Loop starts for increasing numbers
For going to the next line
Loop starts for decreasing numbers
Question Paper pattern
• Write a program
– Simple loops
• Print a series (odd numbers, even numbers, 1 2 4 8
… 1024, a …. z, A…. Z, Factorial of a number,
Sum of a given series of numbers )
• Print a table (8 table)
– Nested loops
• Print a pattern
• Print tables
– Combination of if and loop
Find the mistakes
Wrong Code
for (int c = 0; c >= 10; c = c + 1);
cout << c;
Wrong Code
int a;
while (a <= 10)
{
cout << a;
}
Corrected Code
for (int c = 0; c <= 10; c = c + 1)
cout << c;
Corrected Code
int a = 1;
while (a <= 10)
{
cout << a;
a = a + 1;
}
Find the Output
int sum = 0;
for (int c = 1; c < 10; c = c + 3)
sum = sum + c;
cout << sum << “ “ << c << endl;
Ans: sum = 0 initially
c = 1 sum = 1 in first iteration
c = 4 sum = 5 in second iteration
c = 7 sum = 12 in third iteration
c = 10
Since c < 10, so next iteration will not go inside the
loop
Output will be :
Find the Output
int k = 0, c;
while ( k <= 10)
{
c = k + 3;
k = k + 2;
}
cout << c << “ “ << k;
Ans: k = 0 initially
c = 3 k = 2 in first iteration
c = 5 k = 4 in second iteration
c = 7 k = 6 in third iteration
c = 9 k = 8 in fourth iteration
c = 11 k = 10 in fifth iteration
c = 13 k = 12 in sixth iteration
Since k <= 10, next iteration will not go inside the
loop as k becomes 12
Output will be :
13 12
Fill in the blanks
This is a program to find the sum of odd numbers till 100.
int sum = _____;
int i = 1;
while ( _________ )
{
sum = sum + i;
___________
}
cout << ____________ ;

Weitere ähnliche Inhalte

Was ist angesagt? (19)

C++ programming
C++ programmingC++ programming
C++ programming
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Iteration
IterationIteration
Iteration
 
Looping
LoopingLooping
Looping
 
Nested loops
Nested loopsNested loops
Nested loops
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
For Loop
For LoopFor Loop
For Loop
 
Control statements
Control statementsControl statements
Control statements
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Loops in R
Loops in RLoops in R
Loops in R
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c
Loops in cLoops in c
Loops in c
 
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 

Andere mochten auch

Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C ProgrammingHimanshu Negi
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsTech
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Richard Thomson
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 loopingDhani Ahmad
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++Nitin Jawla
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch casesMeoRamos
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statementJunyoung Jung
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Muhammad Tahir Bashir
 
Control Systems Basics
Control Systems BasicsControl Systems Basics
Control Systems BasicsJohn Todora
 
Loops Basics
Loops BasicsLoops Basics
Loops BasicsMushiii
 
Charging System Automobile
Charging System AutomobileCharging System Automobile
Charging System AutomobileJoren Carcallas
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While LoopAbhishek Choksi
 

Andere mochten auch (20)

Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
 
Loops c++
Loops c++Loops c++
Loops c++
 
C++ loop
C++ loop C++ loop
C++ loop
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Loops in C
Loops in CLoops in C
Loops in C
 
c++ for loops
c++ for loopsc++ for loops
c++ for loops
 
Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++Consuming and Creating Libraries in C++
Consuming and Creating Libraries in C++
 
Chapter 05 looping
Chapter 05   loopingChapter 05   looping
Chapter 05 looping
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statement
 
The Loops
The LoopsThe Loops
The Loops
 
Loops
LoopsLoops
Loops
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
Control Systems Basics
Control Systems BasicsControl Systems Basics
Control Systems Basics
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Loops
LoopsLoops
Loops
 
Charging System Automobile
Charging System AutomobileCharging System Automobile
Charging System Automobile
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 

Ähnlich wie C++ control loops

Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.pptsanjay
 
Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.pptRahulBorate10
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
C++ Programming Club-Lecture 3
C++ Programming Club-Lecture 3C++ Programming Club-Lecture 3
C++ Programming Club-Lecture 3Ammara Javed
 
Control structures.ppt
Control structures.pptControl structures.ppt
Control structures.pptRahul Borate
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptxNaumanRasheed11
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptxAqeelAbbas94
 
Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Béo Tú
 

Ähnlich wie C++ control loops (20)

Ch4
Ch4Ch4
Ch4
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
 
Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.ppt
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
C++ Programming Club-Lecture 3
C++ Programming Club-Lecture 3C++ Programming Club-Lecture 3
C++ Programming Club-Lecture 3
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Control structures.ppt
Control structures.pptControl structures.ppt
Control structures.ppt
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
C# Loops
C# LoopsC# Loops
C# Loops
 
06.Loops
06.Loops06.Loops
06.Loops
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
 
Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014
 

Kürzlich hochgeladen

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 

Kürzlich hochgeladen (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 

C++ control loops

  • 2. ITERATION STATEMENTS Parts of a loop • Initialization expression – Control / Counter Variable has to be initialized before entering inside the loop. This expression is executed only once. • Test expression – The truth value of this expression decides whether the loop has to be further executed or not • Update expression – This changes the value of the control / counter variable. This is executed at the end of loop statements • Body of the loop – Set of loop statements are executed based on the condition.
  • 3. ITERATION STATEMENTS - FOR• Syntax • Example This program prints 1 to 10 for (initialization expr ; test expr ; update expr) body of the loop; for (i = 1; i <= 10; i=i+1) { cout << i ; }
  • 4. ITERATION STATEMENTS - FOR • This will print -2 • This will loop for i values 0, 1, 2, 3, 4 for (a= 10; a >= 0; a=a-3); cout << a; for (i = 0; i < 5; i=i+1) cout << i * i; Note the semicolon here. This means empty loop The loop has empty body
  • 5. Misc Declaration of variables in the loop – Variables declared inside the loop are accessible inside the loop only. They cannot be accessed outside. This is called local scope. (This applies for the selection statements also) int c; for (c = 0; c <=10; c = c+1) { int j = c; cout << j << “ “ << c ; } cout << j << “ “<< c; Valid Invalid
  • 6. ITERATION STATEMENTS - WHILE • The syntax is • In the while loop, the control variable should be initialized outside the loop and it should be updated inside the loop while (expression) Loop body int a = 0; while (a <= 10) { cout << a; a = a + 1; } This program will print 0 to 10
  • 7. Nested Loops• A loop may contain another loop inside its body. It is called Nested Loop. It is used when one variable has to change values for each value of another variable. Output will be 1 table starts 1 x 1 = 1 1 x 2 = 2 …. 1 x 20 = 20 1 table ends 2 table starts 2 x 1 = 2 2 x 2 = 4 …. 2 x 20 = 40 2 table ends upto 10 table ends for (int i = 1 ; i <= 10; i=i+1) { cout << i << “ table starts ”; for (int j = 1 ; j <= 20; j=j+1) cout << i << “ x “ << j << “ = “ << i * j ; cout << i << “table ends” ; cout << endl; }
  • 8. Nested Loops Same program with while loop int i = j = 1; while (i <= 10) { cout << i << “table starts” ; while (j <= 20) { cout << i << “ x “ << j << “ = “ << i * j ; j = j + 1; } cout << i << “table ends” ; cout << endl; i = i + 1; }
  • 9. Nested Loop concept • Identify the varying factors • Design a loop for each varying factor • For example, for a pattern like ******* ***** *** * • The varying factors are the line numbers, the spaces in each line and the number of stars in each line. • In each line, the number of spaces varies from 1 to m • In each line, the number of stars varies from 1 to n • So the loop would look one given in the next slide Line No Total No of Spaces (m) Total No of stars (n) 1 0 7 2 1 5 3 2 3 4 3 1
  • 10. int m = 0; int n = 7; for (int i = 1 ; i <= 4; i=i+1) { for (int j = 0 ; j < m; j=j+1) { cout << “ ” ; } m = m + 1; for (int k = 1 ; k <= n; k=k+1) { cout << “*” ; } n = n – 2; cout << endl ; } Loop starts for each line Loop starts for spaces Loop starts for stars For going to the next line
  • 11. Nested Loop Example • For example, for a pattern like 1 121 12321 1234321 • The varying factors are the line numbers, the spaces in each line, the numbers increasing and the numbers decreasing. • In each line, the number of spaces varies from 1 to m • In each line, the number increases upto the line no • In each line, the number increases from the line no Line No Total No of Spaces (m) Number increasin g till Number decreasi ng from 1 3 1 - 2 2 2 1 3 1 3 2 4 0 4 3
  • 12. int m = 3; for (int i = 1 ; i <= 4; i=i+1) { for (int j = 1 ; j <= m; j=j+1) { cout << “ ” ; } m = m - 1; for (int k = 1 ; k <= i; k=k+1) { cout << k ; } for (int z = i-1 ; z >= 1; z=z-1) { cout << z ; } cout << endl ; } Loop starts for each line Loop starts for spaces Loop starts for increasing numbers For going to the next line Loop starts for decreasing numbers
  • 13. Question Paper pattern • Write a program – Simple loops • Print a series (odd numbers, even numbers, 1 2 4 8 … 1024, a …. z, A…. Z, Factorial of a number, Sum of a given series of numbers ) • Print a table (8 table) – Nested loops • Print a pattern • Print tables – Combination of if and loop
  • 14. Find the mistakes Wrong Code for (int c = 0; c >= 10; c = c + 1); cout << c; Wrong Code int a; while (a <= 10) { cout << a; } Corrected Code for (int c = 0; c <= 10; c = c + 1) cout << c; Corrected Code int a = 1; while (a <= 10) { cout << a; a = a + 1; }
  • 15. Find the Output int sum = 0; for (int c = 1; c < 10; c = c + 3) sum = sum + c; cout << sum << “ “ << c << endl; Ans: sum = 0 initially c = 1 sum = 1 in first iteration c = 4 sum = 5 in second iteration c = 7 sum = 12 in third iteration c = 10 Since c < 10, so next iteration will not go inside the loop Output will be :
  • 16. Find the Output int k = 0, c; while ( k <= 10) { c = k + 3; k = k + 2; } cout << c << “ “ << k; Ans: k = 0 initially c = 3 k = 2 in first iteration c = 5 k = 4 in second iteration c = 7 k = 6 in third iteration c = 9 k = 8 in fourth iteration c = 11 k = 10 in fifth iteration c = 13 k = 12 in sixth iteration Since k <= 10, next iteration will not go inside the loop as k becomes 12 Output will be : 13 12
  • 17. Fill in the blanks This is a program to find the sum of odd numbers till 100. int sum = _____; int i = 1; while ( _________ ) { sum = sum + i; ___________ } cout << ____________ ;