conditions in java. first…boolean operators a boolean data type is always true or false. boolean...
TRANSCRIPT
Conditions in Java
First…Boolean Operators
• A boolean data type is always true or false.
• Boolean operators always return true or false
• For example:
(x >y) asks “Is x bigger then y”?
Operators in Java
x > y Is x greater then y?
x < y Is x less then y?
x>=y Is x greater then or equal to y?
x<=y Is x less then or equal to y?
x==y Is x equal to y?
The “if” statement
Your program is going along, and you need to make a decision, a fork in the road…
?
The if statement
In Java the if statement works like this:
program…
if( some condition ){
come in here!
}
program
For example
if (grade < 50){
c.print(“You fail”);
}
//the program will only go inside the if block
//if the grade is less then 50
Note: Don’t put semicolons on if statements!
If else
• Sometimes you want to specify another path if the condition is not met.
• For this you would use else.
if(grade<50){c.print(“You fail”);
}else{
c.print(“you pass”); }
Conjunctives
• Two conditions can be linked together using conjunctives:
AND OR
Ex if(4<x AND x<10)
Sometimes you want the opposite of a condition,
Ex: if ( x is not equal to 4)
Symbols
AND &&
OR ||
Not !
For example
Let A and B be two conditions.
A and B (A && B)
A or B (A || B)
Not A (!A)
Not (A and B) !(A &&B)
Now Try A3!