際際滷

際際滷Share a Scribd company logo
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 dont 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!
Ad

Recommended

Cocos2d Performance Tips
Cocos2d Performance Tips
Keisuke Hata
Avoiding Share Market is RISK
Avoiding Share Market is RISK
Vidyanand Thakurdesai
CQRS: Theory
CQRS: Theory
Topu Newaj
Production deployment
Production deployment
Topu Newaj
Reading (carr sanger)
Reading (carr sanger)
fisherhistoryteach
The Daisy Cookbook
The Daisy Cookbook
David Vosnacos
Retirement planning
Retirement planning
Vidyanand Thakurdesai
Maximizing LinkedIns Potential
Maximizing LinkedIns Potential
Susanne Daniels
Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.
Topu Newaj
Variables
Variables
Michael Gordon
Recipe of a rockstar developer
Recipe of a rockstar developer
Topu Newaj
Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2
Taufik Setiawan
Mapa conceptual gerencia y ciclo de vida de los proyectos
maryortizh25
CQRS : Introduction
CQRS : Introduction
Topu Newaj
个 2016. 亠仄亠仆亠于舒
个 2016. 亠仄亠仆亠于舒
Oksana Dementava
Thumb Rules for Personal Finance
Thumb Rules for Personal Finance
Vidyanand Thakurdesai
Are you saving or investing
Are you saving or investing
Vidyanand Thakurdesai
Planning for child future
Planning for child future
Vidyanand Thakurdesai
Kooperimi ne bujqesi
Kooperimi ne bujqesi
Erland Dalliu
PPF or ELSS Mutual Funds
PPF or ELSS Mutual Funds
Vidyanand Thakurdesai
While loops
While loops
Michael Gordon
ICP - Lecture 9
ICP - Lecture 9
hassaanciit
For Loops and Nesting in Python
For Loops and Nesting in Python
primeteacher32
Loops Do While Arduino Programming Robotics
Loops Do While Arduino Programming Robotics
JhaeZaSangcapGarrido
Loops
Loops
SAMYAKKHADSE
Introduction to Repetition Structures
Introduction to Repetition Structures
primeteacher32
CSE-1203-Lecture-07-Loops-Part-II. c pptx
CSE-1203-Lecture-07-Loops-Part-II. c pptx
MARaihanEmon
Introduction to programming - class 4
Introduction to programming - class 4
Paul Brebner
Iteration
Iteration
Liam Dunphy
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
eShikshak

More Related Content

Viewers also liked (12)

Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.
Topu Newaj
Variables
Variables
Michael Gordon
Recipe of a rockstar developer
Recipe of a rockstar developer
Topu Newaj
Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2
Taufik Setiawan
Mapa conceptual gerencia y ciclo de vida de los proyectos
maryortizh25
CQRS : Introduction
CQRS : Introduction
Topu Newaj
个 2016. 亠仄亠仆亠于舒
个 2016. 亠仄亠仆亠于舒
Oksana Dementava
Thumb Rules for Personal Finance
Thumb Rules for Personal Finance
Vidyanand Thakurdesai
Are you saving or investing
Are you saving or investing
Vidyanand Thakurdesai
Planning for child future
Planning for child future
Vidyanand Thakurdesai
Kooperimi ne bujqesi
Kooperimi ne bujqesi
Erland Dalliu
PPF or ELSS Mutual Funds
PPF or ELSS Mutual Funds
Vidyanand Thakurdesai
Introducing Selise: Developers Paradise.
Introducing Selise: Developers Paradise.
Topu Newaj
Recipe of a rockstar developer
Recipe of a rockstar developer
Topu Newaj
Tugas praktik bahasa rakitan 2
Tugas praktik bahasa rakitan 2
Taufik Setiawan
Mapa conceptual gerencia y ciclo de vida de los proyectos
maryortizh25
CQRS : Introduction
CQRS : Introduction
Topu Newaj
个 2016. 亠仄亠仆亠于舒
个 2016. 亠仄亠仆亠于舒
Oksana Dementava
Kooperimi ne bujqesi
Kooperimi ne bujqesi
Erland Dalliu

Similar to For loops (20)

While loops
While loops
Michael Gordon
ICP - Lecture 9
ICP - Lecture 9
hassaanciit
For Loops and Nesting in Python
For Loops and Nesting in Python
primeteacher32
Loops Do While Arduino Programming Robotics
Loops Do While Arduino Programming Robotics
JhaeZaSangcapGarrido
Loops
Loops
SAMYAKKHADSE
Introduction to Repetition Structures
Introduction to Repetition Structures
primeteacher32
CSE-1203-Lecture-07-Loops-Part-II. c pptx
CSE-1203-Lecture-07-Loops-Part-II. c pptx
MARaihanEmon
Introduction to programming - class 4
Introduction to programming - class 4
Paul Brebner
Iteration
Iteration
Liam Dunphy
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
eShikshak
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
Loops in c
Loops in c
RekhaBudhwar
C# Loops
C# Loops
Hock Leng PUAH
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
Cs1123 6 loops
Cs1123 6 loops
TAlha MAlik
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
muhammadammarqaiser
C loops
C loops
sunilchute1
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
while and for Loops
while and for Loops
Ray0uni
Loop
Loop
MdEmonRana
ICP - Lecture 9
ICP - Lecture 9
hassaanciit
For Loops and Nesting in Python
For Loops and Nesting in Python
primeteacher32
Loops Do While Arduino Programming Robotics
Loops Do While Arduino Programming Robotics
JhaeZaSangcapGarrido
Introduction to Repetition Structures
Introduction to Repetition Structures
primeteacher32
CSE-1203-Lecture-07-Loops-Part-II. c pptx
CSE-1203-Lecture-07-Loops-Part-II. c pptx
MARaihanEmon
Introduction to programming - class 4
Introduction to programming - class 4
Paul Brebner
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
eShikshak
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
LOOP STATEMENTS AND TYPES OF LOOP IN C LANGUAGE BY RIZWAN
MD RIZWAN MOLLA
DSA 103 Object Oriented Programming :: Week 3
DSA 103 Object Oriented Programming :: Week 3
Ferdin Joe John Joseph PhD
Cs1123 6 loops
Cs1123 6 loops
TAlha MAlik
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
slidesgo-understanding-the-for-loop-in-c-principles-and-applications-20241116...
muhammadammarqaiser
while and for Loops
while and for Loops
Ray0uni
Ad

More from Michael Gordon (16)

Raspberry Pi presentation for Computer Architecture class
Raspberry Pi presentation for Computer Architecture class
Michael Gordon
Strings2
Strings2
Michael Gordon
Strings1
Strings1
Michael Gordon
Introduction to Computer Science 111 Lab
Introduction to Computer Science 111 Lab
Michael Gordon
Introduction to Linux, Pico, G++
Introduction to Linux, Pico, G++
Michael Gordon
Strings
Strings
Michael Gordon
Arrays, continued
Arrays, continued
Michael Gordon
Arrays
Arrays
Michael Gordon
Scope of variables
Scope of variables
Michael Gordon
Functions
Functions
Michael Gordon
Shape logic 1
Shape logic 1
Michael Gordon
If statements
If statements
Michael Gordon
Arithmetic
Arithmetic
Michael Gordon
Output
Output
Michael Gordon
Word cloud
Word cloud
Michael Gordon
Millennial white paper
Millennial white paper
Michael Gordon
Ad

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 dont 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!