05 Flow of Control - mazawi/Teaching-Java GitHub Wiki

5.1 Flow of Control types

  1. Sequential
  2. Method (Function) calls
  3. Selection
  4. Looping

5.2 Equality and Comparison Operations

Relationa Operators Meaning
== is equal to
!= is not equal to
< is less than
<= is less than or equal to
> is greater than
>= is greater than or equal to

5.3 Logical Operators

Logical Operator Type (number of operands) Meaning
! Unary NOT
&& Binary AND
|| Binary OR

5.4 Truth Table

a b !a a && b a || b
true true false true true
true false false false true
false true true false true
false false true false false

5.5 if Statement

  • Used when the program should perform an operation for one set of data, but do nothing for all other data.
  • if is used to check a specific condition whether it is True or False
  • it allow the programme to execute the following code if the code is true and ignore it if the condition is false
  • used when the execution of some code depends on some conditions

image

Example: check if a number is positive

public class Main 
{
  public static void main(String[] args) 
  {
    int x = 5;
    if (x >  0)
    {
       System.out.println("Positive");
    }
  }
}

Open the example here

5.6 _if / _else Statements

image

5.7 if / else if / else Statements

image

public class Main 
{
  public static void main(String[] args) 
  {
    int x = 5;
    if (x >  0)
    {
       System.out.println("Positive");
    }
    else if (x < 0)
    {
       System.out.println("Negative");
    }
    else
    {
       System.out.println("Zero");
    }       
  }
}

Example

//write a programme that prints the value of the numbers (1, 2, 3) in words

public class Main
{
	public static void main(String[] args) 
	{
	    int x = 4;
	    if (x ==1)
	    {
	        System.out.println("ONE");
	    }
	    else if (x == 2)
	    {
	        System.out.println("Two");
	    }
	    else if (x==3)
	    {
	        System.out.println("Three");
	    }
	    else
	    {
	        System.out.println("The number is out of range");
	    }
		
	}
}

5.8 Switch - Case structure

  • The "switch-case" statement in Java is used for decision-making and control flow.
  • It's designed to select one of several code blocks to be executed based on the value of an expression.
  • The expression inside the switch statement is usually an integral data type (e.g., int or char) or an enumerated type.
  • The value of the expression is compared to various "case" values within the switch block.
  • When a match is found between the expression's value and a "case" value, the corresponding block of code is executed.
  • You can also include a "default" block to specify code that will run if no "case" matches the expression.
  • Once a "case" is matched and executed, the program will continue to execute subsequent code unless a "break" statement is used to exit the switch block.

image

Example 1 Access the example here

package CH_05_Flow_Control;

public class Switch_Ex1 
{
    public static void main(String[] args)
    {
        int day = 30;
        String dayName;

        switch (day) 
        {
        case 1:
        dayName = "Sunday";
        break;
        case 2:
        dayName = "Monday";
        break;
        case 3:
        dayName = "Tuesday";
        break;
        case 4:
        dayName = "Wednesday";
        break;
        case 5:
        dayName = "Thursday";
        break;
        case 6:
        dayName = "Friday";
        break;
        case 7:
        dayName = "Saturday";
        break;
    
        default:
        dayName = "Invalid Entry !!!";
        }

        System.out.println("Today is " + dayName);

    }
}
⚠️ **GitHub.com Fallback** ⚠️