Sunday, 2 December 2018

looping statement in java | different types of looping statement in java | what is looping statement in java with example - Online Help

Looping statement in java :            

              Guys in this post we are going to discuss looping statement in java. The process of repeatedly executing statements and is called looping. The statements may be executed multiple times(from zero to infinite numbers).
             If a loop executing continues then it is called an infinite loop. Looping is also called as iterations. There are three types of looping statement. They are:
- while loop statement
- do-while loop statement
- for loop statement


while loop statement:

The while loop is the entry controlled loop statement. The condition is evaluated, if the condition is true then the block of statements or statement block is executed otherwise the block of statement is not executed. The general format is

while(test condition)
{
//body of the loop
}
next statement;

- The computer first evaluates the loop condition.
- If the value is true, then the body of the loop is executed repeatedly until the loop condition becomes false.
- If the value is false, then the control is transferred to the next statement.


Example:
import java.util.*;
class whiledemo
{
public static void main(String args[])
{
int i;
while(i<=5)
{
System.out.println("welcome to this blog");
i++;
}
}
}

The output is:
welcome to this blog
welcome to this blog
welcome to this blog
welcome to this blog
welcome to this blog

do-while loop statement:

In the do-while loop, the first attempt to loop should be executed then it checks the condition. The benefit of a do-while loop/statement is that we get an entry in the loop and then the condition will check for the very first time. In while loop condition will check first and if the condition will not be satisfied then the loop will not be executed. The general format is:

do
{
//body of the loop
}
while(test condition);
next statement;

- checks the test condition
- If it is true, executes the body of the loop
- If it is false, executes the next statement.


Program:
class dowhiledemo
{
public static void main(String args[])
{
int i;
do
{
System.out.println("welocme to this blog");
}
while(i<=5);
}
}


The output is:
welcome to this blog
welcome to this blog
welcome to this blog
welcome to this blog
welcome to this blog

for loop statement:

for statement is used to execute a statement or a group of statement repeatedly for a number of times. The general format is:

for(initialization;test condition; increment/decrement)
{
//body of the loop
}
next statement;

- Give the initial value to control the variable.
- Checks the test condition, if the test condition is true, execute the body of the loop.
- if the test condition is false, then it executes the next statement.



Program:
class fordemo
{
public static void main(String args[])
{
int i;
for(i=0;i<=5;i++)
{
System.output.println("welcome to this blog");
}
}
}

The output is:
welcome to this blog
welcome to this blog
welcome to this blog
welcome to this blog
welcome to this blog

Guys, I hope you  able to understand this looping statement,  you guys want to know about decision-making statement, then click the given link below:
https://beingcomputeracy.blogspot.com/2018/12/decision-making-statement-in-java.html


No comments:

Post a Comment