0% found this document useful (0 votes)
63 views28 pages

Java Operators and Control Statements Guide

Chapter 2 covers Operators and Control Statements in Java, detailing various types of operators such as Arithmetic, Relational, Logical, and Assignment Operators. It explains Control Statements including Conditional Statements (like if-else and switch) and Looping Statements (like while, do-while, and for loops) for controlling program execution flow. Additionally, it discusses Branching Statements like break and continue, and provides examples of Java programs demonstrating these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views28 pages

Java Operators and Control Statements Guide

Chapter 2 covers Operators and Control Statements in Java, detailing various types of operators such as Arithmetic, Relational, Logical, and Assignment Operators. It explains Control Statements including Conditional Statements (like if-else and switch) and Looping Statements (like while, do-while, and for loops) for controlling program execution flow. Additionally, it discusses Branching Statements like break and continue, and provides examples of Java programs demonstrating these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter-2 : Operators & Control Statements

Operators
-> Operator is a symbol which performs some operation on operands

int a = 10 ;

int b = 20 ;

int c = a + b;

-> We have below operators in java

1) Arithmetic Operators

2) Logical Operators

3) Relational Operators

4) Assignment Operators

5) New operator

6) dot (.) Operator

7) Ternary operator (Conditional Operator)

-> Arithmetic Operators are used to perform Arithmetic Operations ( Calculations )

1) Addition -----> +

2) Subtraction ----> -

3) Division ------------> / (quotient)

4) Multiplication ----------> *

5) Modules ---------> % (reminder)

6) Increment -------> ++

7) Decrement ------> --
Increment and Decrement Operators
--> Increment (++) is used to increase the value of variable by 1

-> Increment is divided into 2 types

1) Post Increment ( a ++ )

2) Pre Increment ( ++ a )

--> Decrement (--) is used to decrease the value of variable by 1

-> Decrement is divided into 2 types

1) Post Decrement ( a -- )

2) Pre-Decrement ( --a )

class PostIncrement {

public static void main(String... args){

int a = 5;

[Link](a++); // it will print 5 then it will become 6

a++; // it will become 7

[Link](a++); // it will print 7 then it will become 8

[Link](a); // it will print 8

class PreIncrement {

public static void main(String... args){

int a = 5;

[Link] ( ++ a ); // it will become 6 then it will print

++ a ; // it will become 7
[Link](++a); // it will become 8 then it will print

[Link](a); // it will print 8

class PostPreIncrement {

public static void main(String... args){

int a = 5;

int b = ++a + a++ + a++ + ++a;

// int b = 6 + 6 + 7 + 9 ==> 28

[Link](b);

class Decrement {

public static void main(String... args){

int a = 5;

[Link]( a -- ); // it will print 5 then it will become 4

[Link]( -- a); // it will become 3 then it will print 3

class PostPreDecrement {

public static void main(String... args){

int a = 5;

int b = a-- + --a + a--;


// int b = 5 + 3 + 3

[Link] ( b );

Relational Operators
-> Relations Operators are used to check relation between two Operands

> , < , >= , <=, !=, ==

Logical Operators
-> To check more than one condition then we will use Logical operators

AND ----> &&

OR -----> ||

NOT -----> !

Assignment Operator
-> Equals ( = ) is called as assignment operator

-> It is used to assign the value for a variable

int a = 10 ;

New operator
-> It is used to create the object for a class

ClassName refVar = new ClassName ( );

Note: Creating object means allocating memory in heap area


Dot (.) Operator
-> Dot operator is used to access class variables & methods

[Link] ( );

[Link]

[Link]

Ternary Operator / Conditional Operator


-> Ternary operator is used for decision making

Syntax:

( condition ) ? expression-1 : expression-2

-> If condition satisfied then expression-1 will execute otherwise expression-2 will
execute

Instanceof operator
- > It is used to check object reference belong to a class or not

Syntax:

String str = "ashokit";

if (str instanceof String ) {

//logic

}
Control Statements
-> Java program code will execute line by line sequentially (this is default behaviour)

-> In project code should execute based on user operation

-> To satisfy user requirement our code should execute based on some conditions

-> Using Control Statements we can control program execution flow

-> Control Statements are divided into 3 types

1) Decision Making Statements / Conditional Statements

2) Looping Statements

3) Transfer / Branching Statements

Conditional Statements
=> Execute the code only once based on condition

1) simple if

2) if - else

3) if - else - if - else -if - else (if else ladder)

4) switch

Looping Statements
=> To execute the code repeatedly

1) while loop

2) do-while loop

3) for loop

4) for-each loop
Branching / Transfer Statements
1) break;

2) continue;

3) return

Simple if
-> To execute the statements based on condition

Syntax:

if ( condition )

// stmt - 1

// stmt - 2

// stmt - 3

or

if (condition )

//stmt

class SimpleIf{

public static void main(String... args){

int a = 100;

int b = 20;

if( a > b ) {

[Link]("a is greater than b");

[Link]("Completed");
}

[Link]("Bye");

class IfElseDemo {

public static void main (String... args){

int age = 16 ;

if ( age >= 18 ) {

[Link]("Eligible For Vote") ;

} else {

[Link]("Not eligible for Vote");

Requirement :
int a = 20;

if a > 0 -----> display msg as 'a is positive number'

if a < 0 ----> display msg as 'a is negative number'

When above both conditions are failed then display msg as 'a is zero'

Syntax:

if ( condition_1 ) {

// stmt - 1

} else if ( condition_2 ) {
// stmt - 2

} else if ( condition_3 ) {

//stmt - 3

} else {

//stmt-4

-> if condition_1 is pass then it will execute only stmt-1

-> if condition_1 is fail then it will check condition_2

-> If condition_2 is pass then it will execute only stmt-2

-> If condition_2 is fail then it will check condition_3

-> If condition_3 is pass then it will execute only stmt-3

-> If condition_3 is fail then directley stmt-4 will be executed

class IfElseLadderDemo {

public static void main(String... args){

int a = 0;

if( a > 0) {

[Link](" a is positive number ");

} else if ( a < 0 ) {

[Link]("a is negative number");

} else {

[Link]("a is zero");

}
}

Assignment: Develop a java program to decide role of software engineer based on


his/her experience

0 - 2 year exp -----> Associate Engineer

3 - 5 years exp -----> Software Engineer

6 - 9 years exp -----> [Link] Engineer

10 - 13 years exp ----> Manager

class RoleFinder {

public static void main(String... args) {

int exp = 13;

if( exp >= 0 && exp <= 2 ){

[Link]("Associate Engineer");

}else if ( exp >= 3 && exp <=5 ){

[Link]("Software Engineer");

}else if( exp >= 6 && exp <=9 ){

[Link]("Sr. Software Engineer");

}else if( exp >= 10 && exp <=13 ){

[Link]("Manager");

}else {

[Link]("Role Not Found");

}
=> In above program we have hardcoded value for the variable

=> If we want to test our program with different values we need compile and execute
every time

=> To overcome this problem we can read the data from keyboard

How to read data from keyboard In Java


1) BufferedReader ( [Link] )

2) Scanner ( [Link] )

3) Command Line Arguments (input for main method)

-----------------------BufferedReader Program-------------------------
import [Link].*;

class RoleFinder {

public static void main(String... args) throws Exception {

InputStreamReader isr = new InputStreamReader([Link]);

BufferedReader br = new BufferedReader(isr);

String str = [Link] ( );

int exp = [Link](str);

if( exp >= 0 && exp <= 2 ){

[Link]("Associate Engineer");

}else if ( exp >= 3 && exp <=5 ){

[Link]("Software Engineer");

}else if( exp >= 6 && exp <=9 ){

[Link]("Sr. Software Engineer");


}else if( exp >= 10 && exp <=13 ){

[Link]("Manager");

}else {

[Link]("Role Not Found");

Requirement : Write a java program to find given number is odd or even

Note: Read number from keyboard

import [Link].*;

class OddOrEven {

public static void main(String... args) throws Exception {

InputStreamReader isr = new InputStreamReader([Link]);

BufferedReader br = new BufferedReader ( isr );

[Link]("Enter Number");

String str = [Link] ( );

int num = [Link] (str);

if( num % 2 == 0){

[Link]("It is even");

}else{

[Link]("It is odd");
}

Assignment -1 : Write a java program to check given number is a prime number or


not

Assignment -2 : Write a java program to check given year is a leap year or not

swtich case
-> Using switch case we can make decision

-> When we have upto 5 conditions test then if-else is recommended

-> When we have 10 or 20 conditions to test then switch is recommended

syntax

switch ( case ) {

case 1 : // stmt - 1

break;

case 2 : // stmt - 2

break;

case 3 : // stmt - 3

break;

default : // stmt - default

Requirement: Write a java program to read a number from keyboard.

Based on the given number print week of the day using 'switch' case

1 - Monday
2 - Tuesday

3 - Wednesday

4 - Thursday

5- Friday

6 - Saturday

7 - Sunday

>7 - No day found

import [Link].*;

class WeekDay {

public static void main(String... args) throws Exception {

InputStreamReader isr = new InputStreamReader([Link]);

BufferedReader br = new BufferedReader(isr);

[Link]("Enter number");

String str = [Link] ( );

int num = [Link](str);

switch ( num ) {

case 1 : [Link]("Monday");

break;

case 2 : [Link]("Tuesday");

break;
case 3 : [Link]("Wednesday");

break;

case 4 : [Link]("Thursday");

break;

case 5 : [Link]("Friday");

break;

case 6 : [Link]("Saturday");

break;

case 7 : [Link]("Sunday");

break;

default : [Link]("Day not found");

1) simple if

2) if - else

3) if - else if - else

4) switch

Conclusion
1) 'if' accepts only boolean value (or) boolean expression

2) 'switch' accepts numbers, char & strings (added in java 1.7v)


3) switch will not accept boolean and decimal values

4) switch cases should belongs to same type

5) switch case datatype and switch case input value should belongs to same datatype

6) 'default' case is optional in 'switch case'

7) 'break' keyword is also optional in 'switch case'

Loops in Java
-> Loops are used to execute statements repeatedly

-> In java we have below loops

1) while loop

2) do-while loop

3) for loop

4) for-each loop (arrays & collections)

While loop
-> While loop is used to execute statements until condition is true

-> while loop is called as conditional based loop

-> If condition is true then loop statements will execute otherwise loop will be
terminated

Syntax:

while ( condition ){

//stmts

}
Q) Write a java program to print numbers from 1 to 10 using while loop

class WhileDemo {

public static void main (String... args){

int i = 1;

while ( i <= 10 ){

[Link](i);

i++;

do-while loop
-> It is used to execute statements first then it will check the condition

-> do-while is also called as conditional based loop only

Syntax:
do{

//stmts

}while (condition );

Q) Write a java program to print numbers from 1 to 10 using do-while loop

class DoWhile{

public static void main(String... args){

int i = 1;
do {

[Link](i);

i++;

}while (i <= 10);

Q) What is the difference between while and do-while ?

while ==> It will check the condition first then it will execute the statements

do-while ==> It will execute statement first then it will check condition.

Note: Even if condition is not satisfied our statement will execute once.

for loop
-> It is used to execute statements multiple times

-> For loop is called as Range based loop

Syntax:
for ( initialization ; condition ; increment / decrement ) {

//stmts

Q) Write java program to print numbers from 1 to 10 using for loop

class ForLoop {

public static void main(String... args){

for ( int i = 1 ; i <= 10 ; i++ ) {

[Link](i);
}

Nested Loops
-> Writing one loop inside another loop is called as Nested loop

Syntax:

for ( int i = 1; i <= 5 ; i++ ){

for ( int j = 1; j< = 5; j++){

-> As per above program, for every execution of outer loop 5 times inner loop will
execute

Q) Write a java program to print below pattern using loops

**

***

****

*****

class NestedLoop {

public static void main(String... args){

for ( int i = 1; i <=5 ; i++ ){

for ( int j = 1; j <= i ; j++ ){

[Link]("*");

}
[Link]();

Q) Write a java program to print below pattern

12

123

1234

12345

class NestedLoop {

public static void main(String... args){

for ( int i = 1; i <=5 ; i++ ){

for ( int j = 1; j <= i ; j++ ){

[Link](j);

[Link]();

Branching Statements
 break ====> It is used to come out from switch case and from loops
 continue ====> It is used to skip one iteration in the loop execution then continue
 return ====> To come out from the method
class Break {

public static void main(String... args){

for (int i = 1; i<= 10; i++ ){

if (i >= 5 ){

break;

[Link](i);

class Continue {

public static void main(String... args){

for (int i = 1; i<= 10; i++ ){

if(i == 6 ) {

continue;

[Link](i);

}
Chapter-2 : Operators & Control Statements
Operators : To perform some operations

- Arithmetic Operators

- Relational Operators

- Logical Operators

- Ternary Operator

- Assignment Operator

- New Operator

- Dot operator

Conditional Statements : Execute the code only once based on condition

- simple if

- if - else

- if - else if - else ladder

- switch case

Loops Concept : Execute the code repeatedly based on condition / range

- while loop

- do-while loop

- for loop

- for each ( Arrays & Collections )


Transfer / Branching Statements : To come out from loop, to skip loop iteration, to
come out from method

- break

- continue

- return (used to return some value from the method)

Chapter-2 :: Logical Programs


Q-1) Write a java program to read shoes brand name from keyboard, based on brand
name print brand slogan like below

Nike -> Just do it

Adidas -> Impossible is nothing

Puma -> Forever Faster

Reebok -> I Am What I Am

import [Link].*;

class Shoes {

public static void main(String... args) throws Exception {

InputStreamReader isr = new InputStreamReader ([Link]);

BufferedReader br = new BufferedReader ( isr );

[Link] ("Enter Brand Name");

String brand = [Link] ( );

switch ( brand ) {

case "Nike" : [Link]("Just do it");

break;
case "Adidas" : [Link]("Impossible is nothing");

break;

case "Puma" : [Link]("Forever Faster");

break;

case "Reebok" : [Link]("I Am What I Am");

break;

default : [Link]("Brand Not Found");

Q-2) Write a java program to read person basic salary and calculate Provident Fund
amount from the basic salary

Formula : Provident Fund is 12 % of Basic Salary

import [Link].*;

class EmpPf {

public static void main(String... args) throws Exception {

InputStreamReader isr = new InputStreamReader( [Link] );

BufferedReader br = new BufferedReader (isr);

[Link]("Enter Basic Salary");

String str = [Link] ( );

double basicSalary = [Link] ( str );


double pf = basicSalary * 12 / 100;

[Link](pf);

Q-3) Write a java program to read person age and person salary and print his
eligibility for marriage

Condition : If person age less than 30 and salary greater than 1 lakh then eligible for
marriage

import [Link].*;

class Marriage {

public static void main (String... args) throws Exception {

InputStreamReader isr = new InputStreamReader( [Link] );

BufferedReader br = new BufferedReader(isr);

[Link]("Enter Your Age");

String str1 = [Link] ( );

int age = [Link] ( str1 );

[Link]("Enter Your Salary");

String str2 = [Link] ( );

double salary = [Link](str2);

if ( age < 30 && salary > 100000 ) {


[Link]("You are eligible for marriage");

} else {

[Link]("You are not eligible for marriage");

Q-5) Write a java program to print Right Triangle Star Pattern*

**

***

****

*****

class RightTriangle {

public static void main(String... args) {

for( int i = 1; i<=5 ; i ++ ){

for( int j = 1; j<=i ; j++ ){

[Link] ("* ");

[Link]();

}
Q-6) Write a java program to print left traingle start pattern*

**

***

****

*****

class LeftTriangle {

public static void main(String... args) {

for( int i = 1; i<=5 ; i ++ ){

for ( int k = 5-i ; k >= 1 ; k-- ){

[Link](" ");

for( int j = 1; j<=i ; j++ ){

[Link] ("*");

[Link]();

}
Q-7) Write a java program to print Pyramid pattern

**

***

****

*****

class Pyramid {

public static void main(String... args) {

for( int i = 1; i<=5 ; i ++ ){

for ( int k = 5-i ; k >= 1 ; k-- ){

[Link](" ");

for( int j = 1; j<=i ; j++ ){

[Link] ("* ");

[Link]();

You might also like