Chapter 12 Flashcards
If you wanted to repeat code over and over again what is it that you need in a program? And what is the basic format for this element?
In order to repeat statements over and over again you need something called a while statement also known as a while loop and the basic format goes like this:
while (condition) {
some statements
}
What part of the while loop is repeated over and over again?
The stuff that’s in between curly braces:
while (condition) {
Some statements
}
In a while loop what has to be met in order for the statements to be repeated over and over again?
The condition must be true in a while statement in order for the statements to be repeated over and over again
What is the results of running this while loop?
While( die1 + die2 != 7 && die1 + die2 != 11)
The results would be an iteration of loops until one of the dice hit 7 or 11
In order for this while loop to stop what needs to happen?
While( die1 + die2 != 7 && die1 + die2 != 11)
What happens if part of the condition is true while the other part is false
Both pieces of the condition must not equal seven or 11.
If the condition is false or partly false then when the Java program is run it will continue to iterate loops until it hits 7 or 11
Is similar to a while statement? What can be added to the statement within the curly braces of a while loop?
A while loop can contain one statement or several statements and more than one statement is called a compound statement
When a while loop and an if statement contains more than one statement within the curly braces what is this called ?
When multiple statements are within the curly braces of a while loop or an if statement this is called a block and it performs as one big statement or compound statements
During a while loop as it performs iterations when does it jump out of the loop to continue the rest of the code?
Depending on the condition once the condition is met then it’ll jump out of the loop
How do you stop a loop from performing an infinite amount of iterations or code from continuing to run without stopping?
There is a rectangular red button in the console view and when you hover your cruiser over it it displays terminate so click that button and it will stop any code dead in its tracks
In what occasion should you have code that continues to run continuously?
A heart rate monitor a lung breathing machine etc etc
How do you insert a tab in your code in between quote marks without pressing the tab key? And what is the name of this option?
Wherever you need a tab just type \t inside double quotes marks this is an example of what is called an escape sequence
What are two examples of an escape sequence? What escape sequence do you use to jump to a new line in between double quote marks?
Two examples of escape sequences are \t which jumps a tab in between double quote marks and \n which jumps to a new line and can be a substitute for System.out.println(“”)
System.out.print(“\n”)
When it comes to writing your code and the order of how it should be displayed in order to run correctly what’s one thing to keep in mind?
Depending on the program you’re trying to build you might want to write out the test part of the program first followed by the input and last to print. It just depends on the program you’re trying to build whatever makes sense you would start off with that component first like input print and then test