The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.

Slides:



Advertisements
Similar presentations
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
The switch statement: an N-way selection statement.
The switch Statement, DecimalFormat, and Introduction to Looping
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
JAVA Control Statement.
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
“Introduction to Programming With Java” Lecture - 6 U MESH P ATIL
Control Structures if else do while continue break switch case return for.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Chapter 05 (Part III) Control Statements: Part II.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
CSI 3125, Preliminaries, page 1 Compiling the Program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Introduction to Control Statements IT108 George Mason University.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Switch statement.
Core Java Statements in Java.
Lecture 4b Repeating With Loops
Control Structures.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 5 – part 1 Control Statements: switch Statement
Decision statements. - They can use logic to arrive at desired results
An Introduction to Java – Part I, language basics
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 2 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
PROGRAM FLOWCHART Selection Statements.
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Introduction to java Part I By Shenglan Zhang.
Selection Statements August 22, 2019 ICS102: The course.
Presentation transcript:

The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements from a number of choices depending on the value of an expression. The statement that will handle precisely this sort of situation is called the switch statement. The choices are called cases. The general form of the switch statement is:

The switch StatementtMyn2 switch(expression) { case constant1: statement sequence break; case constant2: statement sequence break; … default: statement sequence }

The switch StatementtMyn3 The switch expression must be of type char, byte, short, or int. Frequently, the expression controlling the switch is simply a variable.

The switch StatementtMyn4 The selection between a number of cases is determined by the value of an expression that you specify between parentheses following the keyword switch. The case values appear in a case label: case constant: The case constants must be literals of a type compatible with the expression. No two case constants in the same switch can have identical values.

The switch StatementtMyn5 The default label identifies the default case, which is a catch-all; the statements that follow are executed if the selection expression does not correspond to any of the case values. The default is optional; if it is not present, no action takes place if all matches fail. The break statement that appears after each set of case statements is absolutely necessary for the logic here.

The switch StatementtMyn6 package switchstatement; public class Main { public static void main(String[] args) { for(int i=0; i<10; i++) switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break;

The switch StatementtMyn7 case 3: System.out.println("i is three."); break; case 4: System.out.println("i is four."); break; default: System.out.println("i is five or more."); break; } run: i is zero. i is one. i is two. i is three. i is four. i is five or more. BUILD SUCCESSFUL (total time: 5 seconds)

The switch StatementtMyn8 The statement list for a case can also be empty, which simply passes control into the statement list for the next case.

The switch StatementtMyn9 package switchstatement1; public class Main { public static void main(String[] args) { for(int i=0; i<10; i++) switch(i) { case 0: System.out.println("i is zero."); break; case 1: System.out.println("i is one."); break; case 2: System.out.println("i is two."); break;

The switch StatementtMyn10 case 3: case 4: case 5: case 6: System.out.println("i has the value between three and six."); break; default: System.out.println("i is seven or more."); break; } run: i is zero. i is one. i is two. i has the value between three and six. i is seven or more. BUILD SUCCESSFUL (total time: 2 seconds)