आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
Java - Loops and Statements

Introduction for Java Loops

एक ही statement को बार-बार repeat करना 'Looping' होता है |

Java में तीन प्रकार के Loops काम करते है |

  1. while Loop
  2. do_while Loop
  3. for Loop

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 :
//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++;
	}
	}
}
Output :
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 :
//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);
	}
}
Output :
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 के लिए दो प्रकार है |

  1. Normal for Loop
  2. 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 :
public class ForLoop{

    public static void main(String args[]){
	
	for( int i=0; i < 10; i++ ){
		
		System.out.println("Value of i is " + i);
	}
	}
}
Output :
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 :
//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);
	}
	
	}
}
Output :
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