Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lab Session-7 CSIT-121 Fall 2003 4 Revising Structured Choice 4 The While Loop variations 4 Lab Exercises.

Similar presentations


Presentation on theme: "1 Lab Session-7 CSIT-121 Fall 2003 4 Revising Structured Choice 4 The While Loop variations 4 Lab Exercises."— Presentation transcript:

1

2 1 Lab Session-7 CSIT-121 Fall 2003 4 Revising Structured Choice 4 The While Loop variations 4 Lab Exercises

3 2 Revising Structured Choice 4 Structured choice can be implemented with switch~case construct. 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 Counter Controlled Loops 4 The for loops are counter controlled loops.(Convert this loop to for) –int k; k=100; –while (k>0) {cout<<k<<endl; k--} 4 You can make the counter increment or decrement or “jump up” or “jump down” (i.e. inc/dec steps allowed) 4 You can increment or decrement multiple counters if you wish

5 4 The counter-controlled loop 4 The for loop is best for implementing a counter controlled loop because we can check all three conditions (I,T,U) at a glance. 4 For example, we can convert a given while loop into for loop to show how it works

6 5 The for loop 4 int count=0; //I 4 while (count<20) { //T 4 cout >thisID; count++;} //U 4 Changed to: 4 int count; 4 for (count=0;/*I*/ count<20; /*T*/count++/*U*/) 4 {cout >thisID; } 4 The for loop may have null expressions 4 e.g. for (; ;){cout<<“Will it end?\n”;}

7 6 The Upper and Lower Case Letters 4 We wish to print a list of uppercase letters with their ASCII codes followed by a list of lower case letters with their ASCII codes in the same row albeit with some tabbing distance

8 7 Printing List of Alphabets 4 Declare two char variables 4 char lower, upper; 4 assign these the starting values 4 (e.g. lower=‘a’) 4 cout <<“\t”<<lower 4 <<“\t”<<int(lower); 4 Increment these in for loop

9 8 While Loop Design 4 Design While loop with care else the program will be stuck in INFINITE LOOP. 4 Check for these three conditions 4 INITIALIZATION (The loop control variable should be initialized before starting the loop) 4 TESTING While(test) {…..} 4 UPDATE Loop control variable should be updated within the loop

10 9 Lab Exercise (Demo Required Sec01-Oct 21, Sec02-Oct 23) 4 Develop a program that accepts three integers and prints their bar chart, followed by their average. For example, input 5, 7 and 9 & get output as: 4 5: ***** 4 7: ******* 4 9: ********* 4 Average is 7.0

11 10 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

12 11 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

13 12 More Exercises for Practice 4 Programming Problem 2 page 306 (old edition see page 322) 4 Exam Preparation Exercise 10 page 303 (old edition see page 320) 4 Exam Preparation Exercise 12 page 304 (old edition see page 321)


Download ppt "1 Lab Session-7 CSIT-121 Fall 2003 4 Revising Structured Choice 4 The While Loop variations 4 Lab Exercises."

Similar presentations


Ads by Google