switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Similar documents
Decision Making in C

Flow Control. CSC215 Lecture

Following is the general form of a typical decision making structure found in most of the programming languages:

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

COMP 110/L Lecture 10. Kyle Dewey

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Type Conversion. and. Statements

CSI33 Data Structures

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Input And Output of C++

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

5. Control Statements

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures in Java if-else and switch

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Pointers, Dynamic Data, and Reference Types

Constructors for classes

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Fundamentals of Programming Session 4

Structs. Comp Sci 1570 Introduction to C++ Introduction. Aggregate data. Example. General syntax Object initialization Initialization and access

Software Design & Programming I

CSC Java Programming, Fall Java Data Types and Control Constructs

Templating functions. Comp Sci 1570 Introduction to C++ Administrative notes. Review. Templates. Compiler processing. Functions Overloading

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CS2141 Software Development using C/C++ C++ Basics

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

FORM 2 (Please put your name and form # on the scantron!!!!)

5. Selection: If and Switch Controls

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

CS201- Introduction to Programming Current Quizzes

MODULE 2: Branching and Looping

UEE1302(1102) F10: Introduction to Computers and Programming

REPETITION CONTROL STRUCTURE LOGO

Introduction to C++ with content from

C Programming Language (Chapter 2 of K&R) Variables and Constants

COMP 202 Java in one week

C++ Final Exam 2017/2018

Maciej Sobieraj. Lecture 1

A Fast Review of C Essentials Part I

Exercise: Using Numbers

Lab # 02. Basic Elements of C++ _ Part1

Fundamentals of Programming Session 13

Control Structures in Java if-else and switch

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

CSCI 1061U Programming Workshop 2. C++ Basics

CHAPTER 9 FLOW OF CONTROL

Boolean Algebra Boolean Algebra

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Unit 3 Decision making, Looping and Arrays

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

8. Control statements

Decisions. Arizona State University 1

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

3 The L oop Control Structure

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Control Statements. If Statement if statement tests a particular condition

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

CS313D: ADVANCED PROGRAMMING LANGUAGE

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

COMS 469: Interactive Media II

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Decision Making -Branching. Class Incharge: S. Sasirekha

Other Loop Options EXAMPLE

Lecture 04 FUNCTIONS AND ARRAYS

Programming Fundamentals

Looping. Arizona State University 1

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CS 1044 Programming in C++ Test 1 READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties.

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Fundamental of Programming (C)

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Enumerations. Fundamentals of Computer Science

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Tokens, Expressions and Control Structures

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

int x = 5; double y = 3; // Integer division rounds the result down to the nearest whole number. cout << "1a: " << x / 3 << endl; //1

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Selection Statements. Pseudocode

bool bool - either true or false

Last Time: Rolling a Weighted Die

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Transcription:

Comp Sci 1570 Introduction to C++

Outline 1

Outline 1

Outline 1

switch ( e x p r e s s i o n ) { case c o n s t a n t 1 : group of statements 1; break ; case c o n s t a n t 2 : group of statements 2; break ;... d e f a u l t : default group of s t a t e m e n t s }

Outline 1

s w i t c h ( e x p r e s s i o n ){ case constant1 : group of statements 1; break ; case constant2 : group of statements 2; break ; default : d e f a u l t group of s t a t e m e n t s } switch evaluates expression and checks if it is equivalent to constant1; if it is, it executes statements-1 until it finds the break statement. When it finds this break statement, the program jumps to the end of the entire switch statement (the closing brace). If expression was not equal to constant1, it is then checked against constant2. If it is equal to this, it executes group-of-statements-2 until a break is found, when it jumps to the end of the switch. If the value of expression did not match any of the previously specified constants (there may be any number of these), the program executes the statements included after the default: label, if it exists (since it is optional).

Outline 1

How it works The value of control var is compared to constant1. If the values are equal, every statement after that is executed until a break is encountered, at which point control exits the switch-case statement. If they don t match, then C++ makes comparison to the value in the next case. This continues until a match is found, or until the default is encountered or until the end of the switch-case statement.

Outline 1

Typically this expression is just a single variable, but it can be something more complex like nx + 2 or nx ny. Expression must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type (that is, char, short, int, long, long long, or enum). Floating point variables and other non-integral types may not be used here. Any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. Constant for a case must be the same data type as the variable in the switch, and it must be a constant or a literal, and known at compile time. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Outline 1

case switch ( ch1 ) { case A : cout << A from Outer s w i t c h ; switch ( ch2 ) { case A : cout << A from I n n e r s w i t c h ; break ; case B : //... } break ; case B : //... }