Iteration
When writing statements that might repeatedly appear in code, you can do 2 things.
You can copy and paste the statement multiple times, and spend hours writing code.
A more efficient way would be to use a conditional statement that is referred to as a loop.
Note: On the AP Exam you will be expected to know what while and for loops are.
while
LoopThis loop cycles through again and again, while the condition is considered true.
It’s similar to the if statement.
Ex: Say that you have a paintbrush that is soaked in paint, and hopefully you don’t put away the paintbrush until it is clean. It’s entirely based on the cleanliness of the paintbrush. You will only put the brush away if it’s true that it is clean.
If the condition is false then the loop will continue to run, until the condition returns true.
However, if the condition is true then it will execute the statement, and it will exit the loop, and if there is another condition present then that will start running instead.
However, sometimes loops aren’t well written, because sometimes there might not be any sort of statements that go through the loop to help change the value of the expression to be true.
This would be considered an infinite loop, which, as its name suggests, means that the loop goes on forever.
It has the similar feeling of TikTok freezing on you when you're on your daily Tiktok spiral.
So an infinite loop isn’t desirable in all situations.
Note: On the AP Exam, you need to be able to trace code in multiple-choice questions and detect infinite loops. In the free-response questions, you need to write code that is free of infinite loops.
All loops are dependent on the conditions that are found within them.
If there are multiple conditions found in the loop when can use boolean operators to simplify it. (View Chapter 5 for a review of boolean operators)
Let’s look at our paintbrush example again.
Ex: The paintbrush needs to be cleaned and dried for it to be put away, and if it isn’t then don’t put the paintbrush away.
In this case, you would end up using the && (and) boolean operator, because while the paintbrush is both wet and dirty, then only will the paintbrushes be put away.
So if the paintbrush is dirty and wet then don’t put away the paintbrush, otherwise, put away the paintbrush.
for
StatementThis is just another type of loop and is just as effective as a while loop. The only difference is that one of the loops may make the code more simplified than the other.
Since this loop needs more components, it can help to avoid an infinite loop in some situations.
Here are some steps for how a for loop executes:
The initializer will start first and it will start the loop off.
The conditions will be checked to see if they are true or false.
The statements will execute.
The incrementer will increment the number, and in turn will change it.
The process above starting from step 2 will continue to repeat until the value turns to false.
Once the value turns false the loop ends.
Ex:
for(int num = 1; num <= 22; num++)
System.out.println(num);
//Explanation: The loop above will show every number and the output on the screen on separate lines.
Ex:
for( int num = 1; num <= 3; num++)
System.out.println(num);
System.out.println(“Done”);
//Explanation: The example above shows that num starts at the value of 1, and as long as the number is less than or equal to 3 the for loop will continue to execute. When the for loop executes it prints the value of num. Each time you go through the loop, the value of num increases by 1. Once the value of num hits 4, it will exit the for loop, and print done instead.
Note: On the AP Exam some of the multiple choice questions will show you multiple loop structures and formats, and ask you to pick which ones run the most efficiently, and which one produces the right output. The free-response questions will ask you to write a loop.
Note: Make sure to use your best judgment when writing your code for loops, because one loop may run more efficiently than others, and run on less code. Which means less work for us :)
Differences between the 2:
While loops perform the blocked statement, or statements once, and then it will evaluate the condition to determine if the loop should continue executing.
For loops evaluate the condition first, and then it performs the blocked statements if the initial condition is determined to be true.
What to Expect on the AP Exam:
Concepts covered on the AP CSA Exam | Concepts not on the AP CSA Exam | |
---|---|---|
Primitives | int, double, boolean | short,long,byte,char,float |
Increment/Decrement Operators | x++,x-- | ++x,- - x |
Logical Operators | ==,!=,<,>,<=,>=,&&,||,! | &,|,^,<<,>>,>>> |
Conditional Statements | *if/else,****for,***while | *switch,****break,***continue, do/while |
Miscellaneous | ?:(ternary operator)User inputJavaDoc comments |
When writing statements that might repeatedly appear in code, you can do 2 things.
You can copy and paste the statement multiple times, and spend hours writing code.
A more efficient way would be to use a conditional statement that is referred to as a loop.
Note: On the AP Exam you will be expected to know what while and for loops are.
while
LoopThis loop cycles through again and again, while the condition is considered true.
It’s similar to the if statement.
Ex: Say that you have a paintbrush that is soaked in paint, and hopefully you don’t put away the paintbrush until it is clean. It’s entirely based on the cleanliness of the paintbrush. You will only put the brush away if it’s true that it is clean.
If the condition is false then the loop will continue to run, until the condition returns true.
However, if the condition is true then it will execute the statement, and it will exit the loop, and if there is another condition present then that will start running instead.
However, sometimes loops aren’t well written, because sometimes there might not be any sort of statements that go through the loop to help change the value of the expression to be true.
This would be considered an infinite loop, which, as its name suggests, means that the loop goes on forever.
It has the similar feeling of TikTok freezing on you when you're on your daily Tiktok spiral.
So an infinite loop isn’t desirable in all situations.
Note: On the AP Exam, you need to be able to trace code in multiple-choice questions and detect infinite loops. In the free-response questions, you need to write code that is free of infinite loops.
All loops are dependent on the conditions that are found within them.
If there are multiple conditions found in the loop when can use boolean operators to simplify it. (View Chapter 5 for a review of boolean operators)
Let’s look at our paintbrush example again.
Ex: The paintbrush needs to be cleaned and dried for it to be put away, and if it isn’t then don’t put the paintbrush away.
In this case, you would end up using the && (and) boolean operator, because while the paintbrush is both wet and dirty, then only will the paintbrushes be put away.
So if the paintbrush is dirty and wet then don’t put away the paintbrush, otherwise, put away the paintbrush.
for
StatementThis is just another type of loop and is just as effective as a while loop. The only difference is that one of the loops may make the code more simplified than the other.
Since this loop needs more components, it can help to avoid an infinite loop in some situations.
Here are some steps for how a for loop executes:
The initializer will start first and it will start the loop off.
The conditions will be checked to see if they are true or false.
The statements will execute.
The incrementer will increment the number, and in turn will change it.
The process above starting from step 2 will continue to repeat until the value turns to false.
Once the value turns false the loop ends.
Ex:
for(int num = 1; num <= 22; num++)
System.out.println(num);
//Explanation: The loop above will show every number and the output on the screen on separate lines.
Ex:
for( int num = 1; num <= 3; num++)
System.out.println(num);
System.out.println(“Done”);
//Explanation: The example above shows that num starts at the value of 1, and as long as the number is less than or equal to 3 the for loop will continue to execute. When the for loop executes it prints the value of num. Each time you go through the loop, the value of num increases by 1. Once the value of num hits 4, it will exit the for loop, and print done instead.
Note: On the AP Exam some of the multiple choice questions will show you multiple loop structures and formats, and ask you to pick which ones run the most efficiently, and which one produces the right output. The free-response questions will ask you to write a loop.
Note: Make sure to use your best judgment when writing your code for loops, because one loop may run more efficiently than others, and run on less code. Which means less work for us :)
Differences between the 2:
While loops perform the blocked statement, or statements once, and then it will evaluate the condition to determine if the loop should continue executing.
For loops evaluate the condition first, and then it performs the blocked statements if the initial condition is determined to be true.
What to Expect on the AP Exam:
Concepts covered on the AP CSA Exam | Concepts not on the AP CSA Exam | |
---|---|---|
Primitives | int, double, boolean | short,long,byte,char,float |
Increment/Decrement Operators | x++,x-- | ++x,- - x |
Logical Operators | ==,!=,<,>,<=,>=,&&,||,! | &,|,^,<<,>>,>>> |
Conditional Statements | *if/else,****for,***while | *switch,****break,***continue, do/while |
Miscellaneous | ?:(ternary operator)User inputJavaDoc comments |