Chapter 4 Flashcards
what is the user validation while loop?
while(!scan.hasNext(datatype){
System.out.println(Not valid);
scan.next();
}
What is a loop in Java?
A repetition statement that allows executing a block of code multiple times.
What controls a loop?
A boolean condition that determines if the loop should continue.
What are the three types of loops in Java?
✅ while loop
✅ do-while loop
✅ for loop
What is the syntax of a while loop?
```java
while (condition) {
// loop body
}
~~~
When does a while loop execute?
As long as the condition remains true.
What happens if the condition never becomes false?
The loop runs forever (infinite loop). Leading to a runtime error
What is an infinite loop?
A loop that never stops running because its condition never becomes false.
Example of an infinite loop?
```java
while (true) {
System.out.println(“This never stops!”);
}
~~~
How can infinite loops be avoided?
Ensure the loop condition eventually becomes false inside the loop body.
What is special about the do-while loop?
It executes at least once, even if the condition is false.
What is the syntax of a do-while loop?
```java
do {
// loop body
} while (condition);
~~~
What is the syntax of a for loop?
```java
for (initialization; condition; update) {
// loop body
}
~~~
How is a for loop different from a while loop?
A for loop is more compact because initialization, condition, and update are in one line.
What are two ways to control loops?
✅ Sentinel Values – Special values to stop the loop.
✅ User Confirmation – Asking the user if they want to continue.
Example of using a sentinel value?
```java
Scanner input = new Scanner(System.in);
int num;
do {
System.out.print(“Enter a number (0 to stop): “);
num = input.nextInt();
} while (num != 0);
~~~
Loop stops when 0 is entered.
What is a nested loop?
A loop inside another loop.
What does the break
statement do?
Exits the loop immediately.
What does the continue
statement do?
Skips the rest of the current loop iteration and moves to the next iteration.
What is variable scope?
The part of the program where a variable exists and is accessible.
Example of scope issue?
```java
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
System.out.println(i); // ERROR: i is out of scope!
~~~
using a variable that was declared inside of a different block
The variable i
only exists inside the for loop.
When to use a For Loop?
When the number of iterations is known beforehand.
When to use a While Loop?
When the number of iterations is unknown beforehand.
When to use a Do-While Loop?
When the loop must run at least once.
What are some real-world uses of loops?
✅ Reading user input until valid data is entered
✅ Repeating a process (e.g., guessing a number game)
✅ Processing files line by line
✅ Creating patterns and tables
✅ Simulating events (e.g., dice rolls, random number generation)
What is optional in the header of a for
loop in Java?
Each expression in the header of a for
loop is optional. This includes initialization, condition, and the action after each iteration.
What happens if the initialization part of a for
loop is left out?
If the initialization is left out, no initialization is performed, and the loop will begin without any predefined variable values.
It just won’t work tbh
What happens if the condition in a for
loop is left out?
If the condition is left out, it is always considered to be true, which results in an infinite loop.
What happens if the action after each iteration in a for
loop is left out?
If the action after each iteration is left out, no increment operation is performed, meaning the loop may run indefinitely unless other conditions are met.