if condition programs example in Java

if condition programs example in Java

In this article, we will discuss the if condition programs example in Java

How to write small programs using  if else statement in java



If condotion is a booleam expression can be use check condition in many programming languages for software development similar, C, C++, php etc… for write various type of  programs. we can write many java program using if condition in java.

This is one of the small program using if condition in java 

Example of  this program, one of the employee, if get basic salary above 30000.00 he get bonus 0.1% at the basic salary, if get under the 30000.00 he get bonus 0.05% at the basic salary   from the companyfind the basic salary bonus and Total salary

 program 1

This program Explain How to calculate bonus

class bonus{   //start of the class
public static void main(String args[]){ //start of the main method
double basicSal=22000.00;   //assign a variable to basis salary
//and initialization
double bonus=0.00;          //assign a variable to bonus
//and initialization
double netSal=0.00;         //assign a variable to net salary
//and initialization
if(basicSal>=30000.00){   //Test condition for check the salary above 30000.00
bonus=basicSal*0.1;   // if true this part executed

}else{
bonus=basicSal*0.05; // if false this part executed
}
netSal=bonus+basicSal; // calculate net salary
System.out.println(“Basic Salary:= “+basicSal); //display basic salary
System.out.println(“Bonus Salary:= “+bonus); //display bonus
System.out.println(“net  Salary:= “+netSal);  //display net salary
}
}

When the above code is executed.it produces te following result

program 2

In this tutorial, electricity bill calculator to a perticular customar in usage unit bases.At this simple program , the class contain two variable unit, payment then initialized value and electricity bill calculated by if condition in java

here is the rate

Sinse 0  to 90 unit  – 7.00
Sinse 90  to 150 unit – 12.00
Sinse 150  to 240 unit – 14.00
Sbove 240 unit- 16.00

class electrisity {
public static void main(String[] args) {
int unit=300;  //assign and initialized the variable unit
double payment=00.0; //assign and initialized the variable unit
if ((0<unit)&&(90>=unit))
{             // check the condition that unit from 0 to 90
payment=unit*7;   //calculate payment 0 – 90 unit 
System.out.println(payment);// display payment
}
else if ((90<unit)&&(150>=unit))
{    // check the condition that unit from 90 to 150
payment=90*7+(unit-90)*12;  // calculate payment
System.out.println(payment); // display payment
}
else if ((150<unit)&&(240>=unit))
{   // check the condition that unit from 150 to 240
payment=90*7+(150-90)*12+(unit-150)*14; // calculate payment
System.out.println(payment);  // display payment
} else if(unit>240)
{     //when become unit above 240
payment= 90*7+(150-90)*12+(unit-150)*14+(unit-240)*16;
//calculate payment
System.out.println(payment);  //display payment
}
else{ //when all are false
System.out.println(“no usage”);// display this massege
}
}
}

When the above code is executed.it produces te following result

Related post

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.