+ All Categories
Home > Documents > The if - else Statements Decision Making in Java.

The if - else Statements Decision Making in Java.

Date post: 01-Apr-2015
Category:
Upload: isaias-linam
View: 246 times
Download: 1 times
Share this document with a friend
Popular Tags:
39
The if - else Statements Decision Making in Java
Transcript
Page 1: The if - else Statements Decision Making in Java.

The if-else Statements

Decision Making in Java

Page 2: The if - else Statements Decision Making in Java.

The if-else Statements

if (expression){

statement1;

}

Page 3: The if - else Statements Decision Making in Java.

IF true

• If the expression evaluates to true, statement1 is executed.

if (expression){

statement1;

}

Page 4: The if - else Statements Decision Making in Java.

If not TRUE

if (expression){statement1;

}

•. If expression is false then nothing is executed and the program execution picks up after the ending curly brace (}).

Page 5: The if - else Statements Decision Making in Java.

for two-way selection

• To provide for two-way selection an if statement may add an else option.

Page 6: The if - else Statements Decision Making in Java.

for two-way selection

• If the expression evaluates to true, the statement is executed.

• In an if-else statement, if the expression is false then statement2 would be executed

Page 7: The if - else Statements Decision Making in Java.

IF, ELSE

• if the expression is false then statement2 would be executed

Page 8: The if - else Statements Decision Making in Java.

IF, ELSE• The expression being tested must

always be placed in parentheses.

( )• This is a common source of syntax errors.

if (expression){statement1;}else{

statement2;}

Page 9: The if - else Statements Decision Making in Java.

Compound Statements

if (expression){

statement1;

statement2;

statement3;

}

else{

statement4;

Page 10: The if - else Statements Decision Making in Java.

Compound Statements

• The statement executed can be a block of statements, grouped together into a single compound statement.{ }

Page 11: The if - else Statements Decision Making in Java.

compound statement• A compound

statement is created by enclosing any number of single statements

by braces { }

if (expression){

statement1;

statement2;

statement3;

}else{

statement4;

statement5;

statement6;

}

Page 12: The if - else Statements Decision Making in Java.

Nested if-else Statements

• The statement inside of an if or else option can be another if-else statement.

} if (expression1){

if (expression2){

statement1;

}else{

statement2;

}

} else{

statement3;

} }

Page 13: The if - else Statements Decision Making in Java.

Braces need Partners

• braces will need to be correct to ensure that

the ifs and elses get paired with their partners.

{ }

Page 14: The if - else Statements Decision Making in Java.

Who needs braces

• Technically, braces are not needed for if and if-else structures

• Indentation is ignored by the compiler

• However, if you always use braces when writing if and if-else statements, you will never have this problem.

Page 15: The if - else Statements Decision Making in Java.

Another alternative

• Another alternative is to use of the && or

the or | | operator.

• A pair of nested if statements can be coded as a single compound && statement.

Page 16: The if - else Statements Decision Making in Java.

What type of triangle?

• determining the type of triangle given the three sides A, B, and C.

• If an equilateral triangle is encountered, the rest of the code is ignored. This can help to reduce the execution time of a program.

Page 17: The if - else Statements Decision Making in Java.

if ( (A == B) && (B == C) )

System.out.println("Equilateral triangle");

else if ( (A == B) || (B == C) || (A == C) )

System.out.println("Isosceles triangle");

else

System.out.println("Scalene triangle");

Page 18: The if - else Statements Decision Making in Java.

Conditional Operator

• Java provides an alternate method of coding an if-else statement using the conditional operator. This operator is the only ternary operator in Java, as it requires three operands. The general syntax is:

•  • (condition) ? statement1 : statement2;

Page 19: The if - else Statements Decision Making in Java.

Conditional short way

• If the condition is true, statement1 is executed. If the condition is false, statement2 is executed.

Page 20: The if - else Statements Decision Making in Java.

int max(int a, int b)

{ // returns the larger of two integers

(a > b) ? return a : return b;

}

Page 21: The if - else Statements Decision Making in Java.

Boolean Identifiers.. . .code that is easier to read

•  

The execution of if-else statements depends on the value of the Boolean expression.

We can use boolean variables to write code that is easier to read.

Page 22: The if - else Statements Decision Making in Java.

boolean variables to write code that is

easier to read.

• code that reads more like English.

if(done == true){

System.out.println("We are done!");

}

we can write

if(done){

System.out.println("We are done!");

}

 

Page 23: The if - else Statements Decision Making in Java.

Switch Statements switch (expression){ case value1:

statement1;statement2;...break;

case value2:statement3;statement4;...break;

case valuen: //any number of case statement may be usedstatement;statement;break;

default:statement;statement;break;

} /* end of switch statement */

Page 24: The if - else Statements Decision Making in Java.
Page 25: The if - else Statements Decision Making in Java.

switch statement

• The switch statement attempts to match the integer value of the expression with one of the case values.

case value2:statement3;statement4;

...break;

Page 26: The if - else Statements Decision Making in Java.

• If a match occurs, then all statements past the case value are executed until a break statement is encountered.

• The effect of the break statement causes program control to jump to the end of the switch statement. No other cases are executed.

Page 27: The if - else Statements Decision Making in Java.

End with a default

default:

statement;

statement;

break;

Page 28: The if - else Statements Decision Making in Java.

A very common error

• when coding a switch do NOT forget to include the break statements to terminate each case value.

• If the break statement is omitted, all the statements following the matching case value are executed.

Page 29: The if - else Statements Decision Making in Java.

Default needs a break

int i = 4;

switch (i) {

case 1: System.out.println(“Apple”); break;

default: System.out.println(“Orange”);

case 2: System.out.println(“Banana”); break;

}

 

Orange

Banana

Page 30: The if - else Statements Decision Making in Java.

Default can be anywhere

• Note that the default statement can actually be placed anywhere.

Page 31: The if - else Statements Decision Making in Java.

printing the work day of the week

switch (day){ case 1: System.out.println ("Monday"); break; case 2: System.out.println ("Tuesday");

break; case 3: System.out.println ("Wednesday");

break; case 4: System.out.println ("Thursday");

break; case 5: System.out.println ("Friday"); break; default: System.out.println ("not a valid

day"); break;}

Page 32: The if - else Statements Decision Making in Java.

count the occurrences of vowels and

consonants if (('a' <= letter) && (letter <= 'z')){ switch (letter){ case 'a' : case 'e' : case 'i' : case 'o' : case 'u' :

vowel++; break; default : consonant++; break; }

}

Page 33: The if - else Statements Decision Making in Java.

• It is good programming practice to include a break statement at the end of the switch structure.

Page 34: The if - else Statements Decision Making in Java.

Switch of If,Else ????

•There are situations where the switch statement should not replace an if-else

chain.

•If the value being compared must fit in a range of values, the if-else statement

should be used.

Page 35: The if - else Statements Decision Making in Java.

Use If, Else

• If the value being compared must fit in a range of values, the if-else statement should be used.

Page 36: The if - else Statements Decision Making in Java.

You got an 88

if(score >= 90 && score <= 100){ grade = 'A';}else if{(score >= 80 && score < 90)

grade = 'B';}else if{(score >= 70 && score < 80)

grade = 'C';}

Page 37: The if - else Statements Decision Making in Java.

Summary

• Control structures are a fundamental part of Java.

Page 38: The if - else Statements Decision Making in Java.

summary

• You will need to practice control structures in Java to become familiar with what types of situations they are useful in.

Page 39: The if - else Statements Decision Making in Java.

summary

• Also, Boolean expressions are very useful and should be used whenever appropriate to make coding easier.


Recommended