- ####"break" is used to exit from the current loop. This is just like terminate that statement.
- breaks is used to end the loop whether condition is true or false.
####"return" statement is used to exit from the current function.
####"continue" is used to skip the current iteration of a loop and continue with the next iteration. isme us condition ko skip kr dega
package learning;
import java.util.Scanner;
public class learnNew {
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
for(int i = 1; i<=4; i++) {
if(i==2) {
continue;
}
System.out.println(i);
}
System.out.println("java is great");
}
}
This is continue statement
Yaha ham if i == 2 likha hai means jaise hi loop hamara i 2 pe aayega continue statement 2 ko skip krke 3 print krne ki kosish krega ya 3 me increment ho jayega .
1
3
4
java is great
Now I m giving you example of Break statement how u can use break statement on java and what will be the output for break statement.
package learning;
import java.util.Scanner;
public class learnNew {
public static void main(String[] args) { Scanner scan = new Scanner(System.in);
for(int i = 1; i<=4; i++) {
System.out.println(i);
break;
}
System.out.println("java is great");
}
}
1
java is great
Here you can see jaise hi i = 1 hua ye loop break kr diya gya break lg gya aur kaha gya ki mujhe ab is loop se koi mtlb nhi hai main ise break krke next iteration pe ja rha.
Comments
Post a Comment