Java conditions – code will be executed based on a condition true or false (if else)
Java loops – Code will be executed no. of times until a condition is false.(while, do while, for loop).
Watch details here
Code samples –
if else
public class IfElseCond { public static void main(String[] args) { int a = -10; if(a > 0) { System.out.println("a is a +ve number"); }else if(a < 0) { System.out.println("a is a -ve number"); } else if(a == 0){ System.out.println("value of a is "+ a); } System.out.println("I execute always"); } }
while / do while
public class WhileLoop { public static void main(String[] args) { int num = 10; /*while(num == 1) { System.out.println(num); num--; }*/ do { System.out.println(num); num--; }while(num == 1); } }
for loop
public class ForLoop { public static void main(String[] args) { /*int i =0; for(; i <= 10 ; i++) { System.out.println(i); }*/ int a[] = {1, 2, 60, -2, 0}; for(int i : a) { System.out.println(i); } } }