Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

For loop in Java language with example

Posted on December 5, 2016December 25, 2019

Table of Contents

  • For loop in Java language with example
  • Loops in Java
    • for loop
    • for loop flowchart
  • 1 4 9 16 25 36 49 64 81 100 Infinite for loop

For loop in Java language with example

In this article, we will discuss the For loop in Java language with example

Loops in Java

Java has three types of basic loops

  • for loop
  • while loop
  • Do-while loop
in this post, we will learn how to functioning for loop in java language

for loop

The for loop used to evaluate and execute the set of Java statements repeatedly, until the test condition is false of the java program.
The for loop executes program when the Boolean expression evaluates and the condition is satisfied.

for loop consist of three statements which generally use
  • initialization statement
  • boolean expression (true / false)
  • increment or decrement statement (+/_)

for loop flowchart

For loop in Java language with example
flow diagram
for loop syntex
   
syntex of for loop here
 
For loop in Java language with example
Syntax

 

Example of for loop

example of for loop here



Explanation of for loop

For loop in Java language with example
Explanation of for loop

 

  • The loop  begins with initialization part it is executed only once
  • Then check the test expression, boolean expression may return true/false when boolean expression is true, codes inside the body of for loop is executed.
  • Then control goes to increment part or decrement part and update statements is executed
  • Test expression of for loop evaluates on each iteration until the boolean expression returns false.
  • When the test expression is returned to false, for loop is terminated

for loop Example
Example 1


 this program display 1 to 10 natural number

class forloop{
public static void main(String args[]){

for(int x=0;x<=10; x++)
{
System.out.print(x); // print in same line because use print(x);
}
}

}

out put

At the above for loop, 
int x=0;  – initialization of variable
x<=10; – Boolean expression
x++ – increment expression

Example 2

This program display first ten square number, using for loop

Output

1
4
9
16
25
36
49
64
81
100

Infinite for loop

Output

If you use two semicolons (;;) in the for loop, it will be infinitive for loop


Output

This is infinite for loop
This is infinite for loop
This is infinite for loop
This is infinite for loop
This is infinite for loop
This is infinite for loop
This is infinite for loop

Ctr+c – control the flow





int x=0;  – initialization of variable
x>=0; – Boolean expression –  No limit to exit from loop
x++ – increment expression

Output
1
2
3
4
5
6
7
8
9
10
…
…
…Ctr+c- to exit from the flow

 

While Loop in Java      while Loop in C++     While Loop In C

Do-while Loop in Java    Do-while Loop C++     Do-while loop in C 

For Loop in C++       For Loop in C       

If condition C++       If condition in Java   If condition in C

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes