Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.


+6 votes

If we are creating a while loop, what should the sentinel value be set equal to so that when the Enter key is pressed, the loop terminates? Assuming we are using the checkKey() method Also, would it be different if we were to use the getKey() method?

asked in old_CSC201 by (8 points)

1 Answer

+1 vote

If you are using the Enter key as a sentinel to end the loop, then you would be entering data from the keyboard using the input function. You wouldn't be using checkKey() or getKey().

checkKey() and getKey() are only using when you have a GraphWin object, ie. a Graphics window. If you are trying to enter words or numbers in a graphics application, then you would be using an Entry object. The final exam does not test you on Entry objects.

checkKey() and getKey() are for detecting one key press in the GraphWin object. The animation on Midterm 2 was to click in the window to start the circle animating up and then click the window again to make the begin moving down. We used getMouse for the first click (since there was no changes to the graphics window until the click so execution of the program just stopped until the click). We used checkMouse for the second click because the program needed to be executing to move the circle and checkMouse doesn't stop execution. checkMouse, each time it executes, returns the mouse click since the last time it executed.

We could have used getKey and checkKey for that problem on the midterm instead. If the directions had said that nothing was going to change in the graphics window until a certain key was pressed, then I would be using getKey(). Then if a particular key was pressed, the circle was to start moving downward, then I would use checkKey() since execution would continue even though the checkKey function executed.

answered by (8 points)