Professional Documents
Culture Documents
Intro To Iteration
Intro To Iteration
Iteration
Introduction to iteration
So far in computer science, you have learned how to write blocks of code that
perform certain tasks
What if you wanted to perform the same task over and over
again?
With the knowledge we have right now, that would be copying and pasting the
same lines of code, but that is NOT the most effective method
What if we can perform a task ANY number of times
while keeping the number of lines of code almost the
same?
For loop
In this for loop, we create a variable called i in the beginning and set it equal to 0. Inside the loop, we
print what i is currently equal to. Then we increase i by 1
This loop keeps repeating until i becomes 15. When i becomes 15, the condition at the top of the loop is
false, so the loop stops running
a for loop
a while loop
What is a for loop?
A for loop is a loop you put outside your code block that is pre-set to run a certain amount of
times and stops after that number has been reached
You just specify a condition and your loop just keeps running until that condition becomes
false
A while loop
The same example we did with the for loop- we can
actually write it as a while loop!
int i=0
while (i<15) {print i;i++;}
Key differences
Here are the key differences between for and while loops. We actually had to declare the variable for
the condition outside the loop
We also must add a line inside the loop to increment i. The for loop would automatically do it for us,
but since it’s a while loop, we have to add i++, otherwise the while loop will NEVER STOP running,
and we’ll run into an infinite loop
int i=0
while (i<15) {print i;i++;}
For loops immediately stop after a certain amount
of time, but you have to MAKE SURE that your
while loop finishes
CREDITS: This presentation template was created by Slidesgo, and includes icons by
Flaticon, and infographics & images by Freepik and content by Swetha Tandri
For more information about editing slides, please read our FAQs or visit our blog:
https://slidesgo.com/faqs and https://slidesgo.com/slidesgo-school
Instructions for use (premium users)
As a Premium user, you can use this template without attributing Slidesgo or keeping the "Thanks" slide.
For more information about editing slides, please read our FAQs or visit our blog:
https://slidesgo.com/faqs and https://slidesgo.com/slidesgo-school
Fonts & colors used
This presentation has been made using the following fonts:
Syne Bold
(https://fonts.google.com/specimen/Syne)
Poppins Regular
(https://fonts.google.com/specimen/Poppins)
PHASE 1
Task 1
Task 2
PHASE 2
Task 1
Task 2
PHASE 1
Task 1
Task 2
...and our sets of editable icons
You can resize these icons without losing quality.
You can change the stroke and fill color; just select the icon and click on the paint bucket/pen.
In Google Slides, you can also use Flaticon’s extension, allowing you to customize and add even more icons.
Educational Icons Medical Icons
Business Icons Teamwork Icons
Help & Support Icons Avatar Icons
Creative Process Icons Performing Arts Icons
Nature Icons
SEO & Marketing Icons
Premium infographics
“Venus is the
“Neptune is the “Saturn is a gas
second planet from
farthest planet from giant and has
the Sun and is
the Sun” several rings”
terribly hot”
75%
“Jupiter is the
TITLE 1 biggest planet in the
entire Solar System”
“Mercury is the
smallest planet in the
Solar System”
“Saturn is a gas
giant and has
several rings”
Premium Icons
Digital Marketing
Premium Icons
Online Learning
Premium Icons
Laboratory
Premium Icons
Goals & Results
Premium Icons
Infographic Elements