SlideShare a Scribd company logo
1
Chapter 03
2
Loops
3
Outline
• Introduction
• for Loop
• while Loop
• do while Loop
• Nested Loops
• Values of functions and assignment
expressions
• break and continue Statements
4
Introduction
• A loop causes a section of program to be repeated a
certain number of times
• The repetition continues till the condition remains
true: When the condition becomes false the loops
exits and the control passes to the statements
following the loop.
• C++ allows (03) three kinds of loops:
– FOR
– WHILE –
DO … WHILE
for Loop
• The for loop
executes a section
of code a fixed
number of times
• It is usually
(although not
always) used when
it is known in
advance how many
times the code
should be
executed.
j is loop variable
for Loop Operation
Exit Loop
for Loop: Example
for Loop: Exercises
1. Square all numbers from 1 to 10
2. Write a program that generates the following table:
2 x 1 = 2 2 x 2 =
4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
3. Print even and odd number from 0 and 1 respectively up to
20
4. Write a program that displays the output:
WHILE Loop
n is loop variable
• A while loop
repeatedly executes a
target statement as
long as a given
condition is true.
WHILE Loop Operation
Exit Loop
WHILE Loop: Example
#include <stdio.h>
#include <conio.h>
int main()
{
int n = 99;
while (n!=0) scanf(“%d”,
&n); getch();
}
DO … WHILE Loop
Unlike FOR and
WHILE loops, which test
the loop condition at the top
of the loop, the DO … WHILE
loop checks its condition at
the bottom of the loop.
The DO … WHILE loop is used
when it is guaranteed that
the loop body will be
executed at least once, even
if the test expression initially
returns false.
DO…WHILE Loop Operation
Exit Loop

More Related Content

DOCX
PPTX
PPTX
MATLAB Programming - Loop Control Part 2
PPTX
What is to loop in c++
DOCX
Instrumentation and measurements
PPTX
C# Loops
PDF
Chapter 3
PPTX
For loop java 2
MATLAB Programming - Loop Control Part 2
What is to loop in c++
Instrumentation and measurements
C# Loops
Chapter 3
For loop java 2

What's hot (7)

PPTX
queue
PPTX
Python project2
PPT
PDF
Mean square error
PPTX
Python project
PPTX
Do...until loop structure
PPTX
Introduction to programming in MATLAB
queue
Python project2
Mean square error
Python project
Do...until loop structure
Introduction to programming in MATLAB
Ad

Similar to Loop Introduction for Loop while Loop do while Loop Nested Loops Values of functions and assignment expressions break and continue Statements (20)

PPTX
Loops c++
PPTX
Looping statements
PDF
LEC 5 [CS 101] Introduction to computer science.pdf
PPTX
Going loopy - Introduction to Loops.pptx
PPTX
Loops in C programming language/ Types of loops
PPTX
Introduction to Java Programming - Lecture 11.pptx
PPTX
C language (Part 2)
PDF
Repetition, Basic loop structures, Loop programming techniques
PPTX
Loop(for, while, do while) condition Presentation
PPTX
Introduction to Repetition Structures
PPTX
python ppt.pptx
PPTX
LOOPS IN PROGRAMMING - Muqaddas Bin Tahir .pptx
PPT
ch5.ppt
PPTX
Loop Statements TanCollege Programming course.pptx
PPTX
Iterative control structures, looping, types of loops, loop working
PDF
1660213363910.pdf
PPT
Programming loop
PPTX
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
PPTX
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
PPTX
Loops in python.pptx/ introduction to loops in python
Loops c++
Looping statements
LEC 5 [CS 101] Introduction to computer science.pdf
Going loopy - Introduction to Loops.pptx
Loops in C programming language/ Types of loops
Introduction to Java Programming - Lecture 11.pptx
C language (Part 2)
Repetition, Basic loop structures, Loop programming techniques
Loop(for, while, do while) condition Presentation
Introduction to Repetition Structures
python ppt.pptx
LOOPS IN PROGRAMMING - Muqaddas Bin Tahir .pptx
ch5.ppt
Loop Statements TanCollege Programming course.pptx
Iterative control structures, looping, types of loops, loop working
1660213363910.pdf
Programming loop
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
C_BASICS FOR C PROGRAMMER WITH SRIVATHS P
Loops in python.pptx/ introduction to loops in python
Ad

More from imtiazalijoono (20)

PDF
Embedded systems io programming
PDF
Embedded systems tools & peripherals
PPTX
Importance of reading and its types.
PPTX
Negative amplifiers and its types Positive feedback and Negative feedback
PPTX
Multistage amplifiers and Name of coupling Name of multistage amplifier
PDF
Programming Fundamentals and basic knowledge
PDF
Programming Fundamentals Functions in C and types
PDF
Software Development Software development process
PDF
Programming Fundamentals Decisions
PDF
C Building Blocks
PDF
Programming Fundamentals Arrays and Strings
PPTX
Programming Fundamentals and Programming Languages Concepts Translators
PPTX
Programming Fundamentals and Programming Languages Concepts
DOCX
Programming Global variable
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
PPTX
Arithmetic and Arithmetic assignment operators
PPTX
INTRODUCTION TO COMPUTER PROGRAMMING
PPTX
COMPUTER PROGRAMMING
PPTX
COMPUTER PROGRAMMING
Embedded systems io programming
Embedded systems tools & peripherals
Importance of reading and its types.
Negative amplifiers and its types Positive feedback and Negative feedback
Multistage amplifiers and Name of coupling Name of multistage amplifier
Programming Fundamentals and basic knowledge
Programming Fundamentals Functions in C and types
Software Development Software development process
Programming Fundamentals Decisions
C Building Blocks
Programming Fundamentals Arrays and Strings
Programming Fundamentals and Programming Languages Concepts Translators
Programming Fundamentals and Programming Languages Concepts
Programming Global variable
Array Introduction One-dimensional array Multidimensional array
NTRODUCTION TO COMPUTER PROGRAMMING Loop as repetitive statement,
Arithmetic and Arithmetic assignment operators
INTRODUCTION TO COMPUTER PROGRAMMING
COMPUTER PROGRAMMING
COMPUTER PROGRAMMING

Recently uploaded (20)

PDF
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PDF
From loneliness to social connection charting
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PPTX
IMMUNIZATION PROGRAMME pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
LDMMIA Reiki Yoga Workshop 15 MidTerm Review
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Open folder Downloads.pdf yes yes ges yes
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
How to Manage Loyalty Points in Odoo 18 Sales
Module 3: Health Systems Tutorial Slides S2 2025
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
From loneliness to social connection charting
Cardiovascular Pharmacology for pharmacy students.pptx
IMMUNIZATION PROGRAMME pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Final Stretch: How to Release a Game and Not Die in the Process.

Loop Introduction for Loop while Loop do while Loop Nested Loops Values of functions and assignment expressions break and continue Statements

  • 3. 3 Outline • Introduction • for Loop • while Loop • do while Loop • Nested Loops • Values of functions and assignment expressions • break and continue Statements
  • 4. 4 Introduction • A loop causes a section of program to be repeated a certain number of times • The repetition continues till the condition remains true: When the condition becomes false the loops exits and the control passes to the statements following the loop. • C++ allows (03) three kinds of loops: – FOR – WHILE – DO … WHILE
  • 5. for Loop • The for loop executes a section of code a fixed number of times • It is usually (although not always) used when it is known in advance how many times the code should be executed.
  • 6. j is loop variable
  • 9. for Loop: Example for Loop: Exercises 1. Square all numbers from 1 to 10
  • 10. 2. Write a program that generates the following table: 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 3. Print even and odd number from 0 and 1 respectively up to 20 4. Write a program that displays the output: WHILE Loop n is loop variable
  • 11. • A while loop repeatedly executes a target statement as long as a given condition is true.
  • 13. WHILE Loop: Example #include <stdio.h> #include <conio.h> int main() { int n = 99; while (n!=0) scanf(“%d”, &n); getch(); } DO … WHILE Loop
  • 14. Unlike FOR and WHILE loops, which test the loop condition at the top of the loop, the DO … WHILE loop checks its condition at the bottom of the loop. The DO … WHILE loop is used when it is guaranteed that the loop body will be executed at least once, even if the test expression initially returns false.