A loop that repeats code a fixed number of times
For Loop
How many times will the following program print "hello"?
var i = 0;
while (i < 10){
println("hello");}
This code will loop infinitely
These can be used to make logical associations between boolean values
Logical Operators
What is the last thing printed by the following program?
var start = 30;
var stop = 10;
for ( var i = start; i >= stop; i -=5){
if (i % 2 ==0){
println(i*2);
} else{
println(i);
}
}
20
What will be the output of this program?
var number = 5;
var greater_than_zero = number > 0;
if(greater_than_zero){
println(number);
}
5
These are used to make comparisons between values
Comparison Operators
We want to print the phrase "CodeHS is the best" exactly 25 times. What kind of control structure should we use?
For Loop
Determine what the following expression would evaluate to.
(9 =/ 7 AND 17 < 4 ) OR 65 < 9
False
What will the following program print when run?
for (var j = 0; j < 2; j++) {
for (var i = 6; i > 4; i --){
println (i);
}
}
6
5
6
5
What will be the output of this program?
var number = 5;
var greater_than_zero = number > 0;
if(greater_than_zero){
if(number>5){
println(number);
}
Nothing will print
Iteration
We want to simulate constantly flipping a coin until we get 3 heads in a row. What kind of loop should we use?
While Loop
Which of the following Boolean expressions is equivalent to the expression?
( a AND b ) OR (a AND c)
A. (a AND b) OR c
B. (a AND b) AND ( a AND c)
C. (b AND c) OR a
D. a AND (b OR C)
a AND (b OR c)
What is printed by the following program?
var isRaining = false;
var isCloudy = false;
var isSunny = !isRaining && !isCloudy;
var isSummer = false;
var isWarm = isSunny || isSummer;
println("Is it warm:" + isWarm);
Is it warm: true
What is the output of the following program?
var result = 0;
var max = 5;
for(var i = 0; i < max; i ++){
result+= i;
}
println(result);
10
Code that you put inside an if statement or while loop
Condition
Which of the following returns a random number between 1 and 99?
A. randomInt (1,99)
B. randInt(1,99)
C. Randomizer.nextInt(1,99)
D. Randomizer.int (1,99)
Randomizer.nextInt (1,99)
IF ((NOT rainy) AND (NOT tooCold))
{DISPLAY("It's a good beach day")}
Which of the following are equivalent to the above code block?
A. IF ((NOT rainy) OR (NOT tooCold))
{DISPLAY ("It's a good beach day")}
B. IF ((NOT rainy) AND tooCold)
{DISPLAY ("It's a good beach day")}
C. IF (NOT (rainy OR tooCold))
{DISPLAY (It's a good beach day")}
D. IF (rainy AND tooCold)
{DISPLAY ("It's a good beach day")}
{
DISPLAY (It's a good beach day")
}
What is printed by the following program?
var numApples= 10;
var numOranges= 5;
if(numApples < 20 || numOranges == numApples){
println("Hello, we are open!");
}else{
println("Sorry, we are closed!");
}
println("Sincerely, the grocery store");
Hello, we are open!
Sincerely, the grocery store
What will be the output when the following code runs?
function start(){
var loggedIn = false;
println("User logged in?:" + !loggedIn);
}
User logged in?: true
Repeating code as long as something is true
While Loop
Consider the following program. Which of the following is NOT a possible output when this runs?
function start(){
var mysteryNum= 10 * Randomizer.nextInt(2,10);
println(mysteryNum);
}
A. 50
B. 30
C. 10
D. 90
10
What is the value of the boolean variable canVote at the end of this program?
var age = 17;
var isCitizen = true;
var canVote = age >= 18 && isCitizen;
FALSE
What will the following program print when run?
var above16= true;
var hasPermit= true;
var passedTest= false;
if(above16 && hasPermit && passedTest){
println("Issue Driver's License");
}else{
if(above16 || hasPermit || passedTest){
print ln("Almost eligible for Driver's License");
}else{
println("No requirements met.");
}
}
Almost eligible for Driver's License
What is the output of the following program?
var numberOne = 5;
var numberTwo = 10;
if(numberOne == 5){
println(1);
}
if (numberOne > 5){
println(2);
}
if(numberTwo < 5) {
println(3);
}
if (numberOne < numberTwo) {
println(4);
}
if (numberOne ! = numberTwo){
println(5);
}
1
4
5