You are on page 1of 9

RESEARCH

1. Java Control Structures


A control structure is a syntactic form in a programming language that expresses the flow of control over
a specific list of instructions to make decisions among the alternate path or given paths and executes the
instructions in the sequence in which they appear, one by one. This condition is called sequence
accomplishment. In computer programming, the statement that will be accomplished next is not
necessarily located in the next line. This scenario is known as the transfer of program control.

In Java, there are three types of control structures.

1. Conditional Branches or Statements: for choosing between two or more paths. There are
three types in Java:

 if/else/else if
If the statement is the most simple decision-making statement in Java. We can use this to decide
whether a certain statement or block of statements will be executed or not. Simply, if a certain
condition is true then a block of the statement will be executed otherwise not.
Syntax

if(condition){
//code to be executed if the condition is true
}
If the test expression is evaluated to true, statements inside the body of if are executed. If the test
expression is evaluated to false, statements inside the body of if are skipped.

int Marks=80;
if(Marks>50){
System.out.print(“You have passed the exam”);
}
For else (and else if), in addition to the condition check using if, here we include an alternative choice
(maybe choices) by including else (and else if). You can combine an else and an if to make an else if and
test a whole range of mutually exclusive possibilities. Simply, by using else art, we can evaluate more
than 1 condition at a time (if required).
Syntax

if(condition){
//code to be executed if the condition is true
}else{
//code to be executed if the above condition is false
}
int count=3;
if (count > 2) {
System.out.println("Count is higher than 2");
} else {
System.out.println("Count is lower or equal than 2");
}
In case of if, else if; Syntax

if(condition1){
//code to be executed if the condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}

else{
//code to be executed if all the above conditions are false
}
int i = 20;
if (i == 10){
System.out.println(“i is 10”);
}else if (i == 15){
System.out.println(“i is 15”);
}else if (i == 20){
System.out.println(“i is 20”);
}else{
System.out.println(“i is not present”);
}
 Nested if
Nested if statements mean an if statement inside an if statement.
Syntax

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
 switch
If we have multiple cases to choose from, we can use a switch statement (multi-way branch statement).

Syntax
switch(expression) {
case n:
code block
case n:
code block
default:
default code block
}
The switch expression (condition)is evaluated once and the value of the expression is compared with the
values of each case. If there is a match, the associated block of code will be executed. It is not matched,
the program will look for the optional default clause, and if found, transfers control to that clause,
executing the associated statements.
Example

int inDay = 2;
String day;
switch (inDay) {
case 1: day = “Subday”;
case 2: day = “Monday”;
case 3: day = “Tuesday”;
case 4: day = “Wednesday”;
case 5: day = “Thursday”;
case 6: day = “Friday”;
case 7: day = “Saturday”;
default: day = “Invalid entry”;
}
System.out.println("Selected day is: " + day);
The output will be
Selected day is: Monday
We can use ternary operators as well in these Conditional Statements.
How? Check this if-else statements

if (count > 2) {
System.out.println(“Count is higher than 2”);
} else {
System.out.println(“Count is lower or equal than 2”);
}

This can be written as


System.out.println(count > 2 ? “Count is higher than 2” :
“Count is lower or equal than 2”);

2. Iteration Statements

Iteration statements create loops in the program. In other words, it repeats the set of
statements until the condition for termination is met. Iteration statements in Java are for,
while and do-while.

1. while statement

The while loop is Java’s most fundamental loop statement. It repeats a statement or block


while its controlling expression is true. Here is its general form:

while(condition) {
// body of loop
}

The condition can be any boolean expression. The body of the loop will be executed as long
as the conditional expression is true.
Here is more practical example.

// Demonstrate the while loop.


class While {
public static void main(String args[]) {
int n = 10;
while (n > 0) {
System.out.println("tick " + n);
n--;
}
}
}

The ouput of the program is:

tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1

2. do-while statement

The do-while loop always executes its body at least once, because its conditional
expression is at the bottom of the loop. Its general form is:

do{
// body of loop
} while(condition);

Each iteration of the do-while loop first executes the body of the loop and then evaluates the
conditional expression. If this expression is true, the loop will repeat. Otherwise, the loop
terminates. As with all of Java’s loops, condition must be a Boolean expression.

The program presented in previous while statement can be re-written using do-while as:

// Demonstrate the do-while loop.


class DoWhile {
public static void main(String args[]) {
int n = 10;
do {
System.out.println("tick " + n);
n--;
} while (n > 0);
}
}

3. for loop

Here is the general form of the traditional for statement:

for(initialization; condition; iteration) {


// body
}

The program from previous example can be re-written using for loop as:

// Demonstrate the for loop.


class ForTick {
public static void main(String args[]) {
int n;
for (n = 10; n > 0; n--) System.out.println("tick " + n);
}
}

The ouput of the program is same as output from program in while loop.


There can be more than one statement in initilaization and iteration section. They must be
separated with comma.

Here, we've presented the example to illustrate this.

// more than one statement using the comma.


class Comma {
public static void main(String args[]) {
int a, b;
for (a = 1, b = 4; a < b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
}
}

The ouput of the program is:

a = 1
b = 4
a = 2
b = 3

Here, the initialization portion sets the values of both a and b. The two comma separated
statements in the iteration portion are executed each time the loop repeats.

Nested Loops

Loops can be nested as per requirement. Here is example of nesting the loops.
// nested loops
class NestedLoop {
public static void main(String args[]) {
int i, j;
for (i = 0; i < 8; i++) {
for (j = i; j < 8; j++)
System.out.print(".");
System.out.println();
}
}
}
Here, two for loops are nested. The number times inner loop iterates depends on the value of
i in outer loop.
The output of the program is:

........
.......
......
.....
....
...
..
.

for-each loop

It is advanced form of for loop. It is easier while iterating over the collection of element and
also the advanced for-each construct gets rid of the clutter and the opportunity for error.

Here is how the example looks with the for-each construct:

for(element: collection) {
// body
}

Example: (from Oracle)

void cancelAll(Collection<TimerTask> c) {
for (TimerTask t : c)
t.cancel();
}

The loop above reads as “for each TimerTask t in c.”

You might also like