Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Control Structures in Java

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Control Structures in Java

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Control Structures in Java

1. conditional statements in Java


 Use if to specify a block of code to be executed, if a specified condition is true
 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

if Statement
 Use the if statement to specify a block of Java code to be executed if a condition is true.

Syntax:
if (condition) {
// block of code to be executed if the condition is true
}

else Statement
Use the else statement to specify a block of code to be executed if the condition is false.

Syntax:
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
}

else if Statement
Use the else if statement to specify a new condition if the first condition is false.

Syntax:
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
}

Short Hand If...Else (Ternary Operator)


There is also a short-hand if else, which is known as the ternary operator because it consists of three operands.
It can be used to replace multiple lines of code with a single line, and is most often used to replace simple if else
statements:

Syntax:
variable = (condition) ? expressionTrue : expressionFalse;

Example
int time = 20;
String result = (time < 18) ? "Good day." : "Good evening.";
System.out.println(result);

1 Prepared by Sisira Palihakkara


Switch Statements
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:

Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
 The switch expression is evaluated once.
 The value of the expression is compared with the values of each case.
 If there is a match, the associated block of code is executed.
 The break and default keywords are optional, and will be described later in this chapter

break Keyword
 When Java reaches a break keyword, it breaks out of the switch block.
 This will stop the execution of more code and case testing inside the block.

2. Loop statements in Java

Loops can execute a block of code as long as a specified condition is reached.


 while Loop
 do/while Loop

while Loop
The while loop loops through a block of code as long as a specified condition is true:
Syntax:
while (condition) {
// code block to be executed
}

Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}

do/while Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the condition is true.

Syntax:
do {
// code block to be executed
}

2 Prepared by Sisira Palihakkara


while (condition);

Example:
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);

for Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of
a while loop:

Syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.

Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}

for-each Loop
There is also a "for-each" loop, which is used exclusively to loop through elements in an array:

Syntax:
for (type variableName : arrayName) {
// code block to be executed
}

Example:
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String i : cars) {
System.out.println(i);
}

Java Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type with square brackets:

String[] cars;
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
int[] myNum = {10, 20, 30, 40};

Access the Elements of an Array


You can access an array element by referring to the index number.

3 Prepared by Sisira Palihakkara


Array Length
To find out how many elements an array has, use the length property:

Java Break statement


The break statement can also be used to jump out of a loop.

Example:
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}

Java Continue statement


The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the
next iteration in the loop.

Example
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
System.out.println(i);
}

******

4 Prepared by Sisira Palihakkara

You might also like