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

Computers - Java

The document provides an overview of the switch-case statement in Java, including its syntax, advantages, and disadvantages. It also presents various programming examples that demonstrate how to use switch statements to determine if numbers are buzz numbers, Armstrong numbers, palindromic numbers, or automorphic numbers. Additionally, it compares switch-case statements with if-else statements, highlighting their similarities and differences.

Uploaded by

hiteshgowdark
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 views11 pages

Computers - Java

The document provides an overview of the switch-case statement in Java, including its syntax, advantages, and disadvantages. It also presents various programming examples that demonstrate how to use switch statements to determine if numbers are buzz numbers, Armstrong numbers, palindromic numbers, or automorphic numbers. Additionally, it compares switch-case statements with if-else statements, highlighting their similarities and differences.

Uploaded by

hiteshgowdark
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

switch()

Statement - 2

CONDITIONAL CONSTRUCT IN JAVA


[ chapter 9 ]
AGENDA

 Recap on switch .. case statement


 Programs on switch.. Case
 Introduction to nested switch
 Syntax of nested switch
 Example on nested switch statement
 Similarities in if vs switch statement
 Advantages and disadvantages of switch ..case statement
 Differentiate between if..else and switch .. case statement
Write a program to accept an integer value. Depending on the user's choice print whether
it is a buzz number or not OR an even number or not. Display all the details.

switch(ch1)
import [Link].*; { case 1: if ( n% 7 ==0 || n%10 == 7 )
class BuzzNo_EvenNo [Link](n + " is a Buzz number ");
{ public static void main() else
{ Scanner sc= new Scanner([Link]); [Link](n + " is not a Buzz number ");
[Link](" \t\tMenu "); break;
[Link] ("1. Buzz number "); case 2: if ( n% 2 ==0 )
[Link] ("2. Even number "); [Link](n + " is an even number ");
[Link] ("Enter your choice 1/2 : "); else
int ch1 = [Link](); [Link](n + " is not an even number");
[Link] ("Enter a number :"); break;
int n = [Link](); default: [Link] ("Invalid choice");
[Link] ("Given number :"+ n); }
} }
3
PROGRAM

Write a menu driven class to accept a 3 digit number from the user
and check whether it is a buzz number or not OR an Armstrong
number or not.
(a) Buzz number - (a number which is divisible by 7 or a number
which ends with 7)
Example1 : 107 is a Buzz number
Example2 : 70 is a buzz number

(b) Armstrong number : Sum of the cube of its digits is equal to the
number.
Example 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
153 is an Armstrong number.

4
import [Link].*;
class Buzz_Automor_No
case 2: int u = n%10;
{ public static void main()
int h = n/100;
{ Scanner sc= new Scanner([Link]);
int t = n%100/10;
[Link]("Enter a 3 digit number :");
int n = [Link]();
if ( u*u*u + t*t*t + h*h*h ==n )
if (n>99 && n<1000)
[Link](n + " is an Armstrong number ");
{ [Link](" \t\tMenu ");
else
[Link]("1. Buzz number ");
[Link](n + " is not an Armstrong number");
[Link]("2. Armstrong number ");
break;
[Link]("Enter your choice 1/2 : ");
default: [Link] ("Invalid choice");
int ch1 = [Link]();
}
switch(ch1)
}
{ case 1:
else
if ( n% 7 ==0 || n%10 == 7 )
[Link](n + " is not a 3 digit number ");
[Link](n + " is a Buzz number ");
}
else
}
[Link](n + " is not a Buzz number ");
break;
5
PROGRAM

Write a menu driven class to accept a double digit number from the
user and check whether it is a palindromic number or not OR an
Automorphic number or not.
(a) Palindromic number - (a number a Palindrome which when read in
reverse order is same as read in the right order)
Example : 11, 88 etc.
(b) Automorphic number : An automorphic number is the
number which is contained in the last digit(s) of its
square.
Example 25 is an automorphic number as its
square is 625 and 25 is present as the last two digits.

6
import [Link].*; if ( u==t) //or (n==rev)
class Palin_Automorphic_No [Link](n + " is a Palindromic number ");
{ public static void main() else
{ Scanner sc= new Scanner([Link]); [Link](n + " is not a Palindromic number " );
[Link]("Enter a double digit number break;
:"); case 2: int sq= n*n;
int n = [Link](); if ( sq % 100 == n)
if (n>9 && n<100) [Link](n + " is an Automorphic number ");
{ [Link](" \t\tMenu "); else
[Link]("1. Palindromic number "); [Link](n + " is not an Automorphic
[Link]("2. Automorphic number "); number");
[Link]("Enter your choice 1/2 : "); break;
int ch1 = [Link](); default: [Link] ("Invalid choice");
int u = n%10; }
int t = n/10; }
rev= u*10+t else
switch(ch1) [Link](n + " is not a double digit number ");
{ case 1: }
} 7
Comparison of if vs switch statement

Similarity

Both are decision making statements


Implement selection program control
Used to select one of the given choices by testing an
expression

8
Advantages & Disadvantages of switch statement

Advantages
faster than if
makes the program compact

Disadvantages
cannot compare ranges
cannot compare multiple values
cannot work with all data types

9
Differences between
Differentiate between ifif and switch statement
and switch statement

switch … case
switch … case
1. Tests
1. Tests against
againstaaset
setofof constants
constants
If … else
2. Tests
Tests only
onlyfor
forequality
equality
1. Can test against a set of constants as well
2. as variables.
3. Cannot
3. Cannotcompare
comparefor for a range
a range of values
of values
2. Can checks for all relational and logical
4. Can
4. Can handle
handleonly
onlyint,
int, byte,
byte, short
short ,char,char
and operations
and String
String type
type of dataof data
3. Can compare for a range of value
5.
5. Enforces branching
Enforces branching onon the
the basis
basis of
of test
test
4. Can operate on all data types
resultof
result ofonly
onlyone
one variable
variable
5. Permits complex, multiple expression
involving multiple variable.

10
THANK
YOU

You might also like