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, andswitch.:- 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)
- Conditional statements - used to perform different actions based on different conditions(
- 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:
ifstatement - executes a block of code if a specified condition is trueif...elsestatement - executes a block of code if a specified condition is true and another block of code if that condition is falseif...else if...elsestatement - executes different codes for more than two conditionsswitchstatement - selects one of many code blocks to be executed
The if Statement
- The
ifstatement 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
ifstatement 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...elsestatement 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...elsestatement 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
switchstatement 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:
whileloop - loops through a block of code as long as a specified condition is truedo...whileloop - loops through a block of code once, and then repeats the loop as long as a specified condition is trueforloop - loops through a block of code a specified number of timesfor-eachloop - 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.