If-s

We already saw that a program executes one command after another, from top to bottom.
But usually we need some branching/decisions to make while executing a program.
Let's write a program that will print if a number is positive or negative:

  • declare an integer x
  • input x
  • add If block with value x > 0
  • in the true branch output "Positive"
  • in the false branch output "Negative"

For example, if the user enters number 9, output should be "Positive".
If user enters number -55, output should be "Negative".

Maybe you haven't thought about it, but expression x > 0 has type Boolean.
Boolean can be true or false.


Congrats, you've come to the end of the Basic tutorial series.
Hope you have learned something useful! :)
Continue learning with Intermediate level tutorials.

6 / 6
 
id_b7d64145_9010_4897_96fb_04e8608872c5 Begin id_1547d1d2_5155_4265_8f83_9b4580dd91c1 End id_b7d64145_9010_4897_96fb_04e8608872c5:s->id_1547d1d2_5155_4265_8f83_9b4580dd91c1:n
public class NewProgram {

  public static void main(String args[]) {
  }
}