Friday, 30 December 2016

While Loop In Java Example

package com.kt4study.corejava.loop;

import java.util.ArrayList;
import java.util.Iterator;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class WhileLoopExample {

  public static void whileLoopExampleDemo() {
    
    //While Loop Type 1
    int i = 1;
    // Check condition
    while (i <= 3) {
      System.out.println("Current value Of 'i' is :: " + i);
      i++;
    }
    
    //While Loop Type 2 forArrayList Object 
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("Java Guider");
    arrayList.add("Kt4Study.blogspot.co.uk");
    // Get list of ArrayList elements to iterate
    Iterator<String> iterator = arrayList.iterator();

    // Check condition
    while (iterator.hasNext()) {
      System.out.println("Iterated element from while loop :: " + iterator.next());
    }

  }

  public static void main(String[] args) {
    WhileLoopExample.whileLoopExampleDemo();
  }
}

Output →

Current value Of 'i' is :: 1
Current value Of 'i' is :: 2
Current value Of 'i' is :: 3
Iterated element from while loop :: Java Guider
Iterated element from while loop :: Kt4Study.blogspot.co.uk

For Loop In Java Example

package com.kt4study.corejava.loop;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class ForLoopExample {
  
  public static void  forLoopExampleDemo(){
    
    //initialization, condition check, Increment/Decrement
    for(int i = 0; i<4; i++){
      System.out.println("Current value of 'i' is :: " + i);
    }
  }
  
  public static void main(String[] args) {
    ForLoopExample.forLoopExampleDemo();
  }
}

Output →

Current value of 'i' is :: 0
Current value of 'i' is :: 1
Current value of 'i' is :: 2
Current value of 'i' is :: 3

For Each Loop In Java Example

package com.kt4study.corejava.loop;

import java.util.ArrayList;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class ForEachLoopExample {
  
  public static void forEachDemo(){
    
    //Iterate ArrayList with For Each Loop
    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("Hello World");
    arrayList.add("kt4study.blogspot.co.uk");
    for (String element : arrayList) {
      System.out.println("Element from ArrayList :: " + element);
    }
    
    //Iterate One Directional Array with For Each Loop
    int intArray[] 156};
    for (int element : intArray) {
      System.out.println("Element from OneDirectionalArray :: "+  element);
    }
    
    //Iterate Multi Directional Array with For Each Loop
    int[][] intMultiArray = new int[2][2];
    intMultiArray[0][010;
    intMultiArray[0][112;
    intMultiArray[1][014;
    intMultiArray[1][116;
    
    for (int[] array : intMultiArray) {
      for (int element: array) {
        System.out.println("Element from MultiDirectionalArray :: " + element);
        }
    }
  }
  
  public static void main(String[] args) {
    ForEachLoopExample.forEachDemo();
  }

}

Output →

Element from ArrayList :: Hello World
Element from ArrayList :: kt4study.blogspot.co.uk
Element from OneDirectionalArray :: 15
Element from OneDirectionalArray :: 6
Element from OneDirectionalArray :: 4
Element from MultiDirectionalArray :: 10
Element from MultiDirectionalArray :: 12
Element from MultiDirectionalArray :: 14
Element from MultiDirectionalArray :: 16

Do While Loop In Java Example

package com.kt4study.corejava.loop;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class DoWhileLoopExample {

  public static void doWhileLoopExampleDemo() {

    int i = 1;
    do{
      System.out.println("Value Of 'i' :: " + i);
      i++;
    }while(i <4)// Post condition check
    
  }

  public static void main(String[] args) {
    DoWhileLoopExample.doWhileLoopExampleDemo();
  }
}

Output →

Current value Of 'i' is :: 1
Current value Of 'i' is :: 2
Current value Of 'i' is :: 3

Ternary Operator In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class TernaryOperatorExample {
  
  public static void ternaryOperator(){
    int a = 10;
    int b = 5;
    int result;
    
    // ?: Ternary Operator Example
    result = ((a+b== 151000 2000 ;
    System.out.println("Result  :: " + result);
    
  }

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

/*Output -->
Result  :: 1000
*/
Output →

Result :: 1000

Relational Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class RelationalOperatorsExample {

  public static void relationalOperators(int a, int b) {

    boolean result;

    // == Equal To example
    result = (a == b);
    System.out.println("Does 'a' equal to 'b' ::" + result);

    // != Not Equal To example
    result = (a != b);
    System.out.println("Does 'a' not equal to 'b' ::" + result);

    // > Greater Than example
    result = (a > b);
    System.out.println("Does 'a' greater than 'b' ::" + result);

    // < Less Than example
    result = (a < b);
    System.out.println("Does 'a' less than 'b' ::" + result);

    // > Greater Than Or Equal To example
    result = (a >= b);
    System.out.println("Does 'a' greater than or equal to 'b' ::" + result);

    // < Less Than Or Equal To example
    result = (a <= b);
    System.out.println("Does 'a' less than or equal to 'b' ::" + result);

  }

  public static void main(String[] args) {
    RelationalOperatorsExample.relationalOperators(2015);
  }
}
Output →

Does 'a' equal to 'b' ::false
Does 'a' not equal to 'b' ::true
Does 'a' greater than 'b' ::true
Does 'a' less than 'b' ::false
Does 'a' greater than or equal to 'b' ::true
Does 'a' less than or equal to 'b' ::false

Logical Operators In Java Example

package com.kt4study.corejava.operators;

/**
 @author kt4study.blogspot.co.uk
 
 */

public class LogicalOperatorsExample {
  
  public static void logicalOperators() {
    
    boolean a = true;
    boolean b = false;
    boolean result;
    
    // && Logical AND example
    result = (a && b);
    System.out.println("Logical AND  (a && b) :: " + result);
    
    // || Logical OR example
    result = (a || b);
    System.out.println("Logical OR  (a || b) :: " + result);
        
    // ! Logical Not example
    result = !(a || b);
    System.out.println("Logical NOT  !(a || b) :: " + result);
  }

  public static void main(String[] args) {
    LogicalOperatorsExample.logicalOperators();
  }

}

Output →

Logical AND (a && b) :: false
Logical OR (a || b) :: true
Logical NOT !(a || b) :: false