10 – Java Control Flow Statements

Selenium Class 10 – Java Control Flow Statements

Java has three types of Control Flow Statements
i) Conditional / Decision Making Statements
ii) Loop Statements
iii) Branching Statements

Java Operators Continuation

d) Java Logical Operator Examples

Example 1:
boolean a= true, b=false;

System.out.println(!(a&&b));//true
System.out.println(a&&b);//false
System.out.println(a||b);//true

Example 2:
int a=1000, b=500, c=900;

if ((a>b)&&(a>c)){
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“A is Not big Number”);
}

Example 3:
int a=1000, b=500, c=1200;

if ((a>b)||(a>c)){
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“A is Not big Number”);
}

Example 4:
int a=1000, b=500, c=700;

if (!(a>b)&&(a>c)){
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“A is Not big Number”);
}

e) Assignment Operators

int a=10, b=20;

System.out.println(a==b);//false (Comparison)
System.out.println(a=b);//20 (Assignment)

1) Assignment =

2) Add and Assign +=

3) Subtract and Assign -=

4) Multiply and Assign *=

Example:
int a=10;

System.out.println(a);//10

a=a+10;
System.out.println(a);//20

a-=10;
System.out.println(a);//10

a*=10;
System.out.println(a);//100

Java has three types of Control Flow Statements

i) Conditional / Decision Making Statements

a) Two types of Conditional Statements

1) if statement
2) switch statement

b) Three types of Conditions

1) Single Condition (Positive and Negative Conditions)

Syntax:
if (condition){
.
.
}
Positive condition
if (a>b) {
.
.
}

Negative Condition

if (!(b>a) {
.
.
}

2) Compound Condition (Positive and Negative Conditions)

Positive Condition
if ((condition1) && Or ||(condition2) {
.
.
}

Negative Condition
if (!(condition1) && Or || (condition2){
.
.
}

3) Nested Condition

if (condition1){
if (condition2){
if (condition3){
.
.
}
}
}

c) Usage of Conditional Statements

1) Execute a block of statements when a condition true

Syntax:

if (condition){
Statements
.
.
.
}

Example:

int a=100, b=500;

if (a>b){
System.out.println(“A is a Big Number”);
}

2) Execute a block of statements when a condition is true, otherwise execute another block of statements

Syntax:
if (condition){
.
.
}
else
{
.
.
}

Example:
int a=100, b=100;

if (a>b){
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“B is a Big Number”);
}
}
}

3) Execute a block of statements when a compound condition is true

Syntax:

if ((condition1) && or || (condition2) && or || (condition3)){
.
.
}

Example:

int a=100, b=90, c=700;

if ((a>b) || (a>c)){
System.out.println(“A is a Big Number”);
}

4) Decide between several alternates (else if)

Syntax:

if (condition){
.
.
}
else if (condition){
.
.
}
else if (condition){
.
.
}
else if (condition){
.
.
}
else {
.
.
}

In Software Testing:
Requirement – Test Case / Test Script

In Software Development:
Problem – Solution

Problem: Initialize an Integer variable and verify the range

if the number is in between 1 and 100 then display “Number is a Small Number”
if the number is in between 101 and 1000 then display “Number is a Medium Number”
if the number is in between 1001 and 10000 then display “Number is a Big Number”
if the number is more than 10000 then display “Number is a High Number”
Otherwise display “Number is either Zero or Negative Number”

Solution:
Input:
1) 50
2) 150
3) 1500
4) 15000
5) 0
6) -100

if ((val>0)&& (val<=100)){ System.out.println(“Val is a Small Number”); } else if ((val>100) && (val<=1000)){ System.out.println(“Val is a Medium Number”); } else if ((val>1000) && (val<=10000)){ System.out.println(“Val is a Big Number”); } else if (val>10000){
System.out.println(“Val is a High Number”);
}
else
{
System.out.println(“val is either Zero or Negaive Number”);
}

Assignment:

  • Handle Invalid Input for the above Program – Bharathi
    Note: It has to accept Integer only (for every iteration)

6) Execute a Block of statements when more than one condition is true

Syntax:

if (condition1){
if (condition2){
if (condition3){
Statements
.
.
}
}
}

Example:

int a=100, b=90, c=80, d=70;

if (a>b){
if (a>c){
if(a>d){
System.out.println(“A is a Big Number”);
}
}
}

Syntax 2:

if (condition1){
if (condition2){
if (condition3){
Statements
.
.
}
else {
.
}
}
else {
.
}
}
else {
.
}

Using Nested If:

int a=100, b=900, c=80, d=70;

if (a>b){
if (a>c){
if(a>d){
System.out.println(“A is a Big Number”);
}
else{
System.out.println(“A is Not a Big Number -3rd Condition is False”);
}
}
else{
System.out.println(“A is Not a Big Number -2nd Condition is False”);
}
}
else{
System.out.println(“A is Not a Big Number-1st Condition is False”);
}

Using Compound Condition:

int a=100, b=900, c=800, d=700;

if ((a>b)&&(a>c)&&(a>d)){
System.out.println(“A is a Big Number”);
}
else{
System.out.println(“A is Not Big Number”);
}

Nested Condition vs. Compound Condition

In Nested Condition we can write multiple else parts, where as in Compound Condition single else part only…

Problem: Find the biggest Number between four numbers

Hint: Use else if and compound condition

Java Program:
int a=100, b=90, c=80, d=200;

if ((a>b)&&(a>c)&&(a>d)){
System.out.println(“A is a Big Number”);
}
else if ((b>a)&&(b>c)&&(b>d)){
System.out.println(“B is a Big Number”);
}
else if ((c>a)&&(c>b)&&(c>d)){
System.out.println(“C is a Big Number”);
}
else {
System.out.println(“D is a Big Number”);
}

Assignment:
If two or more variables having same value the check it and display the output
Ex: “B and C are Big Numbers”