Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises."— Presentation transcript:

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

2 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 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 4 Do~While Loop do { : } while (condition);

5 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 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 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 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 9 More Exercises for Practice Programming Problem 2 page 301 Exam Preparation Exercise 10 page 298 Exam Preparation Exercise 12 page 299


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

Similar presentations


Ads by Google