Chapter 4 Flashcards

1
Q

what is the user validation while loop?

A

while(!scan.hasNext(datatype){
System.out.println(Not valid);
scan.next();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a loop in Java?

A

A repetition statement that allows executing a block of code multiple times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What controls a loop?

A

A boolean condition that determines if the loop should continue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the three types of loops in Java?

A

✅ while loop
✅ do-while loop
✅ for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the syntax of a while loop?

A

```java
while (condition) {
// loop body
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When does a while loop execute?

A

As long as the condition remains true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if the condition never becomes false?

A

The loop runs forever (infinite loop). Leading to a runtime error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an infinite loop?

A

A loop that never stops running because its condition never becomes false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example of an infinite loop?

A

```java
while (true) {
System.out.println(“This never stops!”);
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can infinite loops be avoided?

A

Ensure the loop condition eventually becomes false inside the loop body.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is special about the do-while loop?

A

It executes at least once, even if the condition is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the syntax of a do-while loop?

A

```java
do {
// loop body
} while (condition);
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax of a for loop?

A

```java
for (initialization; condition; update) {
// loop body
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is a for loop different from a while loop?

A

A for loop is more compact because initialization, condition, and update are in one line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are two ways to control loops?

A

✅ Sentinel Values – Special values to stop the loop.
✅ User Confirmation – Asking the user if they want to continue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Example of using a sentinel value?

A

```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.

17
Q

What is a nested loop?

A

A loop inside another loop.

18
Q

What does the break statement do?

A

Exits the loop immediately.

19
Q

What does the continue statement do?

A

Skips the rest of the current loop iteration and moves to the next iteration.

20
Q

What is variable scope?

A

The part of the program where a variable exists and is accessible.

21
Q

Example of scope issue?

A

```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.

22
Q

When to use a For Loop?

A

When the number of iterations is known beforehand.

23
Q

When to use a While Loop?

A

When the number of iterations is unknown beforehand.

24
Q

When to use a Do-While Loop?

A

When the loop must run at least once.

25
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)
26
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.
27
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
28
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.
29
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.