Title: It might be that we want to execute a particular statement or block of statements only when the condition is false.
1The if-else Statement
- It might be that we want to execute a particular
statement or block of statements only when the
condition is false. - The if-else combination provides a choice between
two options. - The general logic of the if-else is shown in the
Figure 1.
2no
condition is true?
if(condition) //statements //when
condition //is true else
//statements //when condition //is
false next statement
yes
Statement or Block of Statements for true
Statement or Block of Statements for false
Next Statement
Figure 1. One of the two blocks in an if-else
statement is always executed.
3- Often you'd want to execute a statement if a
certain condition is met, and a different
statement if the condition is not met. This is
what else is for. else extends an if statement to
execute a statement in case the expression in the
if statement evaluates to FALSE.
4package ifelsestatement public class Main
public static void main(String args)
throws java.io.IOException char
ch, answer'K' System.out.println("I am
thinking of a letter between A and Z.")
System.out.print("Can you gess it ")
ch(char)System.in.read()
if(chanswer) System.out.println("
RIGHT") else
System.out.println("Sorry, you did not guess
it!")
5run I am thinking of a letter between A and
Z. Can you gess it T Sorry, you did not guess
it! BUILD SUCCESSFUL (total time 10 seconds)
6- It is also possible to nest if-else statements
within ifs, ifs within if-else statements, and
indeed if-else statements within other if-else
statements. - This provides us with plenty of versatility and
considerable room for confusion. - Next an example of an if-else nested within an if
7- if(coffeey)
- if(donutsy)
-
- System.out.println(We have coffee and
donuts.) -
-
- else
- System.out.println(We have coffee, but not
donuts.)
8- An example of an if nested within an if-else
- if(coffeey)
-
- if(donutsy)
- System.out.println(We have coffee and
donuts.) -
- else if(teay)
- System.out.println(We have no coffee, but we
have tea.) -
9- else if, as its name suggests, is a combination
of if and else. Like else, it extends an if
statement to execute a different statement in
case the original if expression evaluates to
FALSE. However, unlike else, it will execute that
alternative expression only if the else if
conditional expression evaluates to TRUE. For
example, the following code would display a is
bigger than b, a equal to b or a is smaller than
b
10package elseifstatement public class Main
public static void main(String args)
int a5, b5 if(agtb)
System.out.println("a is bigger than b.")
else if(ab) System.out.println("a
is equal to b.") else
System.out.println("b is bigger than a.")
run a is equal to b. BUILD SUCCESSFUL (total
time 1 second)
11- There may be several else ifs within the same if
statement. The first else if expression (if any)
that evaluates to TRUE would be executed. - The else if statement is only executed if the
preceding if expression and any preceding else if
expressions evaluated to FALSE, and the current
else if expression evaluated to TRUE.