top of page

If statements

If- statements are the base of conditional statements. If the boolean expression in the if statement is true, then the code in the if statement will execute, otherwise it skips. 

For number values in a boolean statement we use conditional operators such as <, > and == to compare values to other values. note the difference between = and == is that == is asking if one value is equal to another while = makes something equal to another. 

For String values in a boolean statement we use the .equals() method. as shown in Data Types tab under "String Methods"

if.PNG

Else-if statements

Else-if statements are the exact same as if statements, however only check and execute when the starting If statement is false. If the else-if boolean expression is false then it skips over and doesn't execute. If the starting If statement is true then Java will not check the Else-if statements. Else if statements must have an If statement to exist, and must go after the if statement.

ELSE IF.PNG

Else statements

Else statements are statements that go at the end of If statements. Else statements will execute the code within them only if all other If and/or else-if statements were all false and were skipped over, Java will not execute the code in the else statement. You need to have an if statement to have an else statement. Else statements go at the very end of any if statement.

Flow charts

As it can become hard to keep track of the outcome of your code with too many If statements, programmers create something called a flow chart, showing what happens and what code executes when certain events play out

flow chart.png

© 2024 by Samuel Wong.
Powered and secured by Wix

Call

(1) + 437-216-9455

Contact

@Brebeuf STEAM

  • Twitter
  • Instagram
bottom of page