Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry Facebook connect
  • Androidberry twitter connect
  • Androidberry Google Plus connect

Java Tutorials


Basic Introduction

What is Java?
Java Features
Java vs Other Languages
Java on Internet
Java Components
Java First Program
Java Commandline argument
Java Constants
Java Variables
Java Types of Variables
Java Datatypes
Java Getting Input from User
Java Typecasting
Java Operators
Java Math Functions

Decision Making

Java if statement
Java if-else statement
Java nested if-else
Java else-if ladder
Java Switch case statement
Java Nested Switch Case
Java Ternary/Conditional operator

Looping

Java while loop
Java do-while loop
Java for loop
Java Enhanced loop/for each loop
Java Labelled for loop
Java Nesting of loops
Java Break and Continue

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Interfaces

Packages

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java For Loop


For Loop

The for is a entry-controlled loop that provides a more short loop control structure that other loops.

For loop is the most popular looping instruction due to it's simple instruction.

Syntax

for (initialization; test-condition; increment/decrement)
{
    // body of for loop
    ....
    ....
    ....
}

Flowchart

For Loop Flowchart

Syntax Explanation

The execution of the for statement is performed based on following 3 steps.

  1. Initialization
  2. Test-condition
  3. Increment/Decrement

1. Initialization

  • Before entering into the for loop,first the initialization of loop control variables is done using assignment operator.
  • Initialization defines from where the loop should be started to execute.
  • Initialization is only performed before entering into the loop and never again.
  • We can write multiple Initialization statements seperated by commas.

2. Test-condition

  • Test-condition decides when the loop will be terminated.
  • If the result of the test-condition is true,then the body of the loop is executed.

3. Increment/Decrement

  • This statement allows the loop to repeat by Incrementing or Decrementing the value.
  • Increment/Decrement is performed after checking for the condition in the for loop.
  • We can also write multiple Increment/Decrement statements seperated by commas.

Example - Printing the the table of Given Number using for loop

class ForLoopEx
{
   public static void main(String args[])
   {
      int i=0,num=5;
      for(i=1;i<=10;i++)
      {
         System.out.println(i * num);
      }
   }
}

Output

5
10
15
20
25
30
35
40
45
50

Different ways to write For Loop


1. Without writting initialization statement

We can write for loop without writting initialization statement. But semicolon is necessary in the condition.

Loop will execute just like above example.

int i=0,num=5;
for( ; i<=10; i++)
{
    System.out.println(i * num);
}

Output: table of 5

2. Without writting Initialization and Increment/Decrement statement

We can also write the for loop without using Initialization and Increment/Decrement statement.But semicolon is necessary in both the conditions.

We can provide Increment/Decrement inside the body of the loop.

int i=0,num=5;
for( ; i<=10; )
{
    System.out.println(i * num);
    i++;
}

Output: table of 5

3. Without writting anything inside the for loop.

We can also write the for loop without using any statement out of 3. This for loop will cause an infinite loop.

int i=0,num=5;
for( ; ; )
{
    System.out.println(i * num);
    i++;
}   

Output: table of 5 to Inifinite numbers

4. Multiple initializations inside the for loop

We can write more than one initializers inside the for loop.

int i=0,num=0;
for( i=1,num=5; i<=10 ;i++ )
{
    System.out.println(i * num);
}   

Output: table of 5

5. Multiple initializations and Increments inside the for loop

We can also write more than one initializations and Increments inside the for loop.

int i=0,num=0,result=0;

for( i=1,num=5,result=num ; i<=10 ; i++,result=i*num)
{
    System.out.println(result);
}

Output: table of 5


Best Examples for Practice
  • Check number is Prime or Not program
  • Check number is Palidrome or Not program
  • Check character is Vowel or Consonent program
  • Find Factorial of Number program
  • Find nth Power of any Number program
  • Print Multiplication table of Any Number program
  • Count total Number of Digits in the given Number program
  • Print Any Number in Reverse Order program
  • Convert given Decimal Number into Binary Number
  • Convert given Number into Words form
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo