Post

Java 4 - Control Flows & Functions

Control Flow

  • Control flow statements are used to control the flow of execution in a program.
  • Java has control flow statements like if, else, for, while, and switch.:
    • Conditional statements - used to perform different actions based on different conditions(if, if...else, if...else if...else, switch)
    • Looping statements - used to execute a block of code repeatedly(while, do...while, for, for-each)
  • Because these statements and topics are very similar to those in other programming languages, I will not go into detail about them.

Conditional Statements

  • Conditional statements are used to perform different actions based on different conditions.
  • Java has the following conditional statements:
    • if statement - executes a block of code if a specified condition is true
    • if...else statement - executes a block of code if a specified condition is true and another block of code if that condition is false
    • if...else if...else statement - executes different codes for more than two conditions
    • switch statement - selects one of many code blocks to be executed

The if Statement

  • The if statement is used to execute a block of code if a specified condition is true.
  • The syntax is as follows:
1
2
3
if (condition) {
    // block of code to be executed if the condition is true
}
  • The if statement can be used without the curly braces if there is only one statement to be executed.
1
2
if (condition)
    // block of code to be executed if the condition is true

The if...else Statement

  • The if...else statement is used to execute a block of code if a specified condition is true and another block of code if that condition is false.
  • The syntax is as follows:
1
2
3
4
5
if (condition) {
    // block of code to be executed if the condition is true
} else {
    // block of code to be executed if the condition is false
}

The if...else if...else Statement

  • The if...else if...else statement is used to execute different codes for more than two conditions.
  • The syntax is as follows:
1
2
3
4
5
6
7
if (condition1) {
    // block of code to be executed if condition1 is true
} else if (condition2) {
    // block of code to be executed if the condition1 is false and condition2 is true
} else {
    // block of code to be executed if the condition1 is false and condition2 is false
}

The switch Statement

  • The switch statement is used to select one of many code blocks to be executed.
  • The syntax is as follows:
1
2
3
4
5
6
7
8
9
10
11
switch (expression) {
    case value1:
        // block of code to be executed if expression is equal to value1
        break;
    case value2:
        // block of code to be executed if expression is equal to value2
        break;
    ...
    default:
        // block of code to be executed if expression is different from all values
}

Looping Statements

  • Looping statements are used to execute a block of code repeatedly.
  • Java has the following looping statements:
    • while loop - loops through a block of code as long as a specified condition is true
    • do...while loop - loops through a block of code once, and then repeats the loop as long as a specified condition is true
    • for loop - loops through a block of code a specified number of times
    • for-each loop - loops through the elements of an array or collection

Functions

  • Functions in Java help us to group code and make them reusable
  • To create a function, we use the:
    • public - keyword which means that the method can be accessed from anywhere.
    • static - keyword which means that the method belongs to the class and not an object of the class.
    • void - keyword which means that the method does not have a return value.
    • the function name
1
2
3
4
5
6
7
8
 public static void singChorus(){
        // Chorus needs to be sung 5 times
        System.out.println("Don't blame it on the sunshine");
        System.out.println("Don't blame it on the moonlight");
        System.out.println("Don't blame it on good times");
        System.out.println("Don't blame it on the boogie\n");

  }
  • When defining a function that returns a value, we must declare the return type
1
2
3
4
5
6
public static double areaCalculator(double length, double width){
  double area = length * width;
  return area;
}

This post is licensed under CC BY 4.0 by the author.