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
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
J-1 University of Washington Computer Programming I Switch Statement © 2000 UW CSE.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Controlling Execution Dong Shao, Nanjing Unviersity.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
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.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Module 3: Steering&Arrays #1 2000/01Scientific Computing in OOCourse code 3C59 Module 3: Algorithm steering elements If, elseif, else Switch and enumerated.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
POLYNOMIALS - Evaluating When evaluating polynomials, we are simply substituting a value in for a variable wherever that variable appears in the expression.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Select (drop-down list) Inputs. Insert/Form/List Menu.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Week 4 Program Control Structure
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
JavaScript and Ajax (Control Structures) Week 4 Web site:
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Java Programming Fifth Edition
EGR 2261 Unit 4 Control Structures I: Selection
DKT121: Fundamental of Computer Programming
Expressions and Control Flow in JavaScript
Control Structures.
IF if (condition) { Process… }
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Control Structures Part 3
The Java switch Statement
Methods.
Week 3 – Program Control Structure
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)
Chapter 3: Selection Structures: Making Decisions
CSC215 Lecture Control Flow.
Controlling Program Flow
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 integer variable or expression. The statement that will handle precisely this sort of situation is called the switch statement. The choices are called cases.

The switch StatementtMyn2 The selection between a number of cases is determined by the value of an integer expression that you specify between parentheses following the keyword switch. The case values appear in a case label: case caseValue: The case expression may be any expression that evaluates to a simple type, that is, integer or floating- point numbers and strings.

The switch StatementtMyn3 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 break statement that appears after each set of case statements is absolutely necessary for the logic here.

The switch StatementtMyn4

The switch StatementtMyn5

The switch StatementtMyn6

The switch StatementtMyn7 It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

The switch StatementtMyn8

The switch StatementtMyn9

The switch StatementtMyn10

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

The switch StatementtMyn12

The switch StatementtMyn13