‘break;’ and ‘continue;’ statements
Hey guys, we are back with
another blog and in this we will discuss about the two keywords that are, break
and continue in java. Happy reading.
‘break’ KEYWORD
Suppose you are
listening to a sad song on a loop for some reason but suddenly your phone rings and you get to know that you
have been selected for the job you applied for, what will you do? Perhaps you
will stop listening to that song and listen to some other song which will give
you an adrenaline rush. So the break statements sort off do the same thing,
they break out of your current loop and jump to the next line of the code (or
to the next iteration) just after the loop (containing ‘break’).
Ex:
public class Coding_Winds {
public static void main(String[] args) {
for(int i=1; i<=10;
i++) {
if(i==6)
break;
System.out.println(i);
}
}
}
Output
‘continue’ KEYWORD
Suppose you are already listening to some motivational pop music in
high volume on loop and suddenly you get a call that you are selected for the
job you applied for, what will you do? Possibly you will keep listening to that
song (maybe this time with more feel). So the keyword continue does the same
thing it continues the loop even when a certain condition comes in your code.
Ex:
public class Coding_Winds {
public static void main(String[] args) {
for(int i=1; i<=10;
i++) {
if(i==6)
continue;
System.out.println(i);
}
}
}
Output
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