LOOPS
What happens when
you want to listen to a song on repeat ? Yes, you loop that particular song,
maybe until your mood for that song changes or maybe until you are bored of
listening to that same song. So loops in a programming languages aren’t
different, they loop a task which you want them to perform under certain
condition, and terminate after that condition becomes false. There are
basically three types of loops in java;
i) For loops
ii) While loops
iii) do while
loops
For loops
‘For loop’ are quite
common and efficient loops ,since they allow you to fully control them. The
basic syntax of the ‘for loops’ is given below:
Syntax:
for(initialisation; condition; increment/decrement){
//loop body
}
In the above
syntax of the for loops we can see that, in the round brackets present just
after the keyword ‘for’ we can initialise a variable, then just after
initialisation after a semicolon we can set the condition until which our loop
will run. And finally after the condition, after a semicolon we can set the
variable to increment or decrement its value each times after it executes
throughout the body with the prior value of the variable. The example below
shows us the working of a loop.
for(int i=1; i<11;
i++) {
System.out.println("2
X "+i+"="+2*i);
}
Output screen :
While loops
While loops are
the most basic loops as they allow you to run your statements until a condition
becomes false. They first check the condition , if it’s true they enter the
loop body, they terminate as soon the condition becomes false. This loop starts
with the keyword ‘while’ followed by the condition inside the round brackets.
The basic syntax for the while loops can be seen below:
Syntax:
while(condition){
//loop body
}
Though the syntax
doesn’t mention any variable initialisation but any externally initialised
variable value can be incremented or decremented at the end of the while loop
manually. The example below illustrates its working in detail
Ex:
int i=1;
while(i<11){
System.out.println("2
X "+i+"="+2*i);
i++;
}
Output
Screen :
Do while loops
What if you wish
to perform a certain task and then check a condition. Well for this we have ‘do-while
loops’ , in do-while loop we can perform a certain task and at the end of the
loop body we can check the condition. These loops are written by using the keyword
‘do’ followed by the loop body and then the condition after the keyword ‘while’
inside the round brackets, with semicolon as a suffix. The basic syntax is
given below
Syntax:
do{
}while(condition);
Remember that in
the do-while loops after writing the condition inside the round brackets we
have to put a semicolon. These are the only loops which have semicolon in their
syntax. Below is the example of the do-while loops.
int i=1;
do {
System.out.println("2
X "+i+"="+2*i);
i++;
}while(i<11);
Output Screen :
Hope you are clear on this topic do read our more articles on JAVA LANGUAGE.
If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".
This article is SUBMITTED By : Pranjal Rai
Do subscribe to our daily blog update by clicking here.
Thank You!
No comments:
Post a Comment