Module 4 Control Structures and Arrays
Module 4 Control Structures and Arrays
(Java)
Module 4
Control Structures and Arrays
Understand the different control structures and their
functions in programs
4.1
• Flow Controls
if-else Statement
The if statement enables your program to
selectively execute other statements, based
on some criteria.
The else statement performs a different set of
statements if the expression is false.
Syntax:
if(condition/boolean expression) {
//codes to execute if the condition is true
}
else {
//codes to execute if the condition is false
}
Control Structures and Arrays
switch Statement
The switch statement is used to conditionally
perform statements based on an integer
expression.
Syntax:
switch(varName)
{
case const1: codes here; break;
case const2: codes here; break;
default: codes here; break;
}
Syntax:
for (initialization; condition; altering list) {
//codes to execute if condition is true
}
Syntax:
for(initialization : [collection/array]) {
//codes to execute if condition is true
}
Control Structures and Arrays
foreach Loop Example
OUTPUT:
100 90 80
4.2
Array length is 10
Element at index 8
Example:
String names[ ] = new String[5];
int [ ]grades = new int[15];
double[ ] grades;
grades = new double[2];
Example:
String names[ ] ={“maria”,”blanco”,”forever”};
Example:
names[0] = “John Doe”;
names[1] = “Jane Doe”;
Example:
int[ ][ ] = new int[5][4];