You are on page 1of 20

Conditional Statements

&
Type Conversion in JAVA
Conditional Statements

• Conditional Statements executes one or set of statements based on a condition


exactly once.

• There are four types of Conditionals statements in java

• Simple if
• else..if
• Nested else…if
• Switch case statements

2
Simple if statement

if(Boolean expression)

statement-block;

Other statements;
Simple if statement
/* This is an example of simple statement */
Public class SampleTest
{
public static void main(String args[])
{
int a = 4;
int b = 20;

if( a < b)
{
System.out.println(“This is if statement”);
}
}
} 4
if….else statement
• The is an extension of simple if statement
• syntax :
if (Boolean expression)
{
True-block statements;
}
else
{
False – block statements;
}
5
Other statement;
if….else statement
/*Example of if else statement */
public class Sample Test
{
public static void main(Strings args[])
{
Scanner sin=new Scanner(System.in);
int age = sin.nextInt();
if(age>40)
{
System.out.println(“Eligible to Covid Vaccinate”)
}
else
{
System.out.println(“ Not Eligible to Covid Vaccinate “);
}
}
6
}
Conditional Operator
• Conditional operator is an one line alternative for if else condition.

• The result of conditional statements can be stored in to a variable

• syntax :
condition? true statements : false statements:

Example:
String results =age>40 ? “eligible” : “not eligible”;
7
Cascading (Nested) if….else
Syntax:
if (condition1)
{
statement – 1
}
.
.
.
else if(condition)
{
statement – n
}
default statement
}
other statement 8
Cascading if….else Example
public class CascadeTest
{
public static void main(Strings args[])
{
Scanner sin=new Scanner(System.in);
int month = sin.nextInt();
if(month == 12 || month == 1 || month ==2)
System.out.println(“Winter”);
else if(month == 3|| month == 4 || month == 5)
System.out.printls(“Springs”);
else if(month == 6 || month == 7 || month == 8)
System.out.println(“Summer”)
else if(month == 9 || month == 10 || month == 11)
System.out.println(“Autumn”);
else
System.out.println(“invalid month”);
}
9
}
Switch Case

• Testing for multiple conditions


Syntax:
Switch (expression)
{
case value-1:
case- 1 block
break;
case values- 2:
break;
default:
default block
break;
}
statement-x;
10
Switch Case public class SwitchCaseTest
{
public static=new Scanner(System.in);
int weekday = sin.nextInt();
switch(weekday) {
case 1: System.out.println(“Sunday”);
break;
case 2: System.out.println(“Monday”);
break;
case 3: System.out.println(“Tuesday”);
break;
case 4: System.out.println(“Wednesday”);
break;
case 5: System.out.println(“Thursday”);
break;
case 6: System.out.println(“Friday”);
break;
case 7: System.out.println(“Saturday”);
break;
default:
System.out.println(“Invalid day”); }
} 11
}
Break statement

• The break statement will terminate the iteration or switch case block during the
execution of program,
• When a break statement is encountered in a loop, the loop exit and the program
continues with the statements immediately following the loop
• When the loops are nested, the break will only terminate the corresponding loop
body

12
Quiz

class QuizExample {
Public static void main(Strings s[] {
If( 100> 145 ) {
System.out.println(“ 100 is greater than 145 “);
}
Else
System.out.println(“ 145 is greater than 100 “);
}
}

13
Type Casting in JAVA

• Type casting is converting a value of one primitive data type to another type during
any operation.
• Two types of casting:
• Widening Casting (automatic) – converting a smaller size data type to a larger size
data type
byte->short->char->int->long->float->double
• Narrowing Casting (manual) – converting a larger size data type to a smaller size data
type.
double->float->long->int->char->short->byte
14
Widening Type Casting (automatic)
Public class Typecast1
{
public static void main(String[] args)
{
int myInt = 12;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 12
System.out.println(myDouble); // Outputs 12.0
}
} 15
Narrowing or Explicit Type Casting (manual)
• Assigns a value of larger data type to a smaller data type.
• Useful for incompatible data types where automatic conversion cannot be done.
• Target data type have to be represented in ( ) next to the = symbol.

public class Typecast2


{
public static void main(Strings[] args)
{
double myDouble = 2.35
int myInt = (int) myDouble; // Manual casting: doubt to int
System.out.println(myDouble); // Outputs 2.35
System.out.println(myInt); // Outputs 2
}
16
}
Narrowing or Explicit Type Casting (manual)
public class Typecast3
{
public static void main(Strings[] args)
{
double myDouble = 1232.35
long k= (long) a; // Manual casting
int j = (int) k;
System.out.println(a); // Outputs 1232.35
System.out.println(k); // Outputs 1232
System.out.println(j); // Outputs 1232
}
17
}
String to integer
//incompatible data type for explicit type conversion
public class Typecast3
{
public static void main(String[] args)
{
String price=“34”;
int num = Integer.parseInt(price);
System.out.println(num);
}
}
18
Char to integer conversion
//incompatible data
public class Typecast3
{
public static void main(String[] args)
{
char ch=“c”;
int num = 88;
System.out.println(num);
}
}
19
Type conversion

20

You might also like