Download presentation
Presentation is loading. Please wait.
Published byHector Watkins Modified over 9 years ago
1
While Loops Indefinite Iteration
2
Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is ‘TRUE’ Lets look at the coding on the next slide.
3
A program which asks the user whether to display hello world or not and thus displaying how many times the user said ‘yes’ Void main () { char answer int count = 0, index = 1; answer=‘y’; while(answer==‘y’) { cout<<endl<<index<<“Hello World”; cout<<endl<<“Do you want another iteration (y/n):”; cin>>answer; index++; count++ } // end loop cout<<“You said yes: “<< (count-1)<<“ times”; }
4
TASK 1 Write programmer comments along each line explaining the code. Make sure for your game and other programs for the assignment you put programmer comments.
5
TASK 2 Create a program that tells the user if the user’s input is in the required range or not and thus calculating the attempt of the user. Look at the sample out put of the program below for a hint. Before coding try designing using pseudo code or structured English Enter a number in the range 1 to 100 inclusive: 341 *** you entered 341 – it is not in the required range *** please try again Enter a number in the range 1 to 100 inclusive: 441 *** you entered 441 – it is not in the required range *** please try again Enter a number in the range 1 to 100 inclusive: 0 *** you entered 0 – it is not in the required range *** please try again Enter a number in the range 1 to 100 inclusive: 73 *** you entered 73 – this is in the range You used 4 attempts to enter a valid number
6
Task 3 Produce a program with a menu driver: a while loop that will continue to iterate until the user chooses the exit option. The menu driver should show a list of numbered options (eg types of calculations) including an exit option, asks the user to enter a choice and then carries out what ever option was chosen (if the choice entered wasn’t a valid option then display an appropriate message). Your program should use the switch case default construct, not if else if else if else.. To do the selection of which bit of code gets called according to the user’s choice.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.