Switch Statement. Nested IF-ELSE Statement if (month == 1) { mString="January"; } else if (month == 2) { mString=“February”; } else if (month == 3) {

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
The switch statement Week 5. The switch statement Java Method Coding CONCEPTS COVERED THIS WEEK.
If/else and switch. Assignments Due – Lab 3 No reading – study for your quiz!
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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.
Switch Statement switch (month) { case 9: case 4: case 6: case 11: days = 30; break; case 2: days = 28; if (year % 4 == 0) days = 29; break; default: days.
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
Nested conditional statements. A conditional statement (i.e., an if-statement or an if-else- statement) is also a statement We can use an if-statement.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 7 Decision Making : selection statements.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Decision Making Selection structures (if....else and switch/case)
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
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.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Boolean Expressions and if-else Statements Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
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.
Condition & loop Android Club Agenda if/else Switch Loops.
1 CS1001 Lecture Overview Java Programming Java Programming Arrays Arrays.
Control statements Mostafa Abdallah
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.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
CSI 3125, Preliminaries, page 1 Control Statements.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Decision Statements, Short- Circuit Evaluation, Errors.
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.
Chapter 6 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Silberschatz and Galvin  C Programming Language Decision making in C Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University.
 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.
If/Else Statements.
CHAPTER 4 DECISIONS & LOOPS
Java Programming Fifth Edition
Decision Making in C.
More on the Selection Structure
Compound Condition Break , Continue Switch Statements in Java
Unit-1 Introduction to Java
Computer Programming Chapter 5: Switch statement
Expressions and Control Flow in JavaScript
Control Structures.
الكلية الجامعية للعلوم التطبيقية
IF if (condition) { Process… }
The switch statement: an alternative to writing a lot of conditionals
Control Structures: Selection Statement
Control structures Chapter 3.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Control structures Chapter 3.
Program Flow.
Control structures Chapter 3.
Control Structures: Selection Statement
Lecture 4: Selection Control Structure
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Presentation transcript:

Switch Statement

Nested IF-ELSE Statement if (month == 1) { mString="January"; } else if (month == 2) { mString=“February”; } else if (month == 3) { mString=“March”; } else if (month == 4) { mString=“April” } else mString=“Some other month”; This statement is only checking for DISCRETE values (not upper and lower limits) If this is what you need to do, you can use another kind of statement which may be more easier to understand and/or use

Switch Statement General form switch (checkVariable) { case 0: commands break; case 1: commands break; case 2: commands break; default: command } the variable you’re checking Opening Brace to start switch statement if the checkVariable equals 0 then do the following commands this tells Java to get out of switch statement if none of the cases exist do these default commands Closing brace to end switch statement

Example switch (month) { case 1: mString="January"; break; case 2: mString=“February”; break; case 3: mString=“March”; break; case 4: mString=“April” break; default: mString=“Some other month”; }