SlideShare ist ein Scribd-Unternehmen logo
1 von 4
Downloaden Sie, um offline zu lesen
CS111 Lab
while loops
Instructor: Michael Gordon
Repeating instructions
 To repeat a set of instructions, we can use
a while loop, which checks a condition
(like an if statement) and continues as
long as the condition is true.
 while (condition to check){
instructions;
}
A word of advice
 If the while loop checks a certain variable,
make sure that:
 A) The variable has a value before the loop
starts.
 B) That variable can change inside the loop.
 x = 1;
 while (x<5){“output”} will never end
 while (x<5) {x++; “output”} will stop once x is 5.
Incrementing
 Incrementing (+1) and decrementing (-1)
are useful tools for iterating in loops.
 int x = 0;
 x++; //means x = x+1; called post-increment
 ++x; // means x = x+1; called pre-increment
 x+=1; // means x = x+1;
 Decrementing:
 --x;
 x--;

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Java loops
Java loopsJava loops
Java loops
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
 
Loops c++
Loops c++Loops c++
Loops c++
 
Programming loop
Programming loopProgramming loop
Programming loop
 
C++ loop
C++ loop C++ loop
C++ loop
 
The Loops
The LoopsThe Loops
The Loops
 
types of loops and what is loop
types of loops and what is looptypes of loops and what is loop
types of loops and what is loop
 
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
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
PRESENTATION ON FOR LOOP
PRESENTATION  ON FOR LOOPPRESENTATION  ON FOR LOOP
PRESENTATION ON FOR LOOP
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Loops in c
Loops in cLoops in c
Loops in c
 
Looping
LoopingLooping
Looping
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Loops in C
Loops in CLoops in C
Loops in C
 
Loop
LoopLoop
Loop
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 

Ähnlich wie While loops

Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingNeeru Mittal
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlabTUOS-Sam
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdfBasirKhan21
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executionseShikshak
 
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
 

Ähnlich wie While loops (20)

M C6java6
M C6java6M C6java6
M C6java6
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Loops and iteration.docx
Loops and iteration.docxLoops and iteration.docx
Loops and iteration.docx
 
Iteration
IterationIteration
Iteration
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Control statements
Control statementsControl statements
Control statements
 
Loops in matlab
Loops in matlabLoops in matlab
Loops in matlab
 
Looping in c language
Looping in c languageLooping in c language
Looping in c language
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
whileloop-161225171903.pdf
whileloop-161225171903.pdfwhileloop-161225171903.pdf
whileloop-161225171903.pdf
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
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
 

Mehr von Michael Gordon (18)

Raspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture classRaspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture class
 
Strings2
Strings2Strings2
Strings2
 
Strings1
Strings1Strings1
Strings1
 
Introduction to Computer Science 111 Lab
Introduction to Computer Science 111 LabIntroduction to Computer Science 111 Lab
Introduction to Computer Science 111 Lab
 
Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++
 
Strings
StringsStrings
Strings
 
Arrays, continued
Arrays, continuedArrays, continued
Arrays, continued
 
Arrays
ArraysArrays
Arrays
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
Functions
FunctionsFunctions
Functions
 
Shape logic 1
Shape logic 1Shape logic 1
Shape logic 1
 
For loops
For loopsFor loops
For loops
 
If statements
If statementsIf statements
If statements
 
Arithmetic
ArithmeticArithmetic
Arithmetic
 
Variables
VariablesVariables
Variables
 
Output
OutputOutput
Output
 
Word cloud
Word cloudWord cloud
Word cloud
 
Millennial white paper
Millennial white paperMillennial white paper
Millennial white paper
 

While loops

  • 2. Repeating instructions  To repeat a set of instructions, we can use a while loop, which checks a condition (like an if statement) and continues as long as the condition is true.  while (condition to check){ instructions; }
  • 3. A word of advice  If the while loop checks a certain variable, make sure that:  A) The variable has a value before the loop starts.  B) That variable can change inside the loop.  x = 1;  while (x<5){“output”} will never end  while (x<5) {x++; “output”} will stop once x is 5.
  • 4. Incrementing  Incrementing (+1) and decrementing (-1) are useful tools for iterating in loops.  int x = 0;  x++; //means x = x+1; called post-increment  ++x; // means x = x+1; called pre-increment  x+=1; // means x = x+1;  Decrementing:  --x;  x--;