13
Decisions with if statements

Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Embed Size (px)

Citation preview

Page 1: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Decisions with if statements

Page 2: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Simple If Statements

if (age < 18)

g.drawString(“You may not vote.”, 50, 50);

Evaluating a single expression

Page 3: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Simple If Statements

if (age > 18)

g.drawString(“You may vote.”, 50, 50);

Evaluating a single expression

Page 4: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Simple If Statements

if (age >= 18)

g.drawString(“You may vote.”, 50, 50);

Evaluating a single expression

Page 5: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Simple If Statements

if (age != 16)

g.drawString(“You are not sixteen years old.”, 50, 50);

Evaluating a single expression

Page 6: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Simple If Statements

if (age == 16)

g.drawString(“You are sixteen years old.”, 50, 50);

Evaluating a single expression

Page 7: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Logical Opereators

> Greater than

< Less than

== Equal to

!= Not equal to

<= Less than or equal to

>= Greater than or equal to

Page 8: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Equal Signs

= This is used to assign values to variables.

int sliderValue = 0;

== This is used to compare values.

if (age == 16)

Page 9: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Multiple Comparisons

if (age >= 21 && age < 65)

g.drawString(“You may be an airline pilot.”, 50, 50);

Evaluating multiple expressions

Page 10: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Multiple Comparisons

if (age < 21 || age >= 65)

g.drawString(“You may not be an airline pilot.”, 50, 50);

Evaluating multiple expressions

Page 11: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

If-Else Statements

if (age < 18)

g.drawString(“You may not vote.”, 50, 50);

else

g.drawString(“You may vote.”, 50, 50);

Using Else with If statements

Page 12: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

If-Else Statements

if (age < 21 || age >= 65)

g.drawString(“You may not be an airline pilot.”, 50, 50);

else

g.drawString(“You may be an airline pilot.”, 50, 50);

Using Else with If statements

Page 13: Decisions with if statements. Simple If Statements if (age < 18) g.drawString(You may not vote., 50, 50); Evaluating a single expression

Grouping Statements

if (age < 18) {g.drawString(“You may not vote.”, 50, 50);

g.drawString(“You may not serve in the military.”, 50, 50);

g.drawString(“You may not sign business contracts.”, 50, 50);

} else {g.drawString(“You may vote.”, 50, 50);

g.drawString(“You may serve in the military.”, 50, 50);

g.drawString(“You may sign business contracts.”, 50, 50);

}

Grouping statements with braces