Java Decision Making

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Core Java Tutorial

mail us:- abrar.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

Java Decisions Making

We all need to alter our actions in the face of changing circumstances.


If the weather is fine, then I will go for a stroll.
If the highway is busy I would take a diversion.
If the pitch takes spin, we would win the match.
If she says no, I would look elsewhere.
If you like this tutorial, I would write the next tutorial. You can notice that all these
decisions depend on some condition being met. There are following decision
making statement in java.
The if statements
The if-else statements
The conditional operators ( ? :)
The switch statement

abrar.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

The if Statement

An if statement consists of a Boolean expression followed by one or more


statements.
Syntax:
The syntax of an if statement is:
if(Boolean expression)
{
statement(s);
}
If the boolean expression evaluates to true then the block of code inside the if
statement will be executed.
If not the first set of code after the end of the if statement(after the closing curly
brace) will be executed.

abrar.alam38@gmail.com

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.alam38@gmail.com

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

abrar.alam38@gmail.com

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

abrar.alam38@gmail.com

Output
Bonus= 25000

Abrar Alam
B-TECH
KIIT UNIVERSITY

The if else statements

An if statement can be followed by an optional else statement, which executes when


the Boolean expression is false.
Syntax:

The syntax of a if...else is:

if(Boolean_expression)
{
//Executes when the Boolean expression is true
}
else
{
//Executes when the Boolean expression is false
}

abrar.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

The if else statements


Copy the following code in your text editor and save
as Testifelse.java
class Testifelse
{
public static void main(String args[])
{
int x = 30;
if( x < 20 )
{
System.out.print("This is if statement");
}
else
{
System.out.print("This is else statement");
}
}
}

abrar.alam38@gmail.com

Output
This is else statement

Abrar Alam
B-TECH
KIIT UNIVERSITY

The Nested if else statements

An if statement can be followed by an optional else if...else statement


which is very usefull to test various conditions using single if...else if statement.
When using if , else if , else statements there are few points to keep in mind.
An if can have zero or one else's and it must come after any else if's.
An if can have zero to many else if's and they must come before the else.
Once an else if succeeds, none of he remaining else if's or else's will be tested.

abrar.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

The Nested if else statements


Syntax:
if(Boolean_expression 1)
{
statement(s); //Executes when the Boolean expression 1 is true
}
else if(Boolean_expression 2)
{
statement(s); //Executes when the Boolean expression 2 is true
}
else if(Boolean_expression 3)
{
statement(s); //Executes when the Boolean expression 3 is true
}
else
{
statement(s); //Executes when the none of the above condition is true.
}

abrar.alam38@gmail.com

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.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

The switch Statement:

A switch statement allows a variable to be tested for equality against a list of


values. Each value is called a case, and the variable being switched on is checked
for each case.
Syntax:
switch(expression)
{
case value :
Statement(s);
break; //optional
case value :
Statement(s);
break; //optional
default : //Optional
Statement(s);
}

abrar.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

The switch Statement

The following rules apply to a switch statement:


The variable used in a switch statement can only be a byte, short, int, or char.
You can have any number of case statements within a switch. Each case is followed
by the value to be compared to and a colon.
The value for a case must be the same data type as the variable in the switch, and
it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following
that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control
will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the
end of the switch. The default case can be used for performing a task when none
of the cases is true. No break is needed in the default case.

abrar.alam38@gmail.com

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

abrar.alam38@gmail.com

Output
input A
Your grade is Excellent!

Abrar Alam
B-TECH
KIIT UNIVERSITY

Conditional Operator ( ? : )

Conditional operator is also known as the ternary operator.


This operator consists of three operands and is used to evaluate boolean
expressions.
The goal of the operator is to decide which value should be assigned to the
variable.
The operator is written as :
variable x = (expression) ? value if true : value if false

abrar.alam38@gmail.com

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.alam38@gmail.com

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.
abrar.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

Thank you!

mail us:- abrar.alam38@gmail.com

Abrar Alam
B-TECH
KIIT UNIVERSITY

You might also like