Java Decision Making
Java Decision Making
Abrar Alam
B-TECH
KIIT UNIVERSITY
Abrar Alam
B-TECH
KIIT UNIVERSITY
The if Statement
Abrar Alam
B-TECH
KIIT UNIVERSITY
Example
Copy the following code in your text editor and
save as TestSimpleif.java
Output
This is if statement
class TestSimpleif
{
public static void main(String args[])
{
int x = 10;
if( x < 20 )
{
System.out.print("This is if statement");
}
}
}
Abrar Alam
B-TECH
KIIT UNIVERSITY
Example 2
Copy the following code in your text editor and
save as TestSimple.java
class TestSimpleif
{
/* Calculation of total expenses */
public static void main(String args[] )
{
int qty=2000, dis = 0 ; float rate=10.5, tot ;
if ( qty > 1000 )
dis = 10;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
System.out.print(total = +tot);
}
}
Output
total= 18900.0
Abrar Alam
B-TECH
KIIT UNIVERSITY
Example 3
Copy the following code in your text editor and
save as TestSimpleif.java
class TestSimpleif
{
/* Calculation of bonus */
public static void main( String args[])
{
Int bonus, cy=2012, yoj=2005, yr_of_ser ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{
bonus = 2500 ;
System.out.print(Bonus = +bonus);
}
}
}
Output
Bonus= 25000
Abrar Alam
B-TECH
KIIT UNIVERSITY
if(Boolean_expression)
{
//Executes when the Boolean expression is true
}
else
{
//Executes when the Boolean expression is false
}
Abrar Alam
B-TECH
KIIT UNIVERSITY
Output
This is else statement
Abrar Alam
B-TECH
KIIT UNIVERSITY
Abrar Alam
B-TECH
KIIT UNIVERSITY
Abrar Alam
B-TECH
KIIT UNIVERSITY
Example
Copy the following code in your text editor and save as
Testnestedif.java
class Testnestedif
{
public static void main(String args[])
{
int x = 30;
if( x == 10 )
{
System.out.print("Value of X is 10");
}
else if( x == 20 )
{
System.out.print("Value of X is 20");
}
else if( x == 30 )
{
System.out.print("Value of X is 30");
}
else
{
System.out.print("This is else statement");
}
}
}
Output
value of x is 30
Abrar Alam
B-TECH
KIIT UNIVERSITY
Abrar Alam
B-TECH
KIIT UNIVERSITY
Abrar Alam
B-TECH
KIIT UNIVERSITY
Example
Copy the following code in your text editor and save as TestSwitch.java
class TestSwitch
{
public static void main(String args[])
{
char grade = args[0].charAt(0);
switch(grade)
{
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
Output
input A
Your grade is Excellent!
Abrar Alam
B-TECH
KIIT UNIVERSITY
Conditional Operator ( ? : )
Abrar Alam
B-TECH
KIIT UNIVERSITY
Example
Copy the following code in your text editor and
save as Conditionaloptr.java
class Conditionaloptr
{
public static void main(String args[])
{
int a , b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
b = (a == 10) ? 20: 30;
System.out.println( "Value of b is : " + b );
}
}
Output
Value of b is : 30
Value of b is : 20
Abrar Alam
B-TECH
KIIT UNIVERSITY
More Example
A library charges a fine for every book returned late. For first 5 days the fine is 50 paise,
for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the
book after 30 days your membership will be cancelled. W.A.P to accept the number of
days the member is late to return the book and display the fine or the appropriate
message
A five-digit number is entered through the keyboard. Write a program to obtain the
reversed number and to determine whether the original and reversed numbers are
equal or not.
If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to
determine the youngest of the three.
A university has the following rules for a student to qualify for a degree with A as the
main subject and B as the subsidiary subject:
(a) He should get 55 percent or more in A and 45 percent or more in B.
(b) If he gets than 55 percent in A he should get 55 percent or more in B. However, he
should get at least 45 percent in A.
(c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to
reappear in an examination in B to qualify.
(d) In all other cases he is declared to have failed.
Write a program to receive marks in A and B and Output whether the student has
passed, failed or is allowed to reappear in B.
Solutions for these all programs and Learn more contact us or mail us.
[email protected]
Abrar Alam
B-TECH
KIIT UNIVERSITY
Thank you!
Abrar Alam
B-TECH
KIIT UNIVERSITY