Java Lesson 4: Using if Statements

Key Points:

  • An if statement is used to make a decision structure that allows a program to have more that one way to execute.
  • An if statement contains a boolean expression, a boolean is a data type that is either true or false. 
  • Here is an example (real life, not actual code!):
    if (no milk){
    go to store;
    get milk;
  • Here is an example of an actual if statement.
    if(money <100){
    System.out.println(“Don’t go shopping!);
  • You can also use an if-else statement.  In an if-else statement, if the first boolean expression is true the second one is false. If the first boolean expression is false, the second one may or may not be true. See example below:
    System.out.println(“Don’t go shopping!);
    System.out.println(“Go shopping!);

View the Code