21x - Learn Java Boolean Data Type - Example1

preview_player
Показать описание

In this exercise, we use the boolean data type to store values that are used to control program flow and execution.
Рекомендации по теме
Комментарии
Автор

Exercise done with an If-else statement

boolean bool = false;

if (bool) {
for (int i = 0; i <= 3; i++) {
System.out.println("The Value of the variabel is true");
}
} else {
for (int i = 0; i <= 3; i++) {
System.out.println("The Value of the variabel is false");
}
}

MrMpeder
Автор

1:35 I don't see why we need curly braces (Code Block) .
After the if statement that's a for loop which has only 1 print statement.
This is a new use case of if and for loop inside that if statement, to me it seems logical that you dont need curly braces because they are all connected and have 1 line of executed code if.

Edit:
Ok it seems like curly brackets are recommended no matter what, so that its more readable and if you go back to add stuff you don't need to also add the brackets too cause they are already there.

Either it;s not neccessary or I need to review the code block videos.
Edit: They didn't have a similar example to clear this up. Even thou I agree about having good practice they don't seem necessary in this case.

joeroganpodfantasy
Автор

I did it the other way around. I quess it doesn't matter?
public static void main(String []args){

boolean bool = (15>6);
int i;

for(i=1; i<=4; i++){

if(bool){
System.out.println("The value of the variable is " + bool);

if(!bool)
System.out.println("The value of the variable is " + bool);}

}



}
}

decall