Review of the if-statements

We learned the 2 basic forms of if-statements:

 (1) The one-way if-statement:          (A.k.a.: "if-statement")

    if ( boolean-expression )
    {
       statement(s); // statement(s) only executed is boolean-expr is true

    }


(2) The two-way if-statement: (A.k.a.: "if-else-statement") if ( boolean-expression ) { statement1(s); // statement1(s) executed is boolean-expr is true } else { statement2(s); // statement2(s) executed is boolean-expr is false }

Making more complex decisions:    Nesting if-staments

We can make more complex decisions by nesting if-statements:

 Example 1:

    if ( boolean-expression )
    {
       Use another (one-way or two-way) if-statement  
       inside the one-way if-statement
    }


Example 2: if ( boolean-expression ) { Use another (one-way or two-way) if-statement inside the then-part of two-way if-statement } else { Use another (one-way or two-way) if-statement inside the else-part of two-way if-statement }

Example:    Nesting if-statements

Problem: write a program to read in the length and width of a rectangle and compute its area

public class Demo1 
{
   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);

      int length = input.nextInt();
      int width  = input.nextInt();
      int area;

      area = length * width;

      System.out.print(area);
   }
} 

DEMO: demo/03-selections/03-nested-if/Demo1.java

Question:    What can go wrong ???

Try enter: length = -4

Example:    Nesting if-statements

7 Improvement 1: check length >= 0

public class Demo2
{
   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);

      int length = input.nextInt();
      int width  = input.nextInt();
      int area;

      if ( length >= 0 )
      {
         area = length * width;
         System.out.print(area);
      }
      else
         System.out.println("Illegal length");
   }
}  

DEMO: demo/03-selections/03-nested-if/Demo2.java

Question:    What can go wrong ???

Try enter: width = -4

Example:    Nesting if-statements

Final program: check length >= 0 and check width >= 0

public class MyProg 
{
   public static void main(String[] args) 
   {
      Scanner input = new Scanner(System.in);

      int length = input.nextInt();
      int width  = input.nextInt();
      int area;

      if ( length >= 0 )
      {
         if ( width >= 0 )
         {
            area = length * width;
         }
         else
            System.out.println("Illegal width");
      }
      else
         System.out.println("Illegal length");
   }
}  

DEMO: demo/03-selections/03-nested-if/Demo3.java

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

The multi-way if-statement is a nested if-statement where the else-statement is another if-statement

 The two-way if-statement:  (we have learned this !)

    if ( condition1 )
    {	 
        statement1(s);
    }	 
    else 	 
        else-statement














 Execution of the above program:
    if condition1 is true: execute  statement1(s)
    otherwise: execute the else-statement

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

The multi-way if-statement is a nested if-statement where the else-statement is another if-statement

 The three-way if-statement:

    if ( condition1 )
    {	 
        statement1(s);
    }	 
    else 	 
        if ( condition2 )
	{   
	    statement2(s);
        }
	else
	    else-statement








 Execution of the above program:
    if condition1 is true: execute  statement1(s)
    if condition2 is true: execute  statement2(s)
    otherwise: execute the else-statement

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

The multi-way if-statement is a nested if-statement where the else-statement is another if-statement

 The four-way if-statement:

    if ( condition1 )
    {	 
        statement1(s);
    }	 
    else 	 
        if ( condition2 )
	{   
	    statement2(s);
        }
	else
	    if ( condition3 )
	    {
	        statement3(s);
	    }
	    else
	        else-statement


 Execution of the above program:
    if condition1 is true: execute  statement1(s)
    if condition2 is true: execute  statement2(s)
    if condition3 is true: execute  statement3(s)
    otherwise: execute the else-statement

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

The else-statement is optional:

 The "no escape" option variant:

    if ( condition1 )
    {	 
        statement1(s);
    }	 
    else 	 
        if ( condition2 )
	{   
	    statement2(s);
        }
	else
	    if ( condition3 )
	    {
	        statement3(s);
	    }
	    
 In that case: if none of the conditions are met        
               none of the statements is executed...

 Execution of the above program:
    if condition1 is true: execute  statement1(s)
    if condition2 is true: execute  statement2(s)
    if condition3 is true: execute  statement3(s)
   

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

Summary:

 Multi-way if-statement is a nested if-statement in this form:

    if ( condition1 )
    {	 
       statement1(s);
    }	 
    else 	 
       if ( condition2 )
       {	 
  	   statement2(s);
       }	 
       else 
  	  if ( condition3 )
  	  {
  	      statement3(s);
  	  } 
         [else                    // Optional
          {
              statement4(s);
          }]

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

The multi-way if-statement is usually written in this form:

 Multi-way if-statement is a nested if-statement in this form:

    if ( condition1 )
    {	 
       statement1(s);
    }	 
    else if ( condition2 )
    {	 
       statement2(s);
    }	 
    else if ( condition3 )
    {
       statement3(s);
    }
   [else                    // Optional
    {
        statement4(s);
    }]

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

The multi-way if-statement will execute the statement(s) associated with the first condition that is true:

 Multi-way if-statement is a nested if-statement in this form:

    if ( condition1 )
    {	 
       statement1(s);
    }	 
    else if ( condition2 )  // <---- first condition that is true
    {	 
       statement2(s);       // <--- Program executes these statements
    }	 
    else if ( condition3 )
    {
       statement3(s);
    }
   [else                    // Optional
    {
        statement4(s);
    }]

Multiway if-statement:    If-statement that selects one option out of multiple (more than 2) options

If none of the conditions are true, the else statements are executed if present

 Multi-way if-statement is a nested if-statement in this form:

    if ( condition1 )
    {	 
       statement1(s);
    }	 
    else if ( condition2 ) 
    {	 
       statement2(s);      
    }	 
    else if ( condition3 )
    {
       statement3(s);
    }
    else                // If present
    {
       statement4(s);   // Executed if ALL condition above are FALSE
    }

Example:    Prime out prime or not prime

Write a program that prints out whether a number x (1 ≤ x ≤ 5) is prime or not prime

   public static void main(String[] args) 
   {
      int x = 3;   // Try changing x

      if ( x == 1 )
      {
         System.out.println("Not prime");
      }
      else if ( x == 2 )
      {
         System.out.println("Prime");
      }
      else if ( x == 3 )
      {
         System.out.println("Prime");
      }
      else if ( x == 4 )
      {
         System.out.println("Not prime");
      }
      else if ( x == 5 )
      {
         System.out.println("Prime");
      }
   } 

DEMO: 03-selections/03-nested-if/IsPrime1.java          Single-step in BlueJ !

Example:    use else in multi-way if-statement

Write a program that prints out whether a number x (1 ≤ x ≤ 10) is prime or not prime

   public static void main(String[] args) 
   {
      int x = 9;   // Try changing x

      if ( x == 2 )
      {
         System.out.println("Prime");
      }
      else if ( x == 3 )
      {
         System.out.println("Prime");
      }
      else if ( x == 5 )
      {
         System.out.println("Prime");
      }
      else if ( x == 7 )
      {
         System.out.println("Prime");
      }
      else
      {
         System.out.println("Not prime OR number out of range [1-10]");
      }
   } 

DEMO: 03-selections/03-nested-if/IsPrime2.java          Single-step in BlueJ !