SlideShare a Scribd company logo
1 of 24
Download to read offline
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
cpu.pdf

More Related Content

Similar to cpu.pdf

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 CSowmya Jyothi
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdfBasirKhan21
 
While loop
While loopWhile loop
While loopFeras_83
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while LoopJayBhavsar68
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 

Similar to cpu.pdf (20)

Loops c++
Loops c++Loops c++
Loops c++
 
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
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
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
 
While loop
While loopWhile loop
While loop
 
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++
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Control structure
Control structureControl structure
Control structure
 
Loop structures
Loop structuresLoop structures
Loop structures
 

Recently uploaded

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....rightmanforbloodline
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

cpu.pdf

  • 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