Hello guys, sorry to disturb you, I just have a quick question concerning boolean conditions.
Consider these lines of codes:
public class BooleanConditionForLoop {
public static void main(String[] args) {
int counter = 0;
// Initialize a boolean variable in the for loop
for (boolean keepLooping = true; keepLooping; counter++) {
System.out.println("Counter is: " + counter);
// Condition to stop the loop when counter reaches 5
if (counter >= 5) {
keepLooping = false;
}
}
System.out.println("Loop has ended.");
}
}
- Whenever we initialize a boolean variable, we should always initialize it to true?
2.If ever we write this:
boolean checking = false;
if (checking) {
. . .
} // This if statement how do we read it, do we read it as follows: If checking is true then do.... or if checking if false then do .... , like does the assignment checking = false has an impact on the condition please
- Also notice that in the for loop, we write, we initialize a boolean variable in the parentheses for (boolean...) didn't know that it was possible... I always used int values.... we can actually initialize any value we want ?