Java Se (Core Java) : Lecture-8
Java Se (Core Java) : Lecture-8
(CORE JAVA)
LECTURE-8
Today’s Agenda
If , if-else , nested if
switch
Ternary Operator
Decision Control Statement
Syntax :-
false
if(test_Condition)
{ true
-----
-----
}
false
if(test_Condition)
{
true
-----
-----
}
else
{
----
----
}
Every else statement should have one if
statement.
if else if
false
if(test_Condition)
{
true
-----
-----
}
else if( test_Condition)
{
----
true
----
}
else
{
----
}
Nested if
Any conditional statement within the other conditional statement makes it nested
in nature.
if(test condition)
{
if(test condition)
{
----------
----------
}
else
{
---------
---------
}
}
Try this…
11,12,1,2 Winter
3,4,5,6 Summer
7,8,9,10 Rainy
class SwitchEx1
{
public static void main(String [ ] args)
{
switch(args[0])
{
case "11": case "12": case "1": case "2": System.out.println("Winter season");
break;
case "3": case "4": case "5": case "6": System.out.println("Summer Season");
break;
case "7": case "8": case "9": case "10": System.out.println("Rainy Season");
break;
default: System.out.println("Wrong input");
}
}
}
Exercise
WAP which should accept 3 arguments via
command line of type operand, operator and
operand and should display the result by
performing appropriate calculation. Assume
operator would be either + or - ?
Sample Run:
Ternary Operator