Java - Loops and Statements
Introduction for Java Loops
एक ही statement को बार-बार repeat करना 'Looping' होता है |
Java में तीन प्रकार के Loops काम करते है |
1. while Loop
while Loop जब तक condition true होती है तब तक loop repeat होता रहता है | अगर condition false होता है, तब Loop exit हो जाता है |


Syntax for while Loop
variable_initialization; while( condition ){ //statement(s); increment/decrement; }
Example for while Loop
Source Code :Output ://WhileLoop.java public class WhileLoop{ public static void main(String args[]) { int i = 0; while(i < 10){ System.out.println("Value of i is " + i); i++; } } }
Value of i is 0 Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4 Value of i is 5 Value of i is 6 Value of i is 7 Value of i is 8 Value of i is 9
2. do_while Loop
do_while ये Loop; while Loop के जैसा ही है, लेकिन ये loop अगर condition true होती है तब do_while loop चलता है और condition false होती है तब सिर्फ do का statement execute हो जाता है |


Syntax for do_while Loop
variable_initialization; do{ //statement(s); increment/decrement; } while( condition );
Example for do_while Loop
Source Code :Output ://DoWhileLoop.java public class DoWhileLoop{ public static void main(String args[]){ int i = 0; do{ System.out.println("Value of i is " + i); i++; } while(i < 10); } }
Value of i is 0 Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4 Value of i is 5 Value of i is 6 Value of i is 7 Value of i is 8 Value of i is 9
3. for Loop
Core Java में for Loop के लिए दो प्रकार है |
- Normal for Loop
- Enhanced or foreach Loop
3.1 Normal for Loop
for loop में दिए हुए condition तक statement repeat होता रहता है |


Syntax for for Loop
for( initialization; condition; increment/decrement ){ //statements; }
Example for for Loop
Source Code :Output :public class ForLoop{ public static void main(String args[]){ for( int i=0; i < 10; i++ ){ System.out.println("Value of i is " + i); } } }
Value of i is 0 Value of i is 1 Value of i is 2 Value of i is 3 Value of i is 4 Value of i is 5 Value of i is 6 Value of i is 7 Value of i is 8 Value of i is 9
3.2 Enhanced or foreach Loop
array के elements की value को print करने के लिए foreach का इस्तेमाल किया जाता है, लेकिन ये array के मामले में for loop से बेहतर रहता है |
Syntax for 'foreach' Loop
for(variable_name : array_name){ //statement(s); }
Example for foreach Loop
Source Code :Output ://ForandForeachLoop.java public class ForandForeachLoop{ public static void main(String args[]){ int[] arr = {10, 15, 20, 25, 30}; System.out.println("Using for Loop"); for( int i=0; i<arr.length; i++ ){ System.out.println("Value of arr is " + arr[i]); } System.out.println("\nUsing foreach Loop"); for(int a : arr){ System.out.println("Value of arr is " + a); } } }
Using for Loop Value of arr is 10 Value of arr is 15 Value of arr is 20 Value of arr is 25 Value of arr is 30 Using foreach Loop Value of arr is 10 Value of arr is 15 Value of arr is 20 Value of arr is 25 Value of arr is 30
Java Decision Making Statements
Decision Making Statements में एक से ज्यादा statements होते है |
Click to Decision Making Statements