Game Lab Documentation

If/else statement

Category:Control

Apps usually need to make decisions and execute some code if something is true and some other code otherwise. Most programming languages have an if/else statement: to check to see if some expression is true, and if it is do something, otherwise do something else.

An if/else statement requires you to define an expression that evaluates to true or false. Just as in arithmetic there are some operators you can use to write expressions that evaluate to a number, programming languages also have a comparison operators (< <= == > >= !=) and boolean operators (&& || !) that let you write expressions that evaluate to true or false.

The if/else statement defines two blocks of code to execute between open and closing curly braces {}. If the condition is true then the block of code inside the first curly braces is executed from top to bottom, exactly once. If the condition is false then then the block of code inside the second curly braces is executed from top to bottom, exactly once.

Examples

Example: Overtime Hours

// Prompts the user for the number of hours they worked and tells them if they worked overtime or not.
var hoursWorked = promptNum("How many hours did you work this week?");
if (hoursWorked > 40) {
  write("You worked " + (hoursWorked-40) + " hours overtime.");
}
else {
  write("You did not work any overtime.");
}  

Example: Even or Odd

Determines if a random number is even or odd.

// Determines if a random number is even or odd.
var num = randomNumber(0, 100);
var evenOrOdd = "unknown";
var remainder = num % 2;
if (remainder == 0) {
  evenOrOdd = "even";
} else {
  evenOrOdd = "odd";
}
console.log(num + " is " + evenOrOdd + ".");

Example: Letter Grade

Prompt the user for an exam score and assign a letter grade.

// Prompt the user for an exam score and assign a letter grade.
var examGrade = promptNum("Enter an exam score from 0 to 100:");
if (examGrade>=90) {
  write("Grade = A");
} else if (examGrade>=80) {
  write("Grade = B");  
} else if (examGrade>=70) {
  write("Grade = C");  
} else if (examGrade>=60) {
  write("Grade = D");  
} else {
  write("Grade = F");  
}

Syntax

if (condition) { statement1 } else { statement2 }

Parameters

NameTypeRequired?Description
conditionBoolean expression

An expression that evaluates to true or false. Comparison operators include < <= == > >= !=. Boolean operators include && || !

statement1App Lab statement(s)

Any valid App Lab statements.

statement2App Lab statement(s)

Any valid App Lab statements.

Returns

No return value.

Tips

  • Unlike an event handler, an if/else statement does not constantly monitor your program checking the condition to see if it's true or false. An if statement is an instruction just like any other that gets executed line by line in order from top to bottom.
  • = is the assignment operator. == is the boolean check for equivalency operator.

Found a bug in the documentation? Let us know at support@code.org.