Chapter 3 Descion & Repeation Statements & Arrays 1
Chapter 3 Descion & Repeation Statements & Arrays 1
statement
statement
statement
statement
statement statement
AU/Computer Sc. Dept./OOP lecture
slides 2
What are Control
Structures?
Control structures alter the flow of the
program, the sequence of statements
that are executed in a program.
if (expression) { yes
statement1;
} execute
statement
rest_of_program
execute
rest_of_program
AU/Computer Sc. Dept./OOP lecture
slides 6
If-Else Statement
if (expression) {
statement1;
}
else{
statement2;
}
next_statement;
For example:
switch (expression) {
case value1:
statement1;
case value2:
statement2;
default:
default_statement;
}
Every statement after the true case is executed
Do default action
Continue the
program
case value2: n
// Do value2 thing
break;
expression y
... equals Do value2 thing break
default: value2?
// Do default action
break;
} n
// Continue the program
do default action
Continue the
break
program
int sum = 0;
int i = 1;
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
int sum = 0;
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
int n = 0;
for(; n <= 100;) {
System.out.println(++n);
}
AU/Computer Sc. Dept./OOP lecture
slides 22
The for loop
Initialize count
The while loop
n n
Test condition Test condition
is true? is true?
y
y
Execute loop
statement(?) Execute loop
statement(s)
Increment
Next statement count
New statement
AU/Computer Sc. Dept./OOP lecture
slides 23
The continue Statement
The continue statement causes the
program to jump to the next iteration of
the loop.
/**
* prints out "5689"
*/
for(int m = 5; m < 10; m++) {
if(m == 7) {
continue;
}
System.out.print(m);
} AU/Computer Sc. Dept./OOP lecture
slides 24
Another continue example:
int sum = 0;
for(int i = 1; i <= 10; i++){
if(i % 3 == 0) {
continue;
}
sum += i;
}
Specifies an array of
variables of type int
We are creating
a new array object
For example:
int[] prices;
String[] names;
AU/Computer Sc. Dept./OOP lecture
slides 32
Creating a New "Empty"
Array
Use this syntax: new int[20]
The new keyword creates an array of
type int that has 20 compartments
The new array can then be assigned to
an array variable:
int[] prices = new int[20];
When first created as above, the items
in the array are initialized to the zero
value of the datatype
int: 0 double: 0.0 AU/Computer
String: null
Sc. Dept./OOP lecture
slides 33
Array Indexes
Every compartment in an array is
assigned an integer reference.
This
number is called the index of the
compartment
Assignvalues to
compartments:
prices[0] = 6.75;
prices[1] = 80.43;
prices[2] = 10.02;
String[] names = {
"David", "Qian", "Emina",
"Jamal", "Ashenafi" };
int numberOfNames = names.length;
System.out.println(numberOfNames);
Output: 5
Output:
Hello Aisha.
Hello Tamara.
Hello Gikandi.
Hello Ato.
Hello Lauri.
Example:
names[0] = “Bekele"
Now the first name in names[] has been
changed from "Aisha" to "Bekele".
So the expression names[0] now
evaluates to "Bekele".
Note:The values of compartments can
change, but no new compartments may
be added. AU/Computer Sc. Dept./OOP lecture
slides 41
Example
b. int[] arr;
arr = new int[4];
Example:
A landscape grid of a 20 x 55 acre piece of land:
We want to store the height of the land at each
row and each column of the grid.