Programming Fundamentals

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
1 10/16/06CS150 Introduction to Computer Science 1 switch Selection Structure.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
Logical Operators and Conditional statements
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Previously Repetition Structures While, Do-While, For.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Control statements Mostafa Abdallah
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Switch Statements Comparing Exact Values
C++ Lesson 1.
The Ohio State University
Chapter 3 Selection Statements
Review 1.
LESSON 4 Decision Control Structure
Programming Fundamentals
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Control Structures.
Chapter 6 More Conditionals and Loops
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Compound Assignment Operators in C++
Counting Loops.
Flow Control Statements
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Control Structures Part 2
Chapter 4: Control Structures I (Selection)
Control Structures Part 3
Conditionals.
2.6 The if/else Selection Structure
The Java switch Statement
CprE 185: Intro to Problem Solving (using C)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
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)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
CprE 185: Intro to Problem Solving (using C)
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Programming Fundamentals Lecture 8 Ms. Farah Younas

Agenda of Lecture Switch Statements

The Switch Statement A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case

The Switch Statement multiway expression value1 action 1 value2

The Switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression, then attempts to match the result to one of several possible cases The match must be an exact match. switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... }

The Switch Statement Each case contains a value and a list of statements The flow of control transfers to statement associated with the first case value that matches switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... }

Switch - syntax The general syntax of a switch statement is: switch and case are reserved words switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } If expression matches value3, control jumps to here

The Switch Statement The break statement can be used as the last statement in each case's statement list A break statement causes control to transfer to the end of the switch statement If a break statement is not used, the flow of control will continue into the next case switch ( expression ){ case value1 : statement-list1 break; case value2 : statement-list2 case value3 : statement-list3 case ... }

Switch Statement Example Examples of the switch statement: switch (option){ case 'A': aCount++; break; case 'B': bCount++; case 'C': cCount++; }

Switch – no breaks!!! switch (option){ case 'A': switch (option){ Another Example: switch (option){ case 'A': aCount++; break; case 'B': bCount++; case 'C': cCount++; } switch (option){ case 'A': aCount++; case 'B': bCount++; case 'C': cCount++; }

Switch- default A switch statement can have an optional default case The default case has no associated value and simply uses the reserved word default If the default case is present, control will transfer to it if no other case value matches If there is no default case, and no other value matches, control falls through to the statement after the switch

The switch Statement switch (option){ case 'A': aCount++; break; Switch with default case: switch (option){ case 'A': aCount++; break; case 'B': bCount++; case 'C': cCount++; default: otherCount++; }

switch Statement: Example #include <iostream> using namespace std; int main() { char answer; cout << "Is Programming Fundamentals an easy course? (y/n): "; cin >> answer; switch (answer){ case ‘Y': cout << "I think so too!" << endl; break; case ‘N': cout << "Are you kidding?" << endl; default: cout << "Is that a yes or no?" << endl; } return 0;

switch Statement: Example #include <iostream> using namespace std; int main() { char answer; cout << "Is Programming Fundamentals an easy course? (y/n): "; cin >> answer; switch (answer){ case 'Y': case 'y': cout << "I think so too!" << endl; break; case 'N': case 'n': cout << "Are you kidding?" << endl; default: cout << "Is that a yes or no?" << endl; } return 0;

switch Statement :Example

To Switch or not to Switch The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int, long) or a char It cannot be a boolean value or a floating point value (float or double) The implicit boolean condition in a switch statement is equality You cannot perform relational checks with a switch statement

Questions??