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 program to print alphabets

Java program to print alphabets Java program to print alphabets on screen, i.e., a, b, c, ..., z; in lower case. Java source code class  Alphabets {     public   static   void  main ( String  args [ ] )     {        char  ch ;          for   ( ch  =   'a' ;  ch  <=   'z' ;  ch ++ )           System . out . println ( ch ) ;     } } You can easily modify the above java program to print alphabets in upper case. Download  Alphabets  program class file. Output of program: Printing alphabets using a while loop (Only the body of the main method is shown): char  c  =   'a' ;   while   ( c  <=   'z' )   {    System . out . println ( c ) ;   c ++; } Using a do while loop: char  c  =...

Use phone camera as a webcam without root in Hindi/urdu.