Java Flow Control - Conditional and Loop Statements

preview_player
Показать описание
Java Flow Control Statements, Java Conditional and Loop Statements, if condition, else if ,nested if and switch statement. Java for loop, while loop, do while loop, and enhanced for loop.
Conditional statements within loops and Loops within conditions.

i) Java Conditional Statements

ii) Java Loop Statements
----------------------------------------------------
Usage of Conditional Statements

1) Execute a Block of statements when condition is true

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

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

4) Decide among several alternates (else if)

5) Execute a block of statements when more than one condition is true

6) Decide among several alternates (using switch statement)
---------------------------------------------------
Java Loop Statements

Four Loop structures in Java

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.

2) while loop

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

3) do while loop

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

Note: It executes statements at least once irrespective of the condition

4) Enhance for loop

Description: It executes all elements in an Array
------------------------------------------------------

We can use,

a) Only Conditions

b) only Loops

c) Condition/s within Loop/s

d) Loop/s within Condition/s
-----------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Class Notes:

Java Flow Control - Conditional and Loop Statements

i) Java Conditional Statements

ii) Java Loop Statements

i) Java Conditional Statements

a) Usage of Conditional Statements in Test Automation

1) To insert Verification points

2) Error Handling

b) Types of Conditional Statements

1) if statements

2) switch statement

c) Types of Conditions

1) Single Condition (Positive and Negative Conditions)

Ex:

Positive Condition

if (a > b) {
.
.
}

Negative Condition

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

2) Compound Condition

Ex:

if ((a>b) && (a>c)) {
.
.
.
}

3) Nested Condition

Ex:

if (a>b){
if (a>c) {
if (a>d){
.
.
.
}
}
}

d) Usage of Conditional Statements

1) Execute a Block of statements when condition is true

Syntax:

if (condition) {
Statements


}

Example:

Positive Condition

int a =100, b=50;

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

Negative Condition

int a =100, b=50;

if (!(a < b)){
System.out.println("A is a Big Number");
}

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

Syntax:

if ((condition1) && Or || (condition2)){
Statements


}

Example:

int a =100, b=500, c =40;

if ((a > b) && (a > c)){
System.out.println("A is a Big Number");
}

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

Syntax:

if (condition) {
Statements


}
else
{
Statements


}

Example:

int a =100, b=500;

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

4) Decide among several alternates (else if)

Syntax:

if (condition) {
Statements


}
else if (condition) {
Statements


}
else if (condition) {
Statements


}
else if (condition) {
Statements


}
else {
Statements


}

Example:

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 High Number"
otherwise display "Number is either Zero or Negative Number"

Input Data

1st iteration 50

2nd iteration 400

3rd iteration 4000

4th Iteration 11000

5th iteration 0

6th iteration -100

int val =-100;

if ((val >= 1) && (val <= 100)){
System.out.println("Number is a Small Number");
}
else if ((val > 100) && (val <=1000)) {
System.out.println("Number is a Medium Number");
}
else if ((val > 1000) && (val <=10000)) {
System.out.println("Number is a Big Number");
}
else if (val > 10000){
System.out.println("Number is High Number");
}
else {
System.out.println("Number is either Zero or Negative Number");
}

5) Execute a block of statements when more than one condition is true

Syntax:

if (condition){
if (condition) {
if (condition) {
Statements


}
}
}

Example:

Check if the value of a variable is bigger than b, c, d variable values or not?

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");
}
else {
System.out.println("A is Not a Big Number");
}
}
else {
System.out.println("A is Not a Big Number");
}
}
else {
System.out.println("A is Not a Big Number");
}

Using Compound Condition

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

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

Advantage of nested condition over compound condition

In Nested condition we can write multiple else parts
In Compond condition we can write single else part only

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

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

Problem: Get biggest number out of 4 numbers

Hint: Use else if structures and Compound condition

Solution:

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

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");
}

6) Decide among several alternates (using switch statement)

Syntax:

switch (Expression){

case value:
Statements


break;

case value:
Statements


break;

case value:
Statements


break;

default:
Statements



}

Example:

char grade ='Z';

switch (grade){
case 'A':

break;

case 'B':
System.out.println("Good");
break;

case 'C':
System.out.println("Better");
break;

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

ii) Java Loop Statements

Four Loop structures in Java

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 (dataType variableName = stratvalue; endValue; increment/decrement) {
Statements


}

Examples:

Ex 1: Print 1 to 10 Numbers

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

Ex 2: Print 10 to 1 Numbers

for (int i = 10; i >= 1; i--){
System.out.println(i);
}

Ex 3: Print 1 to 10 Numbers except 7th number

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

Ex 4: Print 1 to 10 Numbers except 4th and 7th numbers

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

ex 5: Print even numbers up to 20

for (int i = 2; i <= 20; i +=2){
System.out.println(i);
}

ex 6: Print even numbers up from 20 to 2

for (int i = 20; i >= 2; i -=2){
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
}

Examples:

Ex1: Print 1 to 10 Numbers

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

Ex2: Print 10 to 1 Numbers

int i=10;
while (i >=1){
System.out.println(i);
i--;
}

Ex 3: print 1 to 10 Nubers except 4th and 7th numbers

int i=1;
while (i <=10){
if ((i != 4) && (i !=7 )){
System.out.println(i);
}
i++;
}

Ex 4: Print even numbers up to 20

int i=2;
while (i <=20){
System.out.println(i);
i += 2;
}

3) do while loop

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

Note: It executes statements at least once irrespective of the condition

Syntax:
Initialization
do
{
Statements


increment/decrement;
} while (condition);

Example 1: Print 1 to 10 Numbers

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

Example 2: Print 10 to 1 Numbers

int i =10;
do
{
System.out.println(i);
i--;
} while (i >= 1);

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

4) Enhance for loop

Description: It executes all elements in an Array

Syntax:

Array Declaration

for (declaration: Array){
Statements

}

Example 1: Print all elements an Array

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

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

Example 2:

int a=10, b=20;

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

for (int operationOutput: mathOperations){

}

We can use,

a) Only Conditions

Example:
int a=123, b=300;

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

b) only Loops

example:

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

c) Condition/s within Loop/s

Example:

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

Assignment

4) Loop/s within Condition/s

Example: Insert a loop statement within if condition

gcreddy
welcome to shbcf.ru