The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

πŸ•Ή

Unit 4

4.2 For Loops

3 min readβ€’november 16, 2020

Peter Cao


Introduction to For Loops

The second type of loop that we will learn is the for loop. The for loop allows us to simplify some while loops. Although for loops can be used for any while loop, they are mostly used when the number of repetitions is fixed. The most important part of the for loop is the for loop header, where there is a variable initialization, a conditional expression, and an incrementer (decrements work here too!) Here is the for loop's anatomy for the "fixed number of repetitions" situation:
for (int i = 0; i < numberOfTimesToRepeat; i++) { thing to be repeated }
The initialization is the int i = 0, the conditional expression is the i < numberOfTimesToRepeat, and the incrementer is the i++. The initialization is done before the loop itself runs, and the incrementer is done at the end of the iteration and before the next. As with while loops, the conditional expression determines when the loop stops.
All for loops can be converted into while loops. For the loop structure above, here would be how we do it:
int i = 0; while (i < numberOfTimesToRepeat) { thing to be repeated i++; }
As we can see, these both do the same thing, but what takes the while loop 5 lines can be simplified to 3 lines in the for loop. For example, here is code that prints the first 10 even numbers as both a for loop and a while loop:
for (int i = 0; i < 10; i++) { System.out.println(i * 2); } int j = 0; while (j < 10) { System.out.println(j * 2); j++; }
For loops are used for situations where code is run for a fixed number of times. On the other hand, while loops run until a condition is met but don't know when this will happen or how many tries it will take for the loop to exit. For loops are more concise in situations where they can be used, but for loops and while loops for the same situation will take the same time to run. However, while loops in the end are more versatile than for loops with more uses. If there is a situation involving loops that you don't know how to implement at first, first try using while loops, and when you have your solution, see if you can implement it as a for loop.
In Unit 6 and 7, we will learn another important application of for loops β€” to traverse arrays and ArrayLists.

Example: Fibonacci Numbers

Using for loops, we can make a program that prints out the first n Fibonacci numbers as follows:
public static void printNFibonacci(int n) { int a = 0; int b = 1; for (int i = 0; i < n; i++) { fib = a + b; a = b; b = fib; System.out.println(fib); } }

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
πŸ™
Exam Reviews
πŸ–±
Unit 10: Recursion
βž•
Unit 1: Primitive Types
πŸ“±
Unit 2: Using Objects
πŸ–₯
Unit 3: Boolean Expressions and if Statements
βš™οΈ
Unit 5: Writing Classes
⌚️
Unit 6: Array
πŸ’Ύ
Unit 7: ArrayList
πŸ’»
Unit 8: 2D Array
πŸ–²
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741
Β© 2021 Fiveable, Inc.