|
if ( CONDITION )
ONE-statement
|
if ( CONDITION )
ONE-statement
else
ONE-statement
|
|
Therefore:
|
|
|
|
Write a program that:
|
|
public class NestedIf01
{
public static void main(String[] args)
{
char sex;
int age, price = 0;
Scanner in = new Scanner(System.in); // Construct Scanner object
sex = in.next().charAt(0); // Read in next char into sex
age = in.nextInt(); // Read in next integer into age
if ( sex == 'M' )
{ // Then-part of outer if-else statement
if ( age <= 13 )
{
price = 10; // Case: boy <-- then-part of inner if-else
}
else
{
price = 15; // Case: man <-- else-part of inner if-else
}
}
else
{ // Else-part of outer if-else statement
if ( age <= 13 )
{
price = 12; // Case: girl <-- then-part of inner if-else
}
else
{
price = 25; // Case: woman <-- else-part of inner if-else
}
}
System.out.println("Price = " + price);
}
}
|
Explanation:
|
Note:
|
How to run the program:
|
|
Write a program that:
|
|
import java.util.Scanner;
public class Grade01
{
public static void main(String[] args)
{
double ng;
String lg = "";
Scanner in = new Scanner(System.in); // Construct Scanner object
System.out.print("Enter number grade: ");
ng = in.nextDouble(); // Read in next number into ng
if ( ng >= 90 )
lg = "A";
if ( 80 <= ng && ng < 90 )
lg = "B";
if ( 70 <= ng && ng < 80 )
lg = "C";
if ( 60 <= ng && ng < 70 )
lg = "D";
if ( ng < 60 )
lg = "F";
System.out.println("Letter grade = " + lg);
}
}
|
How to run the program:
|
|
|
Explanation:
|
public class Grade02
{
public static void main(String[] args)
{
double ng;
String lg = "";
Scanner in = new Scanner(System.in); // Construct Scanner object
ng = in.nextDouble(); // Read in next number into ng
if ( ng >= 90 )
lg = "A";
else
{
if ( ng >= 80 )
lg = "B";
else
{
if ( ng >= 70 )
lg = "C";
else
{
if ( ng >= 60 )
lg = "D";
else
lg = "F";
}
}
}
System.out.println("Letter grade = " + lg);
}
}
|
|
|
|
The choices of A, B and C are mutually exclusive.
|
|
Notes:
|
Each of the conditions are mutually exclusive
if ( conditionX )
{
statements executed only when:
conditionX = true
}
else
{
if ( conditionY ) --+
{ |
statements executed only when: |
conditionX = false |
and conditionY = true |
} | One statement!!
else |
{ |
statements executed only when: |
conditionX = false |
and conditionY = false |
} --+
}
|
Since there is one statement in the else-part, we do not need to use a block
if ( conditionX )
{
statements executed only when:
conditionX = true
}
else if ( conditionY ) --+
{ |
statements executed only when: |
conditionX = false |
and conditionY = true |
} | One statement!!
else |
{ |
statements executed only when: |
conditionX = false |
and conditionY = false |
} --+
|
A N-way selection construct looks like the following:
if ( condition1 )
{
S1; (one or more statements)
}
else if ( condition2 )
{
S2; (one or more statements)
}
else if ( condition3 )
{
S3; (one or more statements)
}
...
else if ( conditionN-1 )
{
SN-1; (one or more statements)
}
else
{
SN; (one or more statements)
}
|
Notes:
|
public class Grade03
{
public static void main(String[] args)
{
double ng;
String lg = "";
Scanner in = new Scanner(System.in); // Construct Scanner object
ng = in.nextDouble(); // Read in next number into ng
if ( ng >= 90 )
{
lg = "A";
}
else if ( ng >= 80 )
{
lg = "B";
}
else if ( ng >= 70 )
{
lg = "C";
}
else if ( ng >= 60 )
{
lg = "D";
}
else
{
lg = "F";
}
System.out.println("Letter grade = " + lg);
}
}
|
How to run the program:
|