CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.

Slides:



Advertisements
Similar presentations
Operator overloading. Operatorsand precedence zLeft to right associative y:: scope resolution y. direct member access y-> indirect member access y[] subscripting.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Lecture 1 The Basics (Review of Familiar Topics).
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Intro to C++ for Robotics Made for ALARM. Computer System Hardware – cpu, memory, i/o Software – OS, drivers.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 05 (Part III) Control Statements: Part II.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Increment Operator To increment a variable X by 1. X+1;X+=;++X; X = 10 Y = X + 1;Y = 11 X += 1;X = 11 Y = ++X + 4;Y = 15.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 3 More Flow of Control Goals: To analyze the use of Boolean expressions To analyze the use of Boolean expressions To introduce the notion of enumerated.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
Operators & Expressions
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CPS120: Introduction to Computer Science Decision Making in Programs.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Reminders: Have you filled out the questionnaire in Moodle? (3 left – as of last night). Office hours.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Department of Software.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
Chapter 3 Control Statements
Selections Java.
Chapter 2 Assignment and Interactive Input
Engineering Problem Solving with C++, Etter/Ingber
Programming Fundamentals
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Precedence and Associativity
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 7 Additional Control Structures
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
CISC/CMPE320 - Prof. McLeod
Control Structures Part 3
Introduction to Programming – 4 Operators
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
2.6 The if/else Selection Structure
Herbert G. Mayer, PSU CS Status 7/19/2015
CSC215 Lecture Control Flow.
Presentation transcript:

CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC

Operator(s)DescriptionAssociativity ::Scope resolution (C++ only) N/A () []. -> Postfix increment and decrement Function call Array subscript Element selection by reference Element selection by pointer Left-to-Right ! ~ (type) * & sizeof new delete Prefix increment and decrement Unary plus and minus Logical NOT and bitwise one's complement Type cast Indirection (dereference) Address-of (reference) Size-of Dynamic memory (de-)allocation (C++ only)one's complement Right-to-Left.* ->*Pointer to member (C++ only) Left-to-Right * / % Multiplication, division, and modulus (remainder) + -Addition and subtraction >Bitwise left shift and right shift >= Relational “less than” and “less than or equal to” Relational “greater than” and “greater than or equal to” == !=Relational “equal to” and “not equal to” &Bitwise AND ^Bitwise XOR (exclusive or) |Bitwise OR (inclusive or) &&Logical AND ||Logical OR c?t:fc?t:fTernaryTernary conditional Right-to-Left = += -= *= /= %= >= &= ^= |= Direct assignment Assignment by sum and difference Assignment by product, dividend, and remainder Assignment by bitwise shift Assignment by bitwise AND, XOR, and OR,CommaLeft-to-Right Operator Precedence

Conditional Operator Shortcut for an if else Glenn Stevenson CSIS 113A MSJC

An example Glenn Stevenson CSIS 113A MSJC bonusAmt = amountSold <= ? amountSold *.035 : amountSold *.075; Is equivalent to: If(amountSold <= 35000) { bonusAmt = amountSold *.035; } else { bonusAmt = amountSold *.075; }

Multiple Choice Nested if & ladder style if / else / if –Provide a way to do multi-way branching Can make code convoluted –Hard to read switch might be a better choice Glenn Stevenson CSIS 113A MSJC

Switch Statement Glenn Stevenson CSIS 113A MSJC

Switch Continued Uses an integer expression to evaluate switch –Works like a telephone operator or railway switch –Looks for matching case Control of code branches –Can have any number of statements following each case label –break statement is used to end each case –Default statement works like else part of ladder style if / else /if statements Glenn Stevenson CSIS 113A MSJC

How it Works 1. Integer expression is evaluated 2. list of labeled constants is scanned –Looking for an exact match If a match is found c++ begins executing the first line of code following the matching case label Each line of code is executed in order –Until a break statement is found or all of the statements in the switch body have been executed 3. if no match is found –Execution jumps to the statement following the default keyword default is optional. If no default code resumes on the first line of code following the last brace of the switch statement. Glenn Stevenson CSIS 113A MSJC

An Example, A simple menu Glenn Stevenson CSIS 113A MSJC #include using namespace std; int main() { int selection; cout > selection; switch(selection) { case 1: cout << "Running word " << endl; break; case 2: cout << "Running Excel " << endl; break; case 3: cout << "Running Powerpoint " << endl; break; case 4: cout << "Good Bye " << endl; exit(0); default: cout << "Invalid Entry " << endl; } return 0; }

The break statement Omitting the break statement –Causes code to fall through to the next case Sometimes this is wanted Allows you to execute the same code for multiple constant conditions –Lets look at an example Glenn Stevenson CSIS 113A MSJC

Check Vowel Glenn Stevenson CSIS 113A MSJC #include using namespace std; int main() { char c; cout > c; switch(c) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'o': case 'O': case 'u': case 'U': cout << c << " is a vowel " << endl; break; default: cout << c << " is not a vowel " << endl; } return 0; }

Switch Pitfall  Forgetting the break;  No compiler error  Execution simply ‘falls thru’ other cases until break; Glenn Stevenson CSIS 113A MSJC