Sample Coding Blocks

Java , C++ , Javascript , C, HTML, SQL, VHDL and more languages!

Menu Close

Author: Dnz

If-Else statements in Java

If-Else statements are very handy and easy, they are used just like in the English language "if the condition holds do the following else do the following". Simple as that.


public class IfElseInJava {

    public static void main(String[] args) {
        
        //Simple if-else Statements in java
        // WwW.Code-Blocks.OrG
        
        int three = 3;
        int seven = 7;
        
        int bignumber = 999;
        int smallnumber = 1;
        
        if (three > seven) // Condition
        {//If the condition holds, this block will be executed
             System.out.println("Can 3 be greater than 7?"); 
             System.out.println("You can add here some more lines if you wish."); 
        }
        else
        {//If the condition doesnt hold, this block will be executed
            System.out.println("Three is not greater than seven"); 
            System.out.println("You can add here some more lines if you wish."); 
        }
        
        System.out.println("\n\n");  // Lets leave some space between the sentences with new lines.
        
        // On the example below if condition is true;
        
        
        if (bignumber > smallnumber) // Condition
        {//If the condition holds, this block will be executed
             System.out.println("The big number 999 is greater than 1"); 
             System.out.println("This block will be printed because the condition holds."); 
        }
        else
        {//If the condition doesnt hold, this block will be executed
            System.out.println("Since the condition fails this line wont be executed"); 
            System.out.println("You can add here some more lines if you wish."); 
        }    
        
        System.out.println("\n\n");  // Lets leave some space between the sentences with new lines.
        
        // If condition without curly brackets
        
        if (bignumber < smallnumber)
            System.out.println("If Statement without curly bracket"); // Belongs to if statement, since the if condition doesnt hold, the line will not be printed.
            System.out.println("This line does not belong to if statement, only the line right after the if statement WITHOUT curly brackets belongs to if statement."); // Important
            

    }

}

Below is the output of above code:

Three is not greater than seven
You can add here some more lines if you wish.

The big number 999 is greater than 1
This block will be printed because the condition holds.

This line does not belong to if statement, only the line right after the if statement WITHOUT curly brackets belongs to if statement.

Simple for loop in Java

A for loop in java consists of 3 parts, 1) starting value, 2) the limit, 3)Iteration; doesnt have to be necessarily iteration, can also decrement, multiply, add or any other condition you need. More advanced examples will be given on the next tutorials.

Simple for loop in java

Simple for loop in java


public class SimpleForLoop {

    public static void main(String[] args) {
        
        //Simple Sample for loops in java
        // WwW.Code-Blocks.OrG
        
        for (int i=0; i<10; i++)
        System.out.println("This is the value of i " + i);    
        System.out.println("This line doesn't belong to for loop ");
        
        System.out.println("Lets leave some space between the for loops with backslash n \n \n \n  "); // "\n" is an Escape Sequence for new line
        
        for (int z=0; z<5; z++){
            System.out.println("This is the value of z " + z);    
            System.out.println("This line belongs to for loop with z, because it is inside the loop brackets");    
        }

    }

}

Below is the output of above code:

This is the value of i 0
This is the value of i 1
This is the value of i 2
This is the value of i 3
This is the value of i 4
This is the value of i 5
This is the value of i 6
This is the value of i 7
This is the value of i 8
This is the value of i 9
This line doesn’t belong to for loop
Lets leave some space between the for loops with backslash n

This is the value of z 0
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 1
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 2
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 3
This line belongs to for loop with z, because it is inside the loop brackets
This is the value of z 4
This line belongs to for loop with z, because it is inside the loop brackets

© 2018 Sample Coding Blocks. All rights reserved.

Theme by Anders Norén.