0% found this document useful (0 votes)
94 views3 pages

Practical No. 04

The document contains examples of using switch-case statements and ternary operators in Java programs. The first example uses a switch-case statement to print different case statements based on the value of an expression variable. The second example uses a ternary operator to find the maximum of two numbers. The exercises demonstrate additional switch-case statements, with the second checking if a character is a vowel or consonant.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views3 pages

Practical No. 04

The document contains examples of using switch-case statements and ternary operators in Java programs. The first example uses a switch-case statement to print different case statements based on the value of an expression variable. The second example uses a ternary operator to find the maximum of two numbers. The exercises demonstrate additional switch-case statements, with the second checking if a character is a vowel or consonant.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No.

4
Program: Using switch-case statement
class Main{

public static void main(String args[]){

int expression = 2;

switch (expression){

System.out.println("Case 1");

case 2:

System.out.println("Case 2");

case 3:

System.out.println("Case 3");

default:

System.out.println("Default Case");

}}}

Write a program to make use of Ternary operators


class Ternary {

public static void main(String args[]){

int n1 = 5, n2 = 10, max;

System.out.println("1st Number: "+n1);

System.out.println("2nd Number: "+n2);

max = (n1 > n2) ? n1 : n2;


System.out.println("Maximum is= "+max);

}}

Exercise:

1)
public class SwitchCaseExample{

public static void main(String args[]){

int num = 2;

switch(num + 2){

case 1 :

System.out.println("Case 1: Value is:"+num);

case 2:

System.out.println("Case 2: Value is:"+num);

case 3:

System.out.println("Case 3: Value is:"+num);

default:

System.out.println("Default Value is:"+num);

}}}

2)
public class SwitchVowelExample{

public static void main(String args[]){


char ch = 'o';

switch(ch){

case 'a':

System.out.println("Vowel");

break;

case 'e':

System.out.println("Vowel");

break;

case 'i':

System.out.println("Vowel");

break;

case 'o':

System.out.println("Vowel");

break;

case 'u':

System.out.println("Vowel");

break;

default:

System.out.println("Consonant");

}}}

You might also like