Java Practice
Java Practice
Block 1
els
Expression-2)
(Boolean
else if
Block 2
Expression-3)
else if (Boolean
Block3
Expression-n)
else if (Boolean
Block n
else
forthe else if
with Boolean Expression-3 and it F
executed. Ifit fails, the program checks else
if statement.The
final 1S Ooptional
les
till the last else
statemë
3 is executed. The
checking continues The nested if-else se
not met by the earlier alternatives. classifica
will take care of conditions marks
províded ít students
the entire structure. Program 3.4illustrates
will execute only one block of st
th
Output 3.4
Program 3.4
Run-1 Name
inport java.util.Scanner; Enter Student
publiC class NestedIf ( Daniel Marks
them asdistinction,
the marks of the student and classifying
=Daniel 1
Name
Student
and unsuccessful.
(
=92
first, second, third class Marks
Student
=DISTINCTI
main(String[] args) Secured
publiC static void
String name;
nt narki Run-2 Name
inport java.util.scanner;
The program reads name, experience and salary of the employee using scannerclass. If tests whether the
experience i greater than 15 and the salary is greaterthan Rs. 8000.If so,the salary
is increased by Rs. 1500.
Ifthe first conditional statement the control flows
fails to the second ifstatement which checks whether it is
less than 15 and the salary is greater than Rs. 6000 and if so the salary is
improved by Rs. 1000. The output
of Program 3.5 is the same as Program 3.3. So far these sections have
compared numerical values with
various formats of if statements. The next section checks
whether the String objects are equal or not.
Program 3.6
import java.util.Scanner;
Output 3.6
public class Equals{
Enter First String
The program demonstrates the use of equals () Raguram
*
methods
*by comparing two strings Enter Second string
RAGURAM
publiC Enter Third String
static void main (String U args)(
String s1, s2, s3; Raguram
First String =Raguram
Scanner inp= new Scanner(System in) Second Strìng -RAGURAM
Third String -Raguram
Systet,out.println("EnterFirst string "):
Raguram and RAGURAM are
S1 inp,nextiine();
System,out.printn("Enter Second String") EQUAL= false
s2 inp nextLine() Raguram and Raguram are
System, outprintln(Enter Third String,") EQUALtrue
s3 inp.nextLine () i
RAGURAM and Raguram are
EQUALafalse
System.out.printin("PírstString s1)
System.out.println("SecondString 2)
52 Java Programming
Program 3.7
The program received three stringsand to prove the equalityof strings the string s1 is compared with
string s1 and they are equal so the result is equal to 0 and the message
displayed is EQUAL. The string
s2 iS compared with string s3; since they are different, the message displayed is NOT EQUAL. To display
thecompared results the strings s1 and s1 are compared and the outcome is 0. The strings s1 and s3
are compared in the alphabeticalorder. The distance betweenjava and fortran is 4 which is greater than
zero.The stringss2 and s3 are compared in the alphabeticalorder and the distance between fortranand
pascal is -10, which is less than zero. The conditional statements covered so far have another version
if
the true part orthe false part has only one statement, as explained in the following section.
It can assign value to a right hand side variable based on an expression'sBoolean value. The general
format of the ternary operator is
f the test condition is true, assign the value of value1 to result; otherwise, assign the value of value2
to result. In Program 3.3, if the salary of an employee is greaterthan Rs.8000, his increment Rs.1500
else Rs.1000 if theexperience is ignored. This can be expressed as
increraent = salary>8000 ? 1500 1000;
salary #salary +increment;
Ifthe salary ís greater than Rs. 8000 the outcome of the Boolean expression is true and Rs.I 500 is
assigned to the variable increment;otherwise Rs. 1000 is assigned. The example has used the Boolean
expression in the conditional operator. Boolean state can also be used in place of the Boolean expres
Sion. Apartfrom using a ternary operator to assign values to a variable, it can be used as follows
Ifmarki is greater than or equal to 40 "Pass" is assigned to string s;otherwise "Fail'" is assigned. The
same example can be used inside a display statement also.
The line of code will display either Pass or Failbased on the value of markl. A better way to handle
multiple if-else statements is provided in the next section.
Program 3.8
import java.util.Scanner
case D':
System.out.println("DISTINCTION
break;
):
caseF
Output 3.8
System.out.println("FIRST ): Run-1
break;
Enter Student Name
case S: S Maheswari
Enter a character:
Systen,out.println ("SECOND ") D
break; STUDENT NAME Maheswari
GRADE DISTINCTION
Case T
Run-2
System,out.println (
"THIRD "): Enter Student Name
break;
Ruban
Enter a character:
default :
Systen.out.println(FAIL
break;
) X
STUDENT NAME
GRADE
Ruban
=FAIL