2_Flow Control Statements
2_Flow Control Statements
Instructor: DieuNT1
• while
• do..while
• for
• break,continue, return
Arrays
Data structures
c[ 3 ] 72
c[ 4 ] 1543
c[ 7 ] 62
c[ 8 ] -3
c[ 9 ] 1
datatype[] identifier;
datatype[] identifier = new datatype[size];
datatype[] identifier = {value1,value2,…valueN};
▪ You can also place the square brackets after the array's name:
datatype identifier[];//this form is discouraged
▪ Example:
byte[] bArray;
float[] fArray = new float[20];
int[] iArray = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
return max;
}
}
Row 0
a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1
a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2
a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column index
Row index
Array name
// Printing 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
▪ Output: Software
Loops
• while loop
• do-while loop
• for loop
Branching
• break
• continue
• return
▪ Note:
✔“else” is optional
✔Alternative way to if-else is
conditional operator ( ?: )
case value_1:
statement_1; [ break;]
case value_2:
statement_2; [ break;]
…
case value_n:
statement_n; [ break;]
default:
statement_n+1; [break;]
}
while (condition) {
action statements;
}
do {
action statements;
} while (condition);
• The unlabeled form skips to the end of the innermost loop's body
and evaluates the boolean expression that controls the loop.
(Nếu có đủ thời gian thì cho thực hành/demo tại lớp, nếu không thì học viên có thể được
hướng dẫn để làm thêm ở nhà).
• while
• do..while
• for
• break,continue, return