Looping Statements

Home   »    Java   »    Looping Statements


The iteration statements allows a set of instructions to be performed repeatedly until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. Java provides three kinds of loops : while loop, do-while loop, and for loop.



Looping Statements

A Computer is used for performing many Repetitive types of tasks The Process of Repeatedly performing tasks is known as looping The Statements in the block may be Executed any number of times from Zero to Up to the Condition is True The Loop is that in which a task is repeated until the condition is true or we can say in the loop will Executes all the statements are until the given condition is not to be false. These are generally used for repeating the statements. In this There is Either Entry Controlled loop or as Exit Controlled Loop We know that before Execution of Statements all Conditions are Checked these are Performed by Entry Controlled Loops Which First Checks Condition And in Exit Controlled Loop it Checks Condition for Ending Loop Whether given Condition is False or not if a Loop First Checks Condition For Execution then it is called as Entry Controlled Loop and if a Loop Checks Condition after the Execution of Statement then they are Called as Exit Controlled Loops.

In The loop generally there are three basic operations are performed
1) Initialization
2) Condition check
3) Increment

There are the three types of loops in the java
1) while
2) do-while
3) for

all these are used for performing the repetitive tasks until the given condition is not true.

While

While Loop is Known as Entry Controlled Loop because in The while loop first we initialize the value of variable or Starting point of Execution and then we check the condition and if the condition is true then it will execute the statements and then after it increments or decrements the value of a variable. But in the Will Condition is false then it will never Executes the Statement





Output of "WhileLoop.java"






Download Complete Program

  -  







Do while

This is Also Called as Exit Controlled Loop we know that in The while loop the condition is check before the execution of the program but if the condition is not true then it will not execute the statements so for this purpose we use the do while loop in this first it executes the statements and then it increments the value of a variable and then last it checks the condition So in this either the condition is true or not it Execute the statement at least one time.



Output of DoWhileLoop.java"


Download Complete Program

  -  



For Loop

In This loop all the basic operations like initialization ,condition checking and incrementing or decrementing all these are performed in only one line. this is similar to the while loop for performing its execution but only different in its syntax.



Output of "ForLoop.java"


Download Complete Program

  -  



Nesting of For Loops

A for loop may contain another loop in its body. This form of a loop is called nested loop. In a nested loop, the inner loop must terminate before the outer loop. The following is an example of a nested loop:





While working with nested loops, you need to understand one thing and that is, the value of outer loop variable will change only after the inner loop is completely finished (or interrted). Another Example of Nested For Loop...there the program will give below output:

1
22
333
4444
55555



Output of "NestedLoop.java"


Download Complete Program

  -  







Jumping in Loops

Loop will perfume the Execution of Statements until the condition is true but if wants to Executes of Statements is desired order then we have to use jumping statements Jumping Statements are also Called as Control Statements. The Control jumping Statements are used for controlling the Execution of the program there are the three control statements those are break , continue. And labelled loops


Break

This statement is used for stopping the execution of the program the break statement is used where we does t want to execute the next statements.



Output of "BreakStatement.java"


Download Complete Program

  -  



Continue

The Continue Statements is used for executing the statements by skipping a statement for ex if u don t want to execute any statement then we can use the continue means to skip a particular statement and then execute the next statements.



Output of "ContinueStatement.java"


Download Complete Program

  -  



Labelled Loops

These are Provided java Language and Labelled loops are used for giving a name to the loop which is also known as Label of Loop if you wants to give a name to Loop the Label Must be given before loop with a valid name and a Colon.





<< Previous Topic
Next Topic >>





2 comments: