Download presentation
Presentation is loading. Please wait.
2
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 l Loop Exercises
3
2 HW#2 Assigned l Very clear vertical organization l Use separate function to display menu and pass the user choice back to the main() function l Switch-Case preferable over if-else l Use comments at all strategic places l Try to put everything in one file for this assignment
4
3 Multiple Choice Selectors l In case you have more than two alternatives, use switch-case construct l You can only use int, char or bool type variables in switch evaluations l string or float type variables cannot be used in switch evaluations
5
4 The While Loop l While loop has general construct l While (expression) {……….} l This loop will be executed if the expression in the parenthesis evaluates to true l Example: l int k; k=100; l while (k>0) {cout<<k<<endl; k--}
6
5 Switch-Case-Break Exercises l Let us implement switch-case-break constructs in a menu driven software. The menu should present choices for a library catalog computer. Choices are search catalogue, issue book, return book, and print member info. Any wrong input will create two warning beeps and system will re-display the menu.
7
6 Problem Analysis l Data Modeling l int choice; //input l Algorithm Display menu Get user input Check if selected menu item is valid If yes, display selected choice else, create two beeps and go to display menu again
8
7 Implementation of Menu Choice Selector l int disp_menu(); l int choice; l choice = 0; l While ((choice 4) l {choice = disp_menu(); l switch(choice) l case 1: cout<<“You wish to search””<<endl;
9
8 Counter Controlled Loops l The for loops are counter controlled loops.(Convert earlier loop to for) l You can make the counter increment or decrement or “jump up” or “jump down” (i.e. inc/dec steps allowed) l You can increment or decrement multiple counters if you wish l Let us take an example of counter controlled loop implementation
10
9 The Case of the Upper and Lower Case Letters l 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
11
10 Printing List of Alphabets l Declare two char variables l char lower, upper; l assign these the starting values l (e.g. lower=‘a’) l cout <<“\t”<<lower l <<“\t”<<int(lower); l Increment these in for loop
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.