Java Operators and Conditional Statements

Java Operators and Conditional Statements

i) Java Operators

ii) Java Conditional Statements

i) Java Operators

Operators are used to perform Mathematical, Comparison and Logical Operations.

Important Categories of Operators

1) Arithmetic Operators

2) Relational Operators

3) Assignment Operators

4) Logical Operators
——————————-
1) Arithmetic Operators

Mathematical Operations

Addition

Subtraction

Multiplication

Division

Integer Division

Modules

Exponentiation
——————————-
Java Arithmetic Operators

a) Addition + (Addition, String Concatenation)

b) Subtraction – (Subtraction, Negation)

c) Multiplication *

d) Division /

e) Modules %

f) Increment ++

g) Decrement —
——————————-
Example:

public static void main(String[] args) {
int a =10, b=3, e=-100;
String c =”Selenium”, d = ” Testing”;

System.out.println(“Addition of a, b is “+ (a+b));
System.out.println(c+d);//Selenium Testing

System.out.println(a-b);//7
System.out.println(e);

System.out.println(a*b);//30

System.out.println(a/b);//3
System.out.println(10.0/3);//3.333333

System.out.println(a % b);//1

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

b=10;
a = –b;
System.out.println(a);//9

double x = Math.pow(10, 4); //10000
System.out.println(x);
//————————————-
float y =1.2F, p=2.34f;
double z =1.345678, q=234.456787;

System.out.println(y+p);//3.54
System.out.println(z+q);
}
}
——————————-
2) Relational Operators

= (Assignment)
== (Comparison/Relation)
——————————-
a) ==

b) !=

c) >

d) >=

e) <

f) <=
—————————–
Note: Relational Operators return Boolean / Logical Result (true/false)

Example:

public static void main(String[] args) {
int a, b;
a=10; b=20;

System.out.println(a == b);//false
System.out.println(a != b);//true
System.out.println(a > b);//false
System.out.println(a >= b); //false
System.out.println(a < b); //true
System.out.println(a <= b); //true
}
——————————-
3) Assignment Operators

a) Assignment =

b) Add and Assign +=

c) Subtract and Assign -=

d) Multiply and Assign *=
——————————-
Example:

public static void main(String[] args) {
int a =10;
System.out.println(a);//10

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

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

a *= 10;
System.out.println(a);//100
}
————————
4) Logical Operators

a) Logical Not operator !

b) Logical And operator &&

c) Logical Or operator ||
——————————-
Result Criteria for Logical Not Operator

Operand 1 Operand 2 Result
———————————-
true             true               false
true             false              true
false            true               true
false            false              true
———————————-
Result Criteria for Logical And Operator

Operand 1 Operand 2 Result
——————————-
true true true
true false false
false true false
false false false
——————————-
Result Criteria for Logical Or Operator

Operand 1 Operand 2 Result
——————————-
true true true
true false true
false true true
false false false
——————————-
Example:

public static void main(String[] args) {
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:

public static void main(String[] args) {
int a =10000, b =5000, c =700;
if ((a > b) && (a > c)){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“A is Not a Big Number”);
}
}
——————————-
public static void main(String[] args) {
int a =10000, b =50000, c =70000;
if ((a > b) || (a > c)){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“A is Not a Big Number”);
}
}
———————————-
public static void main(String[] args) {
int a =100, b =50, c =70;
if (!(a > b) && (a > c)){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“A is Not a Big Number”);
}
}
——————————-
Java Control Flow
i) Conditional Statements
ii) Loop Statements

Java Operators and Control Flow Statements

ii) Java Conditional Statements

a) Usage of Conditional Statements in Test Automation

1) To insert Verification Points

2) Error Handling
——————————-
b) Two types of Conditional Statements in Java

1) if statement

2) switch statement
—————————————
c) Types of Conditions

1) Single Condition (Positive and Negative)

ex:

Positive Condition

if (a > b) {
.
.
}

Negative Condition

if (!(a < b)) {
.
.
}
————————————–
2) Compound Condition

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

Or

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

Or

if ((a > b) && (a > c) && (a>d)) {
.
.
}
——————————-
3) Nested Condition

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

public static void main(String[] args) {
int a =100, b =50;

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

public static void main(String[] args) {
int a =10, 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 ((condition 1) && Or || (condition 2)){
Statements
——————
———-
}

Example:

public static void main(String[] args) {
int a =100, b =50, c = 400;

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:

public static void main(String[] args) {
int a =100, b =50;

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”

Java Program:
—————————————–
Input Data

1st Iteration 50

2nd Iteration 400

3rd Iteration 4000

4th Iteration 11000

5th Iteration 0

6th Iteration -100
——————-
public static void main(String[] args) {
int a = -100;

if (( a > 0) && ( a <= 100)){
System.out.println(“A is a Small Number”);
}
else if ((a > 100) && ( a <=1000)){
System.out.println(“A is a Medium Number”);
}
else if ((a > 1000) && ( a <=10000)){
System.out.println(“A is a Big Number”);
}
else if (a > 10000){
System.out.println(“A is High Number”);
}
else{
System.out.println(“A is Either Zero or Negative value”);
}
}
——————————-
5) Execute a block of statements when more than one condition is True (Nested if).

Syntax:

if (condition){
if (condition){
if (condition){
Statements
————–
————
———–
}
}
}
——————————-
Check if the value of a variable is bigger than b, c, d variable values or not?

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
———————————-
public static void main(String[] args) {
int a = 100, b =50, c=70, d =900;

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 Compound condition single else part only.
—————————————–
Problem: Get Biggest Number out of Four Numbers

Hint: use Compound conditions and else if structures

Solution:

public static void main(String[] args) {
int a = 100, b =500, c=700, d =900;

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”);
}
———————————–

(59)

You may also like...

Leave a Reply