Recitation 4: Java, if, else, if else

Java if statement

Let’s first look at the anatomy of the humble if statement:


if(<boolean expression>){
	//if boolean expression evaluates to true then execute code here
}
//if boolean expression evaluates to false OR code inside the if statement finishes then execute code here 

Note that there are other ways of writing it.


if(<boolean expression>)
	//if boolean expression evaluates to true then execute code here

//if boolean expression evaluates to false OR code inside the if statement finishes then execute code here 

Java if else statement


if(<boolean expression>){
	//if boolean expression evaluates to true then execute code here
} else {
	//if boolean expression evaluates to false then execute code here	
}
//then execute code here 

if(<boolean expression>)
	//if boolean expression evaluates to true then execute code here
else
	//if boolean expression evaluates to false then execute code here	

//then execute code here 

Java if elseif


if(<boolean expression 1>){
	//if boolean expression 1 evaluates to true then execute code here
} else if(<boolean expression 2>){
	//if boolean expression 2 evaluates to true then execute code here	
} else {
	//any other case the code here executes
}
//then execute code here 

Some examples are here.

[top]

Post originally posted on 2018-02-11 01:00:00 +0000

Post last modified on

comments powered byDisqus