SlideShare a Scribd company logo
4
Most read
7
Most read
10
Most read
Topic : While , For , Do-While Loop
Guided By :
Branch : Batch :
Name ENROLLMENT NO.
Abhishek Chokshi 140120109005
Raj Shah
Kaveesh Raval
140120109054
140120109016
 Repetition statements allow us to execute a
statement multiple times
 Often they are referred to as loops
 Like conditional statements, they are
controlled by boolean expressions
 There are three kinds of repetition
statements:
 The syntax of while statement in C:
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.
Logical expression that determines
whether the action isto be executed
while ( Expression) Action
Action to be iteratively
performed until logical
expression isfalse
 A while statement has the following syntax:
while ( condition ){
statement;
}
• If the condition is true, the statement is executed
• Then the condition is evaluated again, and if it is still true, the
statement is executed again
• The statement is executed repeatedly until the condition becomes
false
statement
true false
condition
evaluated
 An example of a while statement:
int count = 1;
while (count <= 5){
System.out.println (count);
count++;
}
• If the condition of a while loop is false initially, the statement is
never executed
• Therefore, the body of a while loop will execute zero or more times
While , For , Do-While Loop
 The syntax of the do-while statement in
C:
do
statement
while (loop repetition condition);
 The statement is first executed.
 If the loop repetition condition is
satisfied, the statement is repeated else,
the repetition of the loop is stopped.
 Syntax
do Action
while
(Expression)
 Semantics
◦ Execute Action
◦ If Expression is true
then execute Action
again
◦ Repeat this process
until Expression
evaluates to false
 Action is either a
single statement or a
group of statements
within curly braces
Action
true
false
Expression
true
condition
evaluated
statement
false
do {
statement-1;
statement-2;
……
statement-k;
} while(condition);
Initialize
Statement-1
Statement-2
Statement-k
Is condition
TRUE?
N
Y
/* Find an even number input */
do{
printf(“Enter a value:n”);
scanf(“%d”,&num);
}while(num%2!=0)
printf(“n%d”,num);
This loop will repeat if the user inputs
odd number.
 The syntax of for statement in C:
for (initialization expression;
loop repetition condition;
update expression)
statement
 The initialization expression set the initial
value of the loop control variable.
 The loop repetition condition test the value of
the loop control variable.
 The update expression update the loop
control variable.
 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
statement
true
condition
evaluated
false
increment
initialization
 A for loop is functionally equivalent to the
following while loop structure:
initialization;
while ( condition ){
statement;
increment;
}
 An example of a for loop:
for (int count=1; count <= 5; count++){
System.out.println (count);
}
• The initialization section can be used to declare a variable
• Like a while loop, the condition of a for loop is tested prior to
executing the loop body
• Therefore, the body of a for loop will execute zero or more times
 The increment section can perform any
calculation
• A for loop is well suited for executing statements a specific
number of times that can be calculated or determined in advance
for (int num=100; num > 0; num -= 5){
System.out.println (num);
}
 Each expression in the header of a for loop is
optional
 If the initialization is left out, no initialization is
performed
 If the condition is left out, it is always considered
to be true, and therefore creates an infinite loop
◦ We usually call this a “forever loop”
 If the increment is left out, no increment
operation is performed
 Remember the break keyword that we used
to stop a switch statement from executing
more than one statement?
 break can also be used to exit an infinite
loop
 But it is almost always best to use a well-
written while loop
While , For , Do-While Loop

More Related Content

PPT
While loop
PPTX
Loops Basics
PPTX
Loop(for, while, do while) condition Presentation
PPT
BASIC THERMODYNAMICS
PPT
Logic gates
PDF
What is Metaverse ? What is Not ?
PPTX
Zener Diode Full Presentation
While loop
Loops Basics
Loop(for, while, do while) condition Presentation
BASIC THERMODYNAMICS
Logic gates
What is Metaverse ? What is Not ?
Zener Diode Full Presentation

What's hot (20)

PPTX
data types in C programming
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
Functions in c language
PPTX
Loops c++
PPTX
Control statements in c
PPTX
Loops in C Programming Language
PPTX
Loops in c language
PPTX
Pointer in c
PPTX
Conditional Statement in C Language
PPSX
Break and continue
PPT
Looping statements in Java
PPTX
C Programming: Control Structure
PDF
Java threads
PPTX
Strings in C
PPTX
Function in C program
PPT
Functions in C++
PPTX
Storage class in C Language
PPT
Control structure C++
PPTX
Functions in C
PDF
Loops and conditional statements
data types in C programming
Unit ii chapter 2 Decision making and Branching in C
Functions in c language
Loops c++
Control statements in c
Loops in C Programming Language
Loops in c language
Pointer in c
Conditional Statement in C Language
Break and continue
Looping statements in Java
C Programming: Control Structure
Java threads
Strings in C
Function in C program
Functions in C++
Storage class in C Language
Control structure C++
Functions in C
Loops and conditional statements
Ad

Similar to While , For , Do-While Loop (20)

PPT
Programming loop
PDF
Java Repetiotion Statements
PPTX
Looping statements
PPTX
Introduction to Java Programming - Lecture 11.pptx
DOCX
loops and iteration.docx
PPT
15-Loops.ppt
PPT
Visula C# Programming Lecture 4
PPTX
Loops in c programming
PPT
M C6java6
PDF
Unit II chapter 4 Loops in C
PDF
Loops in c++
PPTX
Flow of control C ++ By TANUJ
PDF
Control statements anil
PPTX
Lecture on Loop while loop for loop + program
PPTX
presentation on powerpoint template.pptx
PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PDF
whileloop-161225171903.pdf
PDF
Loop and while Loop
PPTX
C language 2
DOCX
Loops and iteration.docx
Programming loop
Java Repetiotion Statements
Looping statements
Introduction to Java Programming - Lecture 11.pptx
loops and iteration.docx
15-Loops.ppt
Visula C# Programming Lecture 4
Loops in c programming
M C6java6
Unit II chapter 4 Loops in C
Loops in c++
Flow of control C ++ By TANUJ
Control statements anil
Lecture on Loop while loop for loop + program
presentation on powerpoint template.pptx
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
whileloop-161225171903.pdf
Loop and while Loop
C language 2
Loops and iteration.docx
Ad

More from Abhishek Choksi (20)

PPTX
Industrial Instrumentation (2170913) (Variable Inductance & Capacitance Tran...
PPTX
Switch Gear & Protection (2170908) (Generator Protection)
PPTX
Design of Magnetic Circuit for Synchronous Machine
PPTX
Inter Connected Power System (2170901)
PPTX
Third harmonic pwm
PPTX
Electric welding
PPTX
Closed loop speed control
PPT
Synthesis of unsymmetrical phasors from their symmetrical components
PPT
Design of lv winding
PPT
Measurement of hvac (High Voltage Engineering )
PPT
Supply system (Electrical Power System)
PPT
Design of dc armature winding
PPT
8051 microcontroller and it’s interface
PPTX
Principle of regenerative braking and chopper configuration
PPT
Root locus
PPT
Web application attack and audit framework (w3af)
PPTX
Discrete Fourier Transform
PPTX
K - Map
PPTX
Blue led
PPTX
Mars orbiter mission
Industrial Instrumentation (2170913) (Variable Inductance & Capacitance Tran...
Switch Gear & Protection (2170908) (Generator Protection)
Design of Magnetic Circuit for Synchronous Machine
Inter Connected Power System (2170901)
Third harmonic pwm
Electric welding
Closed loop speed control
Synthesis of unsymmetrical phasors from their symmetrical components
Design of lv winding
Measurement of hvac (High Voltage Engineering )
Supply system (Electrical Power System)
Design of dc armature winding
8051 microcontroller and it’s interface
Principle of regenerative braking and chopper configuration
Root locus
Web application attack and audit framework (w3af)
Discrete Fourier Transform
K - Map
Blue led
Mars orbiter mission

Recently uploaded (20)

PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
AgentX UiPath Community Webinar series - Delhi
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
436813905-LNG-Process-Overview-Short.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPT
Project quality management in manufacturing
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
“Next-Gen AI: Trends Reshaping Our World”
Structs to JSON How Go Powers REST APIs.pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
AgentX UiPath Community Webinar series - Delhi
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
Simulation of electric circuit laws using tinkercad.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
436813905-LNG-Process-Overview-Short.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Project quality management in manufacturing
Lesson 3_Tessellation.pptx finite Mathematics
Operating System & Kernel Study Guide-1 - converted.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
“Next-Gen AI: Trends Reshaping Our World”

While , For , Do-While Loop

  • 1. Topic : While , For , Do-While Loop Guided By : Branch : Batch :
  • 2. Name ENROLLMENT NO. Abhishek Chokshi 140120109005 Raj Shah Kaveesh Raval 140120109054 140120109016
  • 3.  Repetition statements allow us to execute a statement multiple times  Often they are referred to as loops  Like conditional statements, they are controlled by boolean expressions  There are three kinds of repetition statements:
  • 4.  The syntax of while statement in C: 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.
  • 5. Logical expression that determines whether the action isto be executed while ( Expression) Action Action to be iteratively performed until logical expression isfalse
  • 6.  A while statement has the following syntax: while ( condition ){ statement; } • If the condition is true, the statement is executed • Then the condition is evaluated again, and if it is still true, the statement is executed again • The statement is executed repeatedly until the condition becomes false
  • 8.  An example of a while statement: int count = 1; while (count <= 5){ System.out.println (count); count++; } • If the condition of a while loop is false initially, the statement is never executed • Therefore, the body of a while loop will execute zero or more times
  • 10.  The syntax of the do-while statement in C: do statement while (loop repetition condition);  The statement is first executed.  If the loop repetition condition is satisfied, the statement is repeated else, the repetition of the loop is stopped.
  • 11.  Syntax do Action while (Expression)  Semantics ◦ Execute Action ◦ If Expression is true then execute Action again ◦ Repeat this process until Expression evaluates to false  Action is either a single statement or a group of statements within curly braces Action true false Expression
  • 15. /* Find an even number input */ do{ printf(“Enter a value:n”); scanf(“%d”,&num); }while(num%2!=0) printf(“n%d”,num); This loop will repeat if the user inputs odd number.
  • 16.  The syntax of for statement in C: for (initialization expression; loop repetition condition; update expression) statement  The initialization expression set the initial value of the loop control variable.  The loop repetition condition test the value of the loop control variable.  The update expression update the loop control variable.
  • 17.  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
  • 19.  A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ){ statement; increment; }
  • 20.  An example of a for loop: for (int count=1; count <= 5; count++){ System.out.println (count); } • The initialization section can be used to declare a variable • Like a while loop, the condition of a for loop is tested prior to executing the loop body • Therefore, the body of a for loop will execute zero or more times
  • 21.  The increment section can perform any calculation • A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance for (int num=100; num > 0; num -= 5){ System.out.println (num); }
  • 22.  Each expression in the header of a for loop is optional  If the initialization is left out, no initialization is performed  If the condition is left out, it is always considered to be true, and therefore creates an infinite loop ◦ We usually call this a “forever loop”  If the increment is left out, no increment operation is performed
  • 23.  Remember the break keyword that we used to stop a switch statement from executing more than one statement?  break can also be used to exit an infinite loop  But it is almost always best to use a well- written while loop