Chapter3 (Conditional Statements and Type Casting)

You might also like

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

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 Conditional Statements in java


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

D. Kuppusamy P
Simple if statement
• syntax :
if(boolean expression)
{
statement–block;
}
Other statements;
Simple if statement
/* This is an example of simple if 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");
}
}
}
if….else statement
• The is an extension of simple if statement
• syntax :
if (Boolean expression)
{
True -block statements;
}
else
{
False -block statements ;
}
Other statement;
if….else statements
/* Example of if else statement */
public class SampleTest
{
public static void main(String 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 ");
}
}
}
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 result = age>=40 ? ”eligible” : ”not eligible”;
Cascading (Nested) if….else
Syntax:
if (condition1)
{
statement - 1
}
.
.
.
else if(condition)
{
statement - n
}
else
{
default statement
}
other statement
Cascading if….else Example
public class CascasdeTest
{
public static void main(String 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.println("Spring");
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");
}
}
Switch Case
• Testing for multiple conditions

Syntax:
switch (expression)
{
case value-1:
case-1 block
break;
case value-2:
case-2 block
break;
default:
default block
break;
}
statement-x;
Switch Case
public class SwitchCaseTest
{
public static void main(String args[])
{
Scanner sin=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"); }
}
}
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
Quiz
class QuizExample {
public static void main(String s[]) {
if( 100 > 145 ) {
System.out.println(" 100 is greater than 145 ");
}
else
System.out.println(" 145 is greater than 100 ");
}
}
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
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
}
}
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 = sybmbol.

public class Typecast2


{
public static void main(String[] args)
{
double myDouble = 2.35
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 2.35
System.out.println(myInt); // Outputs 2
}
}
Narrowing or Explicit Type Casting (Manual)
public class Typecast3
{
public static void main(String[] args)
{
double a = 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

}
}
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);
}
}
Char to integer conversion
//incompatible data type
public class Typecast3
{
public static void main(String[] args)
{
char ch = “c”;
int num = 88;
ch = num;

System.out.println(num);
}
}
Type conversion
References

Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition,
2017.

You might also like