Final Exams Pt 4 Flashcards
Describe the function of a while loop and compare it to a for loop
A while loop repeats code until a condition becomes false. In comparison with a for loop, a while loop runs an arbitrary (INCONSISTENT) number of times, while a for loop runs for a fixed number of
times.
Write a while loop that will:
Run as long as a value equals 4
while (value == 4)
Write a while loop that will:
Stop when a value equals “quit”
while (!value.equals (“quit”))
Write a while loop that will:
Run while a value is more than 100 and a value is ‘y’
while (first > 100 && second == ’y’)
Describe what user input is
1) interaction between person using program and program.
2) entering data
3) interacting with mouse
4) actions when certain keys are pressed on keyboard.
What types of data can be entered into Processing?
The same types that can be declared as variables: int, float, String, char
Write a line of code to:
Get an integer from the user and store it in a variable
int num = getInt(“Please enter an integer”);
Write a line of code to:
get a float from the user and store it in a variable
float val = getFloat (“Enter any number”);
Write a line of code to:
get a word from the user and store it in a variable
String word = getString (“Please enter a word”);
Describe how keyboard input is different from user input above
1) action of pressing key.
2) doesn’t send value to be stored, but creates ACTION that calls keyboard method.
What procedures are available for keyboard input? Briefly describe each
1) keyPressed() - when key is pressed
2) keyReleased() - when key is released
3) keyTyped() - when printable character is typed.
Write code for the following:
(a) Code that draws a SQUARE whenever any key on the keyboard is pressed
void keyPressed(){
square(x, y, 100);
}
Write a code for the following:
Code that will increase a variable by 2 when ‘w’ is pressed and decrease a variable by
3 when ‘s’ is pressed
void keyPressed(){
if(keyPressed == ‘w’){
x+=2;
}
if(keyPressed == ‘s’){
x-=3;
}
}