0% found this document useful (0 votes)
7 views4 pages

Lab05 TypeCastingIFElseSwitch

The document explains type casting in Java, distinguishing between implicit (widening) and explicit (narrowing) type casting with examples. It also covers decision statements such as if, if-else, else-if ladder, and switch statements, providing code examples for each. The examples illustrate how to control program flow based on conditions and user input.

Uploaded by

jainul.spec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Lab05 TypeCastingIFElseSwitch

The document explains type casting in Java, distinguishing between implicit (widening) and explicit (narrowing) type casting with examples. It also covers decision statements such as if, if-else, else-if ladder, and switch statements, providing code examples for each. The examples illustrate how to control program flow based on conditions and user input.

Uploaded by

jainul.spec
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit-1/05 Type Casting – if-else/switch

Type Casting in Java

Type casting is the process of converting one data type into another. It is categorized into:

1. Implicit Type Casting (Widening Conversion): Converting a smaller data type into
a larger one automatically. Example: int to double.
2. Explicit Type Casting (Narrowing Conversion): Converting a larger data type into
a smaller one manually using (dataType). Example: double to int.

Program for Type Casting in Java

class TypeCastingExample {
public static void main(String[] args) {
// Implicit Type Casting (Widening)
int num = 100;
double doubleNum = num; // int to double conversion

System.out.println("Implicit Type Casting:");


System.out.println("Integer value: " + num);
System.out.println("Converted to double: " +
doubleNum);

// Explicit Type Casting (Narrowing)


double pi = 3.14159;
int intPi = (int) pi; // double to int conversion

System.out.println("\nExplicit Type Casting:");


System.out.println("Double value: " + pi);
System.out.println("Converted to integer: " + intPi);
}
}

Output

Implicit Type Casting:


Integer value: 100
Converted to double: 100.0

Explicit Type Casting:


Double value: 3.14159
Converted to integer: 3

Part-05
Unit-1/05 Type Casting – if-else/switch

Decision Statements in Java

Decision statements control the flow of execution based on conditions.

1. if Statement: Executes a block of code if the condition is true.


2. if-else Statement: Executes one block if the condition is true, otherwise executes
another block.
3. else-if Ladder: Used when multiple conditions need to be checked sequentially.
4. switch Statement: Used when there are multiple possible values for a variable,
replacing multiple if-else statements.

if Statement Example

class IfExample {
public static void main(String[] args) {
int num = 10;
if (num > 0) {
System.out.println("The number is positive.");
}
}
}

Output

The number is positive.

if-else Statement Example

class IfElseExample {
public static void main(String[] args) {
int num = -5;
if (num > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is negative or zero.");
}
}
}

Output
The number is negative or zero.

Part-05
Unit-1/05 Type Casting – if-else/switch

else-if Ladder Example

class ElseIfExample {
public static void main(String[] args) {
int marks = 85;

if (marks >= 90) {


System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
}
}

Output
Grade: A

switch Statement Example

import java.util.Scanner;

class SwitchExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a day number (1-7): ");
int day = sc.nextInt();

switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");

Part-05
Unit-1/05 Type Casting – if-else/switch

break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day number.");
}
sc.close();
}
}

Example Input & Output:

Enter a day number (1-7): 3

Wednesday

**Run program again

Enter a day number (1-7): 8

Invalid day number.

Part-05

You might also like