Press enter to see results or esc to cancel.


java if statement source code-Simple Java Programs


In this video I have shown the four common different if statements with example source code.
The four if statements are,
1. if statement
2. if-else statement
3. if-else-if ladder
4 .nested if statement

  1. Project Structure
  2. If_Statement.java

    
    package com.chillyfacts.com;
    public class If_Statement {
    	public static void main(String[] args) {
    		int i=15;
    		System.out.println(i>10);
    		if(i>10){
    			System.out.println("i is greater than 10");
    		}
    	}
    }
    

    Output

    true
    i is greater than 10
    
  3. If_Else_Statement.java
    
    package com.chillyfacts.com;
    public class If_Else_Statement {
    	public static void main(String[] args) {
    		int i=15;
    		System.out.println(i>10);
    		if(i>10){
    			System.out.println("i is greater than 10");
    		}else{
    			System.out.println("i is less than 10");
    		}
    	}
    }
    

    Output

    true
    i is greater than 10
    
  4. If_Else_If_Statement.java
    
    package com.chillyfacts.com;
    public class If_Else_If_Statement {
    	public static void main(String[] args) {
    		int i=60;		
    		if(i<5){
    			System.out.println("i is less than 5");
    		}else if(i<10){
    			System.out.println("i is less than 10");
    		}else{
    			System.out.println("i is greater than 10");
    		}		
    	}
    }
    

    output

    i is greater than 10
    
  5. Nested_If_Statement
    
    package com.chillyfacts.com;
    public class Nested_If_Statement {
    	public static void main(String[] args) {
    		int i=16;
    		//System.out.println(i>10);
    		if(i<5){
    			System.out.println("i is less than 5");
    		}else if(i<10){
    			System.out.println("i is less than 10");
    		}else if(i<15){
    			System.out.println("i is less than 15");
    		}else if(i<20){
    			System.out.println("i is less than 20");
    		}else if(i<25){
    			System.out.println("i is less than 25");
    		}else{
    			System.out.println("i is greater than 25");
    		}
    		System.out.println("Done");
    	}
    }
  6. Output

    i is less than 20
    Done
    
  7. Download complete source code here

    If_Else.rar
    

Comments

Leave a Comment