Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 5: Control Structures II (Repetition)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structures: continued.
Control Structures in C++ while, do/while, for switch, break, continue.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
The University of Texas – Pan American
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Control Structures RepetitionorIterationorLooping Part I.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7 Problem Solving with Loops
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Chapter 4 – C Program Control
while Repetition Structure
Chapter 4 C Program Control Part I
Engineering Problem Solving with C++, Etter/Ingber
JavaScript: Control Statements I
Chapter 4 - Program Control
The University of Texas – Pan American
Program Control Topics While loop For loop Switch statement
Presentation transcript:

Lecture 7: Menus and getting input

switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can assume and different actions are taken. Creating a menu. ATM, iPhone, … Format Series of case labels and an optional default case switch ( controlling expression ) { case ‘1’: actions case ‘2’: actions default: actions } break - exits from statement. It causes program control to continue with the first statement after the switch statement. default case occurs if none of the case s are matched The switch statement can be used only for testing a constant integral expression. Any combination of character constants and integer constants that evaluates to a constant integer value.

Create a Menu - Simple Calculator Problem Create a menu that either adds, subtracts, multiplies or divides two user-entered numbers. Assume only using integers Loop until the user selects “exit.” Keep printing the menu after each selection. Formulate the algorithm What does the menu look like? 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Exit Please select an operation:

Create a Menu - Simple Calculator Formulate the algorithm Which loop control structure should be used? Counter-controlled repetition Sentinel-controlled repetition

Create a Menu - Simple Calculator Formulate the algorithm Pseudocode algorithm Print the calculator menu Input the operation While the user has not as yet entered the sentinel If the operation is: addition: input two numbers (operands) add two numbers print the result subtraction: input two numbers (operands) subtract the second number from the first one print the result multiplication: input two numbers (operands) multiply the two numbers print the result division: input two numbers (operands) divide the first number by the second one print the result exit: no action default: print “Invalid operation! Please select your operation.” Print the calculator menu Input the operation

Flowcharting for the simple calculator begin Print the calculator menu; Input the operation; false end true case Addition true Input two numbers; Perform addition; Print result; break false case Subtraction true Input two numbers; Perform subtraction; Print result; break false case multiplication true Input two numbers; Perform multiplication; Print result; break false case division true Input two numbers; Perform division; Print result; break false default Print message operation != Exit Print the calculator menu; Input the operation; switch statement case Exit true Print messagebreak false

C Code Print out the menu

C Code switch statement checks each of its nested case s for a match break statement makes program skip to end of switch

C Code Repeat the menu for each iteration default case occurs if none of the case s are matched

Create a basic ATM menu interface using a switch statement  Create a menu with the following items: 1. Check balance, 2. Deposit, 3. Withdrawal, 4. Quick Withdrawal $25, and 5. Exit. (Hint: These will be the selections for your switch statement.)  Initialize the balance at $100. (Hint: Start your balance variable at 100.)  Each selection should perform it's task within the switch statement.  Loop until the user selects "5. Exit" (keep printing the menu after each selection). Save your file and submit in the ATM Interface dropbox. In-Class Programming Exercise Challenge: 4.28