top of page

Conditionals

Conditional statements define conditions that are true or false and then execute based on whether or not the condition is true. Basically, conditions say, “If x is true, then execute y”. This logic is called an “if-statement”.

Java supports the usual logical conditions from mathematics:

​

  • Less than: a < b

  • Less than or equal to: a <= b

  • Greater than: a > b

  • Greater than or equal to: a >= b

  • Equal to a == b

  • Not Equal to: a != b

​

You can use these conditions to perform different actions for different decisions.

Java has the following conditional statements:

​

  • Use if to specify a block of code to be executed, if a specified condition is true

  • Use else to specify a block of code to be executed, if the same condition is false

  • Use else if to specify a new condition to test, if the first condition is false

If- statement Example

Capture.PNG
snipe1.PNG

 If you look at the word 'if' in the code you will see in the brackets if num1 ,which is 10, is less than num2, which is 20, the code will execute that is between the {}.

If-else Statement Example

wixc4.PNG

Here in this example above you will see that since time is greater than 18 the if statement is false so the code will print out 'good evening' because of the else statement. 

Capture.PNG
sipe2.PNG

Else If Statement Example

wixc5.PNG
Capture.PNG

In this code you can see that the code will print out "Good day", because time is greater than 10 but less than 20 so there for it will print "Good day".

bottom of page