11 – Java Control Flow Statements – 2

Selenium Class 11 – Java Control Flow Statements Part -2

Java Loop Statements and Branching Statements

Java Conditional Statements Continuation:

7) Decide among several alternates (using switch statement)
Expression: An expression in a programming language is a combination of one or more constants, variables, operators, and functions that the programming language interprets

Syntax:

switch (expression){
case1 value:
statements
…………..
…………..
break;

case2 value:
statements
…………..
…………..
break;

case3 value:
statements
…………..
…………..
break;
case4 value:
statements
…………..
…………..
break;

default:
statements
…………..
…………..
}

Example:

char grade =’1′;

switch (grade){

case ‘A’:
System.out.println(“Excellent”);
break;

case ‘B’:
System.out.println(“Good”);
break;

case ‘C’:
System.out.println(“Better”);
break;

default:
System.out.println(“Invalid Grade”);
}

Note:
You can use Condition/s only in your program, or Loop/s only in your Program and You can insert Loop/s in Condition/s and vice versa.

ii) Java Control Flow – Loop Statements

1) for loop
2) while loop
3) do while loop
4) enhanced for loop

1) for loop

Description: It repeats a block of statements for a specified number of times

Syntax:

for (StartValue; EndValue; Increment/Decrement){
.
.
}

Example1: Print 1 to 10 Numbers…

for (int i=1; i<=10; i++){
System.out.println(i);
}

Example2: Print 1 to 5 Numbers except 4th Number

for (int i=1; i<=5; i++){
if (i!=4){
System.out.println(i);
}
}

Example3: Print 1 to 10 Numbers in reverse Order

for (int i=10; i>=0; i–){
System.out.println(i);
}

Example4: Print 1 to 10 Numbers in reverse Order except 4th and 9th numbers
for (int i=10; i>=1; i–){
if ((i!=4)&&(i!=9)){
System.out.println(i);
}
}

2) while loop

Description: It repeats a block of statements while condition is true

Syntax:

Initialization;
while (condition){
statements
.
.
increment/decrement;
}

Example1: Print 1 to 10 Numbers…

int i=1;

while (i<=10){
System.out.println(i);
i++;
}

Example2: Print 1 to 5 Numbers except 4th Number

while (i<=5){
if (i!=4){
System.out.println(i);
}
i++;
}

Example3: Print 1 to 10 Numbers in reverse Order

int i=10;

while (i>=1){
System.out.println(i);
i–;
}

Example4: Print 1 to 10 Numbers in reverse Order except 4th and 9th numbers

int i=10;

while (i>=1){
if ((i!=4) && (i!=9)){
System.out.println(i);
}
i–;
}

3) do while loop

Description: It repeats a block of statements while condition is true and it executes statements at least once irrespective of the condition

Syntax:

Initialization;
do
{
Statements
.
.
Increment/Decrement;
} while (condition);

Example1: Print 1 to 10 Numbers…

int i=1;

do
{
System.out.println(i);
i++;
} while(i<=10);

Example2:
int i=20;

do
{
System.out.println(i);
i++;
} while(i<=10);

Example3: Print 1 to 10 Numbers in reverse order
int i=10;

do
{
System.out.println(i);
i–;
} while(i>=1);

Example4: Print 1 to 5 Numbers except 3rd Number

do
{
if (i!=3){
System.out.println(i);
}
i++;
} while(i<=5);

4) Enhanced for loop

It executes all elements in an Array

Syntax:

Array Declaration;
for (declaration: Expression/Array){
Statements
.
.
}

Example1:

String [] languages={“COBOL”, “C”, “Java”, “VBScript”};

for (String lang: languages){
System.out.println(lang);

Example2:

public static void main(String[] args) {
int a=10, b=5;
int [] mathOperations = new int[3];

mathOperations[0]= a+b;
mathOperations[1]= a-b;
mathOperations[2]= a*b;

for (int operation: mathOperations){
System.out.println(operation);
}

iii) Java Control Flow – Branching Statements

  • Branching statements are used to transfer control from one point to another in the code
  • Branching statements are defined by three keywords – break, continue and return

1) break

  • break statement is used to stop the execution and comes out of loop
  • Mostly break statements are used in switch and in loops….

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

2) continue

  • continue statement is also same as break statement, the only difference is when break statement executes it comes out from the loop, whereas continue statement executes comes out from the loop temporarily.

Example:
for (int i=1; i<=10; i++){
if (i%2 != 0){
continue;
}
System.out.println(i);
}

3) return

  • return statement is used in User defined methods (methods with return value) and return statement must be always last statement in the method

Example:
public class ControFlow {

public static void main(String[] args) {
ControFlow obj = new ControFlow();
int res= obj.add(100, 50);
System.out.println(res);//150

System.out.println(obj.add(100, 200));//300
}
public int add(int a, int b){
int result=a+b;
return result;
}
}

//Create Object

(ClassName objectName = new ClassName();
ControFlow obj = new ControFlow();

DataType variableName = Object.Method(Values for Arguments)
int res= obj.add(100, 50);