Copyright ©2005  Department of Computer & Information Science Switch Statements.

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
More Program Flow Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences National.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
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.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Copyright ©2005  Department of Computer & Information Science Introducing DHTML & DOM: Form Validation.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Decision Structures.
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.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Chapter 4 Selection Structures: if and switch Statements Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
ا.ريم العتيبي ا.ندى الطويرقي ا.هدى السفياني
Copyright ©2005  Department of Computer & Information Science Introducing Dialogue Windows.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Computer Science: A Structured Programming Approach Using C1 5-3 Multiway Selection In addition to two-way selection, most programming languages provide.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright ©2005  Department of Computer & Information Science JavaScript Modularity.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CISC105 – General Computer Science Class 8 – 06/28/2006.
CPS120: Introduction to Computer Science Decision Making in Programs.
Complex Conditionals. N-way decisions How to deal with more than 2 alternates? Nested else statements.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Copyright ©2005  Department of Computer & Information Science If-Then-Else Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
 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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Java Programming Fifth Edition
Course Outcomes of Programming In C (PIC) (17212, C203):
More on the Selection Structure
Compound Condition Break , Continue Switch Statements in Java
Lecture 3- Decision Structures
Control Structures.
CS149D Elements of Computer Science
Topics discussed in this section:
IF if (condition) { Process… }
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Control Structure Senior Lecturer
CSC530 Data Structure - Decision
Control Structures: Selection Statement
During the last lecture we had a discussion on Data Types, Variables & Operators
Three Special Structures – Case, Do While, and Do Until
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Islamic University of Gaza
Program Flow.
Using Decision Structures
PROGRAM FLOWCHART Selection Statements.
Decisions, decisions, decisions
Control Structures: Selection Statement
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Copyright ©2005  Department of Computer & Information Science Switch Statements

Copyright ©2005  Department of Computer & Information Science Goals By the end of this lecture you should … Understand how to program switch statements with case blocks to test a single variable against multiple possible values.Understand how to program switch statements with case blocks to test a single variable against multiple possible values.

Copyright ©2005  Department of Computer & Information Science Nesting Decision Structures? Of course, JavaScript allows us to program nested if-then-else structures to deal with multiple possible situations.Of course, JavaScript allows us to program nested if-then-else structures to deal with multiple possible situations. However, sometimes it is easier to use a switch statement instead, especially if you are testing a single variable against multiple possible values …However, sometimes it is easier to use a switch statement instead, especially if you are testing a single variable against multiple possible values …

Copyright ©2005  Department of Computer & Information Science The switch Statement The switch statement allows us to test a single variable against multiple possible values.The switch statement allows us to test a single variable against multiple possible values. We test each possible value in case blocks, each of which includes a value against which we test and an executable block, executed if the value matches.We test each possible value in case blocks, each of which includes a value against which we test and an executable block, executed if the value matches.

Copyright ©2005  Department of Computer & Information Science More on the switch Statement We can have any number of cases. Each case's executable block needs to contain a break statement at the end in order to "break out" of the switch structure, if we found a matching value.We can have any number of cases. Each case's executable block needs to contain a break statement at the end in order to "break out" of the switch structure, if we found a matching value. It's a good idea to include a default case at the end of the switch statement.It's a good idea to include a default case at the end of the switch statement.

Copyright ©2005  Department of Computer & Information Science switch Statement – General Form switch(variable) { case value1: //Block for value1 break;default: //Block for default }

Copyright ©2005  Department of Computer & Information Science Take the next few minutes to examine the file called switchCase_01.html.

Copyright ©2005  Department of Computer & Information Science Summary We can use switch statements to test single variables against multiple possible values.We can use switch statements to test single variables against multiple possible values. case blocks provide executable blocks for matching values in a switch statement.case blocks provide executable blocks for matching values in a switch statement. continued …

Copyright ©2005  Department of Computer & Information Science Summary Each case block must end with a break statement.Each case block must end with a break statement. It's considered good programming to include a default block after all case blocks.It's considered good programming to include a default block after all case blocks.