Mark As Completed Discussion

Decisive Statements

These statements are used when you need to decide between alternative actions during the execution of a program. They allow you to control the flow of execution depending on the conditions known only during runtime

1) if statements

  • An if statement is the most simple decision making a statement in Java.

  • We can use this to decide whether a certain statement or block of statements will be executed or not.

  • Simply, if a condition is true then the block statement will be executed otherwise it is not.

Conditionals

In the example below, we are checking to see the given grade is enough to pass.

TEXT/X-JAVA
1public class Example {
2
3    public static void main(String[] args) {
4        int grade = 85;
5
6        //condition
7        if (grade > 50) {
8
9            System.out.println("You have passed the final exam!");
10        }
11
12    }
13}

The value of grade is 85. Since 85 is greater than 50, then the condition is true and we print the statement.

2) else/ if else/ statements

Combining an else and an if to make an else if statement creates a whole range of mutually exclusive possibilities.

Conditionals

In the example below, we are checking to see the age given is proper drinking age.

As you can see, the conditions in both the if and else if statements were false, so we automatically executed what is inside the else after checking.

JAVA
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment