Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lab Session-7 CSIT-121 Fall 2004 4 Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.

Similar presentations


Presentation on theme: "1 Lab Session-7 CSIT-121 Fall 2004 4 Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises."— Presentation transcript:

1

2 1 Lab Session-7 CSIT-121 Fall 2004 4 Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises

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

4 3 Using a do-while loop 4 You should use a do~while loop if you wish to run the loop body at least once before testing the condition 4 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

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

6 5 Do-While + Switch-Case Demo 4 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

7 6 Lab Exercise (Demo Required on Oct 19) 4 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 their average. 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 enter all the numbers.

8 7 Test Data and Expected Output 4 Input number of values=9 4 Values entered: {2,-3,4,9,22,12,25,20,18,23} 4 2: ** 4 4: **** 4 9: ********* 4 22: ********************** 4 12: ************ 4 25: ************************* 4 20: ******************** 4 18: ****************** 4 23: *********************** 4 Average is 15

9 8 Practice Exercises 4 Design a program that: 4 prompts the user to input the salary for tax computation 4 Checks the user input to see if it is within the given salary range (2000-5000) 4 If not, it gives a warning beep and re- displays the prompt on a clean screen. 4 Use do-while{} loop and see how the screen can be cleared between menu displays

10 9 More Exercises for Practice 4 Programming Problem 2 page 301 (old edition see page 306) 4 Exam Preparation Exercise 10 page 298 (old edition see page 303) 4 Exam Preparation Exercise 12 page 299 (old edition see page 304)


Download ppt "1 Lab Session-7 CSIT-121 Fall 2004 4 Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises."

Similar presentations


Ads by Google