1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.

Slides:



Advertisements
Similar presentations
Control Structures Corresponds with Chapters 3 and 4.
Advertisements

1 Session-17 CSIT 121 Spring 2006 Loops: Conditional and Count Controlled Loops: Conditional and Count Controlled LCV: Initialize; Test and Update LCV:
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 Session-16 CSIT 121 Spring 2006 Demo for switch-case due now Demo for switch-case due now String comparison String comparison Slide 23; sample case.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 Session-I & II CSIT-121 Spring 2006 Session Targets Introducing the VC++.NET Solving problems on computer Programming in C++ Writing and Running Programs.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lab Session-3 CSIT221 Spring 2003 b Group Worksheet 3 Exercise (Demo Required) b No new lab demo will be assigned to allow you to focus on HW#1.
1 Session-7 CSIT 121 Spring 2006 Lab Demo of NiMo Lesson 3-2 Exercises 1,2,3,4,5,6 (Arithmetic Operators with Program ‘Convert.cpp’) Q&A about operators.
1 Session-15 CSIT 121 Spring 2006 Selection with Switch~Case Selection with Switch~Case Need an integer or character to test for Need an integer or character.
1 Session-11 CSIT 121 Spring 2006 Test-1 is on March 9 th ; Demo-4 due Now Chapter 4 topics –Using get and ignore to control input data (3-19) –Prompting.
1 Lab Session-7 CSIT-121 Fall Revising Structured Choice 4 The While Loop variations 4 Lab Exercises.
1 Lab Session-3 CSIT 121 Fall 2004 Section-3 should finish Lab-1 Exercise first Division rules Operator precedence rules Lab Exercise.
1 Lab Session-VII CSIT-121 Fall Revising Previous Lab and performing ASCII chart printing experiment (DEMO) 4 Visual Studio Features 4 The While.
1 Lab Session-V CSIT-120 Fall 2000 Menu Driven Software Lab Exercise While and do-while statements Lab-V Continues: The if statement Logical Operators.
1 Lab Session-3 CSIT 121 Spring’05 Division rules Operator precedence rules Lab Exercise.
1 Lab Session-11 CSIT 121 Fall 2003 Using arrays in functions Programming Exercise.
1 Lab Session-VI CS121 Fall 2000 l HW#2 Assigned l Multiple Choice Selectors l The While Loop l Switch-Case-Break Exercise l The Counter Controlled Loops.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
1 Lab 2 CSIT-120 Fall 2000 Session II-A (September 14th) Operations on Data Lab Exercise 2-A Data Types Variables Lab Exercise 2-B Session II-B (September.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
1 Lab Session-8 CSIT-121 Spring 2005 Call by Reference Lab Exercise for Demo Practice Problems.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Previously Repetition Structures While, Do-While, For.
Control Structures Repetition or Iteration or Looping Part II.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CREATING MENUS IN JAVA Mimi Opkins CECS 174. Menus Menus work well for console applications. The menu can be contained within a do-while loop and the.
Control Structures RepetitionorIterationorLooping Part I.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Data Validation while loops. Data validation Programs get input data from different sources All data should be ‘validated’ before the program tries to.
1 CS161 Introduction to Computer Science Topic #8.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
LAB SESSION ONE DIMENSIONAL ARRAY.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
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.
COMP Loop Statements Yi Hong May 21, 2015.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
C++ Programming: CS102 LOOP. Not everything that can be counted counts, and not every thing that counts can be counted. −Albert Einstein Who can control.
Exercise : Write a program that print the final price of purchase at a store where everything costs exactly one dollar. Ask for the number of items purchased.
C++ Programming: CS150 For.
Chapter Topics 11.1 Introduction to Menu-Driven Programs
Control Structures - Repetition
Structured Program Development in C
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Lab5 PROGRAMMING 1 Loop chapter4.
Looping III (do … while statement)
EECE.2160 ECE Application Programming
Introduction to Programming
Learning Intention I will learn about the standard algorithm for input validation.
Presentation transcript:

1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises

2 Structured Choice Structured choice can be implemented with switch~case construct. switch(variable) { case x: {………} break; case y: {………} break; case z: {……….} break; default: {……..} break; } When should we prefer switch-case over if-else? Why is a default case added at the end? Is it ok to use a floating point value in the switch statement?

3 Using a do-while loop You should use a do~while loop if you wish to run the loop body at least once before testing the condition For example, a menu driven program should display all the choices and then accept user input. Next, the user input can be tested for validity. If invalid, run the loop body again

4 Do~While Loop do { : } while (condition);

5 Do-While + Switch-Case Demo Write a program that displays menu choices for computing a shopping invoice. The choices include entering total number of items, entering prices of all the items, viewing the invoice with prices and sales tax followed by Grand Total, printing the invoice, saving the invoice to a file and exiting the program

6 Lab Exercise (Demo Due March 9th) Develop a program that accepts several integers from the keyboard (range:1 to 25) and prints their values followed by their bar charts and finally the min and max values. Program must ask the user the number of values in advance and it must check for valid numbers. Invalid numbers are rejected but the user is given a chance to complete the count. (HINT: Think about saving the valid numbers into a file while the numbers are being entered. Once all the numbers have been entered, you can close the file and open it again for reading. Thus you can get the bar chart display similar to the sample output shown)

7 Algorithm for finding max of a set of values Step 1: Set max  0 Step 2: Read the new data Step 3: If max is less than the new data then – Step 3a: Set max  new data If more numbers, go back to step 2

8 Test Data and Expected Output Input number of values=9 Values entered: {2,-3,4,9,22,12,25,20,18,23} 2: ** 4: **** 9: ********* 22: ********************** 12: ************ 25: ************************* 20: ******************** 18: ****************** 23: *********************** Min accepted value entered was 2 Max accepted value entered was 25

9 More Exercises for Practice Programming Problem 2 page 301 Exam Preparation Exercise 10 page 298 Exam Preparation Exercise 12 page 299