Sharp Tutorial
loop in java

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

Programming in Java

  • Java Home
  • Introduction To Java
  • JDK, JRE, JVM
  • First Programm in Java
  • Data types and Operators
  • IF-ELSE, SWITCH-BREAK
  • WHILE, DO WHILE, FOR LOOP
  • Array In Java

Object Oriented in Java

  • Object and Class
  • Constructor in Java
  • Overloading in Java
  • Inheritance in Java
  • Overriding and java
  • super in Java
  • final in Java

Advanced topics in Java

  • Package in Java
  • Access Specifier in Java
  • Interface in Java
  • Exception Handling in Java
  • IO in Java
  • Applet in Java
  • String Handling
  • File Handling
  • Multithreading in Java
  • Awt and Events in Java
  • Collection in Java
  • GUI programming using Swing
  • Java Database Connectivity (JDBC)

loop in Java

Loop is used to iterate a block of code mulitple times (till the given condition is true) .

The looping statements are used to execute a single statement or block of statements repeatedly until the given condition is FALSE. So if i have to print myname 100 times, then instead of writing printf hundred times, we can use loop with condition that executes 100 times.

There are following types of commonly loop used in java.

1)while loop

2) do while loop

3) for loop

While loop syntax

while(<condition>)
{
// body part to be executed till the condtion is true
}

 

  class WhileLoopExample
  {
    public static void main(String args[])
	 {
	   int i=1;
	   while(i<=100)
	    {
		 System.out.println(i);
		 i++;
	    }
	 }
    }	

Above program prints the no 1-to 100 with the help of while loop.

Do While loop Syntax

In do while loop, body of the loop is executed first and after that, the condition is checked.
Do while loop executes at least once even if the condition is not true for a single time.

do
{
// body to be executed but executes at least once
}
while(<condtion>);
  class DoWhileLoopExample
  {
    public static void main(String args[])
	 {
	   int i=100;
	   do
	    {
		 System.out.println("Hello");
		 i++;
	    }
	   while(i<0);
	 }
  }	

Above program prints 1 time even if the condition is not true first time as well.

For loop syntax

In for loop, the first part begins with variable’s initialization, followed by the condition and increment/decrement operation on the variable.
The first part (initialization) and the last part (increment/decrement) may not be present, but the semicolon is there.
general syntax for for loop is

for(initialization; condition; increment/decrement)
{
//body part to be executed till the condition is true.
}
 class TablePrint
  {
    public static void main(String args[])
	 {
	   int n=4;
	   for (int i=1; i<=10; i++)
	    {
		 System.out.println(n*i);
	    }
	 }
  }	

Above program prints prints the table of the given no ie 4.

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial