SlideShare a Scribd company logo
1 of 15
CSE110
Principles of Programming
with Java
Lecture 10:
switch Statement and Operator ?
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
switch Statement
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
The switch Statement
• The switch statement provides another means to
decide which statement to execute next
• The switch statement evaluates an expression, then
attempts to match the result to one of several
possible cases
• Each case contains a value and a list of statements
• The flow of control transfers to statement associated
with the first value that matches
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
The switch Statement
• The general syntax of a switch statement is
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5
The switch Statement
• Often a break statement is used as the last
statement in each case's statement list
• A break statement causes control to transfer to the
end of the switch statement
• If a break statement is not used, the flow of control
will continue into the next case
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6
The switch Statement
• A switch statement can have an optional default case
• The default case has no associated value and simply
uses the reserved word default
• If the default case is present, control will transfer to it if no
other case value matches
• Though the default case can be positioned anywhere in
the switch, usually it is placed at the end
• If there is no default case, and no other value matches,
control falls through to the statement after the switch
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
The switch Statement
• The expression of a switch statement must result in
an integral type, meaning an int or a char
• It cannot be a boolean value, a floating point value
(float or double)
• A switch statement tries to match the expression
with a value
• You cannot perform relational checks with a switch
statement
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
Example 1
String s = JOptionPane.showInputDialog(“Write a number”):
int option = Integer.parseInt(s);
switch (option) {
case 1: System.out.println(“Phoenix”); break;
case 2: System.out.println(“NY”); break;
case 3: System.out.println(“Seattle”); break;
default: System.out.println(“Not available”);
}
System.out.println(“Bye.”);
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9
Example 2
String s = JOptionPane.showInputDialog(“Write a letter”):
int letter = s.charAt(0);
switch (letter) {
case 'A':
System.out.println("It is an A");
break;
case 'B':
System.out.println("It is a B");
break;
case 'C':
System.out.println("It is a C");
break;
default:
System.out.println("Invalid option");
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10
Example 1
String s = JOptionPane.showInputDialog(“Write your grade”);
int grade = Integer.parseInt(s);
switch (grade) {
case 100: System.out.println(“A+”); break;
case 95:
case 90: System.out.println(“A”); break;
case 85:
case 80: System.out.println(“B”); break;
default: System.out.println(“C”);
}
System.out.println(“Bye.”);
// What is the result for grade = 97 ?
The operator ?
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12
The Conditional Operator
• Java has a conditional operator that evaluates a
boolean condition that determines which of two
other expressions is evaluated
• The result of the chosen expression is the result of
the entire conditional operator
• Its syntax is:
condition ? expression1 : expression2
• If the condition is true, expression1 is evaluated; if it
is false, expression2 is evaluated
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13
The Conditional Operator
• The conditional operator is similar to an if-else
statement, except that it forms an expression that
returns a value
• For example:
larger = ((num1 > num2) ? num1 : num2);
• If num1 is greater that num2, then num1 is assigned
to larger; otherwise,num2 is assigned to larger
• The conditional operator is ternary because it
requires three operands
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
Example
System.out.println ("Your change is "
+ count
+ ((count == 1) ? "Dime" : "Dimes"));
• If count equals 1, then "Dime" is printed
• If count is anything other than 1, then "Dimes" is
printed
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
Javascript conditional statements 2
Javascript conditional statements 2Javascript conditional statements 2
Javascript conditional statements 2Jesus Obenita Jr.
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Computer Programming - if Statements & Relational Operators
Computer Programming - if Statements  & Relational OperatorsComputer Programming - if Statements  & Relational Operators
Computer Programming - if Statements & Relational OperatorsJohn Paul Espino
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsTech
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Python Programming - Control Flow
Python Programming - Control Flow Python Programming - Control Flow
Python Programming - Control Flow Omid AmirGhiasvand
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statementsrajshreemuthiah
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlanDeepak Lakhlan
 

What's hot (19)

Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
Branching in C
Branching in CBranching in C
Branching in C
 
Javascript conditional statements 2
Javascript conditional statements 2Javascript conditional statements 2
Javascript conditional statements 2
 
Control statement
Control statementControl statement
Control statement
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Computer Programming - if Statements & Relational Operators
Computer Programming - if Statements  & Relational OperatorsComputer Programming - if Statements  & Relational Operators
Computer Programming - if Statements & Relational Operators
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Python Programming - Control Flow
Python Programming - Control Flow Python Programming - Control Flow
Python Programming - Control Flow
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
Chap 6(decision making-looping)
Chap 6(decision making-looping)Chap 6(decision making-looping)
Chap 6(decision making-looping)
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
Looping Structures
Looping StructuresLooping Structures
Looping Structures
 

Similar to 201707 CSE110 Lecture 10

Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statementsmaznabili
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & ElseAbdullah Bhojani
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Neeru Mittal
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)BoneyGawande
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.pptTekle12
 

Similar to 201707 CSE110 Lecture 10 (20)

201707 CSE110 Lecture 07
201707 CSE110 Lecture 07  201707 CSE110 Lecture 07
201707 CSE110 Lecture 07
 
201707 CSE110 Lecture 12
201707 CSE110 Lecture 12  201707 CSE110 Lecture 12
201707 CSE110 Lecture 12
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
201707 CSE110 Lecture 08
201707 CSE110 Lecture 08  201707 CSE110 Lecture 08
201707 CSE110 Lecture 08
 
Control structure
Control structureControl structure
Control structure
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
201707 CSE110 Lecture 04
201707 CSE110 Lecture 04   201707 CSE110 Lecture 04
201707 CSE110 Lecture 04
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
CPP04 - Selection
CPP04 - SelectionCPP04 - Selection
CPP04 - Selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Java script – basic auroskills (2)
Java script – basic   auroskills (2)Java script – basic   auroskills (2)
Java script – basic auroskills (2)
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
 
Introducing Swift v2.1
Introducing Swift v2.1Introducing Swift v2.1
Introducing Swift v2.1
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 

Recently uploaded (20)

Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 

201707 CSE110 Lecture 10

  • 1. CSE110 Principles of Programming with Java Lecture 10: switch Statement and Operator ? Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 The switch Statement • The switch statement provides another means to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first value that matches
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4 The switch Statement • The general syntax of a switch statement is
  • 5. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5 The switch Statement • Often a break statement is used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case
  • 6. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 6 The switch Statement • A switch statement can have an optional default case • The default case has no associated value and simply uses the reserved word default • If the default case is present, control will transfer to it if no other case value matches • Though the default case can be positioned anywhere in the switch, usually it is placed at the end • If there is no default case, and no other value matches, control falls through to the statement after the switch
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 The switch Statement • The expression of a switch statement must result in an integral type, meaning an int or a char • It cannot be a boolean value, a floating point value (float or double) • A switch statement tries to match the expression with a value • You cannot perform relational checks with a switch statement
  • 8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8 Example 1 String s = JOptionPane.showInputDialog(“Write a number”): int option = Integer.parseInt(s); switch (option) { case 1: System.out.println(“Phoenix”); break; case 2: System.out.println(“NY”); break; case 3: System.out.println(“Seattle”); break; default: System.out.println(“Not available”); } System.out.println(“Bye.”);
  • 9. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 9 Example 2 String s = JOptionPane.showInputDialog(“Write a letter”): int letter = s.charAt(0); switch (letter) { case 'A': System.out.println("It is an A"); break; case 'B': System.out.println("It is a B"); break; case 'C': System.out.println("It is a C"); break; default: System.out.println("Invalid option"); }
  • 10. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 10 Example 1 String s = JOptionPane.showInputDialog(“Write your grade”); int grade = Integer.parseInt(s); switch (grade) { case 100: System.out.println(“A+”); break; case 95: case 90: System.out.println(“A”); break; case 85: case 80: System.out.println(“B”); break; default: System.out.println(“C”); } System.out.println(“Bye.”); // What is the result for grade = 97 ?
  • 12. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 12 The Conditional Operator • Java has a conditional operator that evaluates a boolean condition that determines which of two other expressions is evaluated • The result of the chosen expression is the result of the entire conditional operator • Its syntax is: condition ? expression1 : expression2 • If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated
  • 13. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 13 The Conditional Operator • The conditional operator is similar to an if-else statement, except that it forms an expression that returns a value • For example: larger = ((num1 > num2) ? num1 : num2); • If num1 is greater that num2, then num1 is assigned to larger; otherwise,num2 is assigned to larger • The conditional operator is ternary because it requires three operands
  • 14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14 Example System.out.println ("Your change is " + count + ((count == 1) ? "Dime" : "Dimes")); • If count equals 1, then "Dime" is printed • If count is anything other than 1, then "Dimes" is printed
  • 15. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2017 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.