SlideShare una empresa de Scribd logo
1 de 24
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
 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

Más contenido relacionado

La actualidad más candente (20)

The Loops
The LoopsThe Loops
The Loops
 
Loops in c
Loops in cLoops in c
Loops in c
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
String functions in C
String functions in CString functions in C
String functions in C
 
Loops in C
Loops in CLoops in C
Loops in C
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
C tokens
C tokensC tokens
C tokens
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Operators in java
Operators in javaOperators in java
Operators in java
 

Similar a While , For , Do-While Loop

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

Programming loop
Programming loopProgramming loop
Programming loop
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Looping statements
Looping statementsLooping statements
Looping statements
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
15-Loops.ppt
15-Loops.ppt15-Loops.ppt
15-Loops.ppt
 
Visula C# Programming Lecture 4
Visula C# Programming Lecture 4Visula C# Programming Lecture 4
Visula C# Programming Lecture 4
 
M C6java6
M C6java6M C6java6
M C6java6
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
C language 2
C language 2C language 2
C language 2
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Loops in c
Loops in cLoops in c
Loops in c
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 

Más de Abhishek Choksi

Industrial Instrumentation (2170913) (Variable Inductance & Capacitance Tran...
Industrial Instrumentation (2170913)  (Variable Inductance & Capacitance Tran...Industrial Instrumentation (2170913)  (Variable Inductance & Capacitance Tran...
Industrial Instrumentation (2170913) (Variable Inductance & Capacitance Tran...Abhishek Choksi
 
Switch Gear & Protection (2170908) (Generator Protection)
Switch Gear & Protection (2170908) (Generator Protection)Switch Gear & Protection (2170908) (Generator Protection)
Switch Gear & Protection (2170908) (Generator Protection)Abhishek Choksi
 
Design of Magnetic Circuit for Synchronous Machine
Design of Magnetic Circuit for Synchronous MachineDesign of Magnetic Circuit for Synchronous Machine
Design of Magnetic Circuit for Synchronous MachineAbhishek Choksi
 
Inter Connected Power System (2170901)
Inter Connected Power System (2170901)Inter Connected Power System (2170901)
Inter Connected Power System (2170901)Abhishek Choksi
 
Closed loop speed control
Closed loop speed controlClosed loop speed control
Closed loop speed controlAbhishek Choksi
 
Synthesis of unsymmetrical phasors from their symmetrical components
Synthesis of unsymmetrical phasors from their symmetrical componentsSynthesis of unsymmetrical phasors from their symmetrical components
Synthesis of unsymmetrical phasors from their symmetrical componentsAbhishek Choksi
 
Measurement of hvac (High Voltage Engineering )
Measurement  of  hvac (High Voltage Engineering )Measurement  of  hvac (High Voltage Engineering )
Measurement of hvac (High Voltage Engineering )Abhishek Choksi
 
Supply system (Electrical Power System)
Supply system (Electrical Power System)Supply system (Electrical Power System)
Supply system (Electrical Power System)Abhishek Choksi
 
Design of dc armature winding
Design of dc armature windingDesign of dc armature winding
Design of dc armature windingAbhishek Choksi
 
8051 microcontroller and it’s interface
8051 microcontroller and it’s interface8051 microcontroller and it’s interface
8051 microcontroller and it’s interfaceAbhishek Choksi
 
Principle of regenerative braking and chopper configuration
Principle of regenerative braking and chopper configurationPrinciple of regenerative braking and chopper configuration
Principle of regenerative braking and chopper configurationAbhishek Choksi
 
Web application attack and audit framework (w3af)
Web application attack and audit framework (w3af)Web application attack and audit framework (w3af)
Web application attack and audit framework (w3af)Abhishek Choksi
 
Discrete Fourier Transform
Discrete Fourier TransformDiscrete Fourier Transform
Discrete Fourier TransformAbhishek Choksi
 

Más de Abhishek Choksi (20)

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

Último

Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxNadaHaitham1
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwaitjaanualu31
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 

Último (20)

Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 

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
  • 9.
  • 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