1/21
unit 4
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
for loop
a for loop lets us repeat code a fixed number of times.
while loop
lets us repeat code as long as something is true.
condition
a condition is a code that you put inside an if statement or a while loop.
boolean
true or false value.
if/else statement
Control structure that lets us run either one section of code or another depending on a test. |
logical operator
used to make logical associations between boolean values.
comparison operator
used to make comparisons between values
randomize
to generate or select a random object.
break statement
the break; statement breaks out of the current loop, without executing any more code in the loop.
Iteration
repition of instructions a specified number of times, or until the condition is met.
if/else statements look like in code hs
if(condition == true){
Code to execute;
} else {
Code to execute;
}
correct syntax for loops
for(var i = 0; i < 10; i++){
Code to execute;
}
correct syntax for while loops
var count = 0;
while(count < 5){
println(count);
count = count + 1;
correct syntax for loops and a half
var SENTINEL = “abc123”;
while(true){
var password = readLine(“Enter your password”);
if(password == SENTINEL){
break;
}
}
when would you use a for loop?
when we want to repeat a line of code a fixed number of times
when should you use a while loop?
When we do not know how many times a line of code should repeat
What causes an infinite loop?
When there is no ending condition of there is an ending condition that is impossible to meet.
how do you randomize a number?
var dice= Randomizer.nextInt(1,6);
How do you randomize a boolean?
var choice= Randomizer.nextBoolean();
how do you randomize a color?
var rand color = Randomixer.nextColor();
truth table for &&
T && T = true
T && F = false
F && T = false
F && F = false
truth tables for || (or)
T || T = true
T || F = true
F || T = true
F || F = false