Simple Uses Loops
In simple
words loop means repeatation, means when you want that particular block of code
run again and again, then for this you have to use Loops. Java support four
different types of Loops
·
For Loop
·
While Loop
·
Do While Loop
·
For Each Loop
For Loop
For loop is
also called counter loop, this will be executed until its country will become
complete.loops java
For (initialization; testing;
increment/decrement) {
// body of for
loop
}
According to
syntax for loop contain three parts
o Initialization: In this part you will initialize initial
value of loop, it indicates that from where loop will start You just
initialize first value.loops java
o Testing: In this part loop will check either condition is
true or false, if condition is true then control will go into body of loop and
then on third part
o Increment/Decrement: when body of loop execute after
testing condition will become true then control go into increment/decrement
part, this part either increment the counter or decrement the counter depends
upon operator which we use
Note: Initialization
part will execute only once in a program, after initialization part the testing
and increment/decrement parts will repeats until condition become false. loops java
While
loops java
While loop
will be executed until a condition is true, whenever condition become false it
will stop its execution
While (testing) {
Body of while
loop
}
While loop depends upon its testing condition, until
condition is true it will execute, when its condition become false it will stop
its execution.loops java
Do-while
loop is work same like while loop but do-while loop will execute once, either
condition is true or false.
do {
do while loop body
} while
(testing);
Break statement Loops java
Break
statement is used only in loops and switch. Break statement is
used for break the current flow at specified condition.loops java
Example: in this loop I gives an condition that if I
value equal to 5 then break the loop.
Continue
statement is used only in loops, its job is to continue the loop flow and skip
the current task
Example:in
this program I set a condition that when I equal to 5 then continue means when
I value become 5 it will skip it continue the loop.loops java
No comments:
Post a Comment