Posts

Showing posts from April, 2020

Complete Java masterclass for beginners -14 (break statement)

Image
Java Break Statement When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. The Java  break  statement is used to break loop or  switch  statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop. We can use Java break statement in all types of loops such as  for loop ,  while loop  and  do-while loop . Syntax: jump-statement ;         break; Java Break Statement with Loop Example: //Java Program to demonstrate the use of break statement    //inside the for loop.  public class BreakExample {     public static void main (String[] args) {         //using for loop          for ( int i= 1 ; i<= 10 ; i++)...

Complete Java masterclass for beginners -13 (do while loop)

Image
Java do-while Loop The Java  do-while loop  is used to iterate a part of the program several times. If the number of iteration is not fixed and you must have to execute the loop at least once, it is recommended to use do-while loop. The Java  do-while loop  is executed at least once because condition is checked after loop body. Syntax: do { //code to be executed          } while (condition) ; Example: public class DoWhileExample {     public static void main (String[] args) {         int i= 1 ;         do {             System. out .println(i) ;             i++ ;         } while (i<= 10 ) ;     } } Output: Java Infinitive do-while Loop ...

Complete Java masterclass for beginners -12 (while loop)

Image
Java While Loop The  Java   while loop  is used to iterate a part of the  program  several times. If the number of iteration is not fixed, it is recommended to use while  loop . Syntax: while (condition){ //code to be executed         } Example: public class WhileExample {     public static void main (String[] args) {         int i= 1 ;         while (i<= 10 ){             System.out.println(i) ;              i++ ;         }     } } Output: Java Infinitive While Loop If you pass  true  in the while loop, it will be infinitive while loop. Syntax: while ( true ){ //code to be executed     ...

Complete Java masterclass for beginners -11 (for loop)

Image
Loops in Java In programming languages, loops are used to execute a set of instructions/functions repeatedly when some conditions become true. There are three types of loops in Java. for loop while loop do-while loop Java For Loop vs While Loop vs Do While Loop Java For Loop The Java  for loop  is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop. There are three types of for loops in java. Simple For Loop For-each  or Enhanced For Loop Labeled For Loop Java Simple For Loop A simple for loop is the same as  C / C++ . We can initialize the  variable , check condition and increment/decrement value. It consists of four parts: Initialization : It is the initial condition which is executed once when the loop starts. Here, we can initialize the variable, or we can use an already initialized variable. It is an optional condition. Condition : It is the ...