I’ve Made It To Day 6 Of My Month Of Java

in #java6 years ago

Rejoice...I've Made It To Day Six!

ai-close-up-code-160107.jpg
Image courtesy of pexels.com

Learning the basics, is just that...its learning the basics. How far will this actually get me when it comes to looking at real work examples and this is where I have gone wrong on so many occasions. I am looking around for my next options to take my month of Java to the next level and will hopefully have a cool announcement in the next few days.

What Did I Learn Today


If Else Statements in Java: We use them to control flow based on a condition. Generally if the condition is evaluated as true, the statement of code is then run. In Java there are five types of If control statements:

  1. If Statements: An if statement consists of a condition, followed by a statement or set of statements which will only get executed if the condition is true.

  2. Nested If Statements: Sometimes you need to check another condition after your if statement, this is where you may need to nest an if statement inside another if statement. Once again the condition needs to be evaluated as true for the statement and nested if statement to be run.

  3. If, Else Statements: The statement inside the if would execute if the condition is true and the statement inside else would execute if the condition is false. This means we will have an outcome weather our “If Statement” is true or false.

  4. If, else if, else statements: If you need to check for multiple conditions, we can use the if, else if statements to use multiple conditions. In this type of statement, the first “If Statement” is tested to see if it is true, if it is false, the code then drops down to a “Else If Statement” to check if this is true or false. These types of conditional statements will also include an “Else” at the end to cover anything not previously included in the earlier conditionals.

  5. Switch Statement: You can imagine that if you need to have a heap of “If, Else If Statements” it could get a little messy. this is where switch statements come in as it allows you to put into a neater format your set of conditions. The case statement needs to match the result of the switch expression and must be a constant value. The case value needs to match what you are looking for. A default case is set if none of the other conditions are met. Below is an example of a switch statement:

switch(num)
{
    case 1:
        // Something happens
        break;
    case 2:
        // Something happens
        Break;
    Default:
        // Something happens if all else fails
    }
}

Code For The Day...There's Lots


Example If Statement

  1 public class IfStatementExample {
  2         public static void main(String args[]) {
  3                 int num = 70;
  4                 if( num < 100) {
  5                         System.out.println("Number is less than 100");
  6                 }
  7         }
  8 }

Output:

java IfStatementExample 
Number is less than 100

Example of a Nested If Statement

  1 public class NestedIfExample {
  2         public static void main(String args[]) {
  3                 int num = 70;
  4                 if( num < 100 ) {
  5                         System.out.println("Number is less than 100");
  6                         if( num > 50 ) {
  7                                 System.out.println("Number is also greater than 50");
  8                         }
  9                 }
 10         }
 11 }

Output:

java NestedIfExample 
Number is less than 100
Number is also greater than 50

Example of an If Else Statement

  1 public class IfElseExample {
  2         public static void main(String args[]) {
  3                 int num = 70;
  4                 if( num < 50 ) {
  5                         System.out.println("Number is less than 50");
  6                 }
  7                 else {
  8                         System.out.println("Number is equal to or greater than 50");
  9                 }
 10         }
 11 }

Output:

java IfElseExample 
Number is equal to or greater than 50

Example If, Else If Statement

  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

Example Switch Statement

  1 public class SwitchCaseExample {
  2         public static void main(String args[]) {
  3                 int num = 2;
  4                 switch(num)
  5                 {
  6                         case 1:
  7                                 System.out.println("Case1: Value is: " + num);
  8                                 break;
  9                         case 2:
 10                                 System.out.println("Case2: Value is: " + num);
 11                                 break;
 12                         default:
 13                                 System.out.println("Default: Value is: " + num);
 14                 }
 15         }
 16 }

Output:

java SwitchCaseExample 
Default: Value is: 2

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

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

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by run.vince.run from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

A very strong post, my friend, a good job really

Posted using Partiko Android

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 62345.27
ETH 2427.57
USDT 1.00
SBD 2.49