Day 22 Of Our Month Of Java

Words
408
Reading
2 min
Listen
Play
8y

Revisiting Conditionals In Java

code-coding-computer-879109.jpg

Its now day 22 of our "Month of Java" and even though we went through conditional statements earlier in the month, We are now working through these types of statements again in CodeGym so I thought it would be a good time to go through them clarifying any issues I had the first time around. We use conditional statements in any programming language so our programs can adapt and change to different circomstances that come up while it is being run.

What Did I Learn Today


When it comes to conditional statements in Java, the first thing you will think of is the If Else statement. This is pretty common through most programming languages and I expected these to be a little be more strict when it comes to typing the syntax but in Java, they are quite simple. As we discussed earlier in the month the If Else statement consists of three main parts where a condition is tested, "If" the condition is tested as true then the first comand statement is run. An If Else statement can finish at this point as it does not need to run anything else afterwards. The If Else code block can simply end there and the rest of the program is then run.

An "Else" statement is usually in place to then catch any conditions, if the first set of conditions are tested as false. the "Else" then provides the program with a different set of options or code to run, if the initial condition is tested as false. If we print that out in its basic form, we can see an If Else statement as the one below:

if (condition)
    command_1;
else
    command_2;

As you can see the syntax is pretty basic and does not include too many brackets or semi colons, etc. So there is no reason for you to not use them in your code. We can also test for numerous conditions by adding in an Else If to the code to test further if our first condition is tested as false. We can add in as many Else If statements as we want to.

Code For The Day

  1 public class IfElseIfExample {
  2         public static void main(String args[]) {
  3                 int num = 123;
  4                 if( num < 100 && num >= 1 ) {
  5                         System.out.println("Number is a two digit number");
  6                 }
  7                 else if( num < 1000 && num >= 100 ) {
  8                         System.out.println("Number is a three digit number");
  9                 }
 10                 else {
 11                         System.out.println("Number is not between 1 & 999");
 12                 }
 13         }
 14 }

Output

java IfElseIfExample
Number is a three digit number

I'll be posting daily, sharing my experiences on my “1 Month of Java Code” experiences”, my previous post on day 20 can be found below, so feel free to have a look:
https://steemit.com/java/@run.vince.run/musings-of-my-month-of-java-progress

If you have found this post useful or interesting, please consider Commenting, Upvoting, Following and/or Resteeming

Day 22 Of Our Month Of Java | Ecency