Java Basics - Control Flow and Conditionals 1

IF Statement 

Using Boolean Varibles in If statements.

The below if statement will execute only when true. ie if(1){}

boolean isCold = true;

if(isCold)
{
//execute code
}
Variable scope is the block of code where a variable can be used and referred to.
____________________

Else Statement

boolean isCold = true;

if(isCold)
{
//execute code
}
else
{
//execute code
}
_________________________

Else if  Statement

An else if comes after an if statement

boolean example1 = ?;
boolean example 2 = ?;

if(example1) {
//execute code
}
else if(example2){
//execute code
}
else
{
//execute code
}

_______________________________

Boolean expressions

This is where we compare numbers or variables to true or false

boolean b1 = 3<5; //this will evaluate to true.
boolean b2 = 5<3; //this will evaluate to false.

Side Notes:
 ">" and "< " always come before "=" in expressions. For example  x>=10 or x<=7.
The "==" is an check for equality sign. while "=" is an assignment expression
The "!=" is a check for not equality.



Comments

Popular posts from this blog

Java Basics - Variable and Data Types Part 1

Java Basics - Variable and Data Types Part 2

OOP - Objects and Classes