Skip to main content

Java for loop

Java for loop






Java for loop is used to repeat execution of the statement(s) until a certain condition holds true. for is a keyword in Java programming language.

Java for loop syntax

  1. for (/* Initialization of variables */ ; /*Conditions to test*/ ; /* Increment(s) or decrement(s) of variables */){
  2.   // Statements to execute, i.e., Body of a for loop
  3. }
You can initialize multiple variables, test many conditions and perform increments or decrements on many variables according to requirement. Please note that all three components of a for loop are optional. For example, following for loop prints "Java programming" indefinitely.
  1. // Infinite for loop
  2. for ( ; ; ) {
  3.   System.out.println("Java programming");
  4. }
You can terminate an infinite loop by pressing Ctrl+C.

Simple for loop example in Java

Example program below uses for loop to print first 10 natural numbers i.e. from 1 to 10.
  1. //Java for loop program
  2. class ForLoop {
  3.   public static void main(String[] args) {
  4.     int c;
  5.    
  6.     for (= 1; c <= 10; c++) {
  7.       System.out.println(c);
  8.     }
  9.   }
  10. }
Output of program:

Java for loop example to print stars in console

Following star pattern is printed
*
**
***
****
*****
  1. class Stars {
  2.   public static void main(String[] args) {
  3.     int row, numberOfStars;
  4.    
  5.     for (row = 1; row <= 10; row++) {
  6.       for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
  7.         System.out.print("*");
  8.       }
  9.       System.out.println(); // Go to next line
  10.     }
  11.   }
  12. }
This program uses nested for loops i.e. for loop inside a for loop to print the pattern of stars. You can also use spaces to create another pattern, it is left for you as an exercise.
Output of program:

Comments

Popular posts from this blog

Java program to print multiplication table

Java program to print multiplication table of a number entered by a user using a  for  loop. You can modify it for  while  or  do while  loop for practice. Using nested loops (a loop inside another loop), we can print tables of numbers between a given range say a to b, for example, if the input numbers are '3' and '6' then tables of '3', '4', '5' and '6' will be printed. Java programming source code import   java.util.Scanner ;   class   MultiplicationTable {     public   static   void   main ( String   args [ ] )     {       int   n, c ;       System . out . println ( "Enter an integer to print it's multiplication table" ) ;     Scanner in   =   new   Scanner ( System . in ) ;     n   =   in. nextInt ( ) ;       System . out . println ( "Multiplication table of "   + ...

Java while loop

Java while loop Java while loop is used to execute statement(s) until a condition is true. In this tutorial, we will learn to use while loop with examples. First of all, let's discuss while loop syntax: while (condition(s)) { // Body of loop } 1. If the condition(s) holds true then the body of the loop is executed, after execution of the loop body condition is tested again and if the condition is still true then the body of the loop is executed again, and the process repeats until the condition(s) becomes false. The condition evaluates to true or false and if it is a constant, for example, while (c) {…}, where c is a constant, then any non zero value of c is considered to be true, and zero is considered false. 2. You can test multiple conditions such as while   ( a  >  b  &&  c  !=   0 )   {   // Loop body } Loop body is executed till value of variable a is greater than value of...

Install & Use Gujrati Indic Input Font | સરળ છે ગુજરાતી ઈન્ડિક ઇન્પુટ (શ...