SlideShare a Scribd company logo
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 << ____________ ;

More Related Content

What's hot

C++ programming
C++ programmingC++ programming
C++ programming
viancagerone
 
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
Gagan Deep
 
Looping statements
Looping statementsLooping statements
Looping statements
Chukka Nikhil Chakravarthy
 
Iteration
IterationIteration
Iteration
Liam Dunphy
 
Looping
LoopingLooping
Nested loops
Nested loopsNested loops
Nested loops
Adnan Ferdous Ahmed
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
deekshagopaliya
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
Jd Mercado
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
Rahul Sahu
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
ShifatiRabbi
 
For Loop
For LoopFor Loop
For Loop
Ghaffar Khan
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 
Loops in R
Loops in RLoops in R
Loops in R
Chris Orwa
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
primeteacher32
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
 
Loops in c
Loops in cLoops in c
Java Programming: Loops
Java Programming: LoopsJava Programming: Loops
Java Programming: Loops
Karwan Mustafa Kareem
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
Muhammad Uzair Rasheed
 

What's hot (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
 

Viewers also liked

Loops in C Programming
Loops in C ProgrammingLoops in C Programming
Loops in C Programming
Himanshu Negi
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
C++ loop
C++ loop C++ loop
C++ loop
Khelan Ameen
 
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
Tech
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
Loops in C
Loops in CLoops in C
Loops in C
Kamal Acharya
 
c++ for loops
c++ for loopsc++ for loops
c++ for loops
MOHAMMED ALZAYLAEE
 
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 looping
Dhani 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 cases
MeoRamos
 
[C++]3 loop statement
[C++]3 loop statement[C++]3 loop statement
[C++]3 loop statement
Junyoung Jung
 
The Loops
The LoopsThe Loops
The Loops
Krishma Parekh
 
Loops
LoopsLoops
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 Basics
John Todora
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
Mushiii
 
Loops
LoopsLoops
Charging System Automobile
Charging System AutomobileCharging System Automobile
Charging System Automobile
Joren Carcallas
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 

Viewers also liked (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
 

Similar to C++ control loops

Ch4
Ch4Ch4
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
TAlha MAlik
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
sanjay
 
Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.ppt
RahulBorate10
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
C++ Programming Club-Lecture 3
C++ Programming Club-Lecture 3C++ Programming Club-Lecture 3
C++ Programming Club-Lecture 3
Ammara Javed
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 
Control structures.ppt
Control structures.pptControl structures.ppt
Control structures.ppt
Rahul Borate
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
Shameer Ahmed Koya
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Mohammed Khan
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
NaumanRasheed11
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
khaledahmed316
 
C# Loops
C# LoopsC# Loops
C# Loops
guestae0484
 
06.Loops
06.Loops06.Loops
06.Loops
Intro C# Book
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Mohammed Khan
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
Ayman Hassan
 
12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx12-Lec - Repetition For Loop.pptx
12-Lec - Repetition For Loop.pptx
AqeelAbbas94
 
Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014Verilog Lecture3 hust 2014
Verilog Lecture3 hust 2014
Béo Tú
 

Similar to 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
 

Recently uploaded

Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
GDSC PJATK
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
flufftailshop
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 

Recently uploaded (20)

Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdfNunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
Nunit vs XUnit vs MSTest Differences Between These Unit Testing Frameworks.pdf
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 

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 << ____________ ;