SlideShare una empresa de Scribd logo
1 de 6
Descargar para leer sin conexión
CS111 Lab
Iteration: for loops
Instructor: Michael Gordon
for loop structure
 Think of a for loop as a special kind of
while loop with initialization, comparison,
and incrementation built in.
 for (int i = 0; i<n; i++){
instructions…
}
Simplest for loops
 The previous slide shows one of the most
common types of for loops.
 Initialize an integer (i) to zero.
 Increment by one (i++)
 As long as i<n (i is less than some number n)
 To count backwards from n, you could do:
 for (int i = n; i<0; i--)
 In addition to incrementing you can do:
 for (int i = n; i<0; i=i+5) etc.
When to use for loops
 Use while loops when you don’t know the
number of iterations (such as if the
condition to check depends on the user).
 Use for loops if you know how many times
you need to loop
Nested for loops
 You can “nest” one for loop (or more)
inside of another for loop.
 for (int i = 1; i<=2; i++){
for (int j = 1; j<=2; j++){
instructions…
}
}
Nested for loops
 The “i” loop runs twice and the “j” loop
runs twice each time the “i“ loop runs.
 for (int i = 1; i<=2; i++){
for (int j = 1; j<=2; j++){
instructions…
}
}
 Make sure to use different variables!

Más contenido relacionado

Destacado

Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.Topu Newaj
 
Recipe of a rockstar developer
Recipe of a rockstar developerRecipe of a rockstar developer
Recipe of a rockstar developerTopu Newaj
 
Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2Taufik Setiawan
 
Mapa conceptual gerencia y ciclo de vida de los proyectos
Mapa conceptual gerencia y ciclo de vida de los proyectosMapa conceptual gerencia y ciclo de vida de los proyectos
Mapa conceptual gerencia y ciclo de vida de los proyectosmaryortizh25
 
CQRS : Introduction
CQRS : IntroductionCQRS : Introduction
CQRS : IntroductionTopu Newaj
 
РИФ 2016. Дементьева
РИФ 2016. ДементьеваРИФ 2016. Дементьева
РИФ 2016. ДементьеваOksana Dementava
 
Kooperimi ne bujqesi
Kooperimi ne bujqesiKooperimi ne bujqesi
Kooperimi ne bujqesiErland Dalliu
 

Destacado (12)

Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.
 
Variables
VariablesVariables
Variables
 
Recipe of a rockstar developer
Recipe of a rockstar developerRecipe of a rockstar developer
Recipe of a rockstar developer
 
Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2
 
Mapa conceptual gerencia y ciclo de vida de los proyectos
Mapa conceptual gerencia y ciclo de vida de los proyectosMapa conceptual gerencia y ciclo de vida de los proyectos
Mapa conceptual gerencia y ciclo de vida de los proyectos
 
CQRS : Introduction
CQRS : IntroductionCQRS : Introduction
CQRS : Introduction
 
РИФ 2016. Дементьева
РИФ 2016. ДементьеваРИФ 2016. Дементьева
РИФ 2016. Дементьева
 
Thumb Rules for Personal Finance
Thumb Rules for Personal FinanceThumb Rules for Personal Finance
Thumb Rules for Personal Finance
 
Are you saving or investing
Are you saving or investingAre you saving or investing
Are you saving or investing
 
Planning for child future
Planning for child futurePlanning for child future
Planning for child future
 
Kooperimi ne bujqesi
Kooperimi ne bujqesiKooperimi ne bujqesi
Kooperimi ne bujqesi
 
PPF or ELSS Mutual Funds
PPF or ELSS Mutual FundsPPF or ELSS Mutual Funds
PPF or ELSS Mutual Funds
 

Similar a For loops

Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loopsmcollison
 
1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]tototo147
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxmonicafrancis71118
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loopsbsdeol28
 

Similar a For loops (10)

06 Loops
06 Loops06 Loops
06 Loops
 
06.Loops
06.Loops06.Loops
06.Loops
 
Pi j1.4 loops
Pi j1.4 loopsPi j1.4 loops
Pi j1.4 loops
 
1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]1.3 core programming [identify the appropriate method for handling repetition]
1.3 core programming [identify the appropriate method for handling repetition]
 
Iteration
IterationIteration
Iteration
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
algorithm_analysis2
algorithm_analysis2algorithm_analysis2
algorithm_analysis2
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
 
Loops
LoopsLoops
Loops
 

Más de Michael Gordon (17)

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
 
While loops
While loopsWhile loops
While loops
 
If statements
If statementsIf statements
If statements
 
Arithmetic
ArithmeticArithmetic
Arithmetic
 
Output
OutputOutput
Output
 
Word cloud
Word cloudWord cloud
Word cloud
 
Millennial white paper
Millennial white paperMillennial white paper
Millennial white paper
 

For loops

  • 1. CS111 Lab Iteration: for loops Instructor: Michael Gordon
  • 2. for loop structure  Think of a for loop as a special kind of while loop with initialization, comparison, and incrementation built in.  for (int i = 0; i<n; i++){ instructions… }
  • 3. Simplest for loops  The previous slide shows one of the most common types of for loops.  Initialize an integer (i) to zero.  Increment by one (i++)  As long as i<n (i is less than some number n)  To count backwards from n, you could do:  for (int i = n; i<0; i--)  In addition to incrementing you can do:  for (int i = n; i<0; i=i+5) etc.
  • 4. When to use for loops  Use while loops when you don’t know the number of iterations (such as if the condition to check depends on the user).  Use for loops if you know how many times you need to loop
  • 5. Nested for loops  You can “nest” one for loop (or more) inside of another for loop.  for (int i = 1; i<=2; i++){ for (int j = 1; j<=2; j++){ instructions… } }
  • 6. Nested for loops  The “i” loop runs twice and the “j” loop runs twice each time the “i“ loop runs.  for (int i = 1; i<=2; i++){ for (int j = 1; j<=2; j++){ instructions… } }  Make sure to use different variables!