conditions in java. first…boolean operators a boolean data type is always true or false. boolean...

11
Conditions in Java

Upload: colleen-richardson

Post on 04-Jan-2016

223 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: 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

Conditions in Java

Page 2: 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

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”?

Page 3: 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

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?

Page 4: 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

The “if” statement

Your program is going along, and you need to make a decision, a fork in the road…

?

Page 5: 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

The if statement

In Java the if statement works like this:

program…

if( some condition ){

come in here!

}

program

Page 6: 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

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!

Page 7: 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

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”); }

Page 8: 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

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)

Page 9: 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

Symbols

AND &&

OR ||

Not !

Page 10: 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

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)

Page 11: 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

Now Try A3!