JavaScript Control Statements In Hindi
if(num%2==0) { document.write(num,"is even"); } |
Types of Control Statements
- If
- If-Else
- Nested-If
- Switch case
- For
- Do-While
- While
- Break
- Go to
Selection Statements
if Statement
if(5>3) { document.write("This will be displayed"); } |
Example 2
if(3>5) { document.write("This will not be displayed"); } |
If else
if(10>15) { document.write("This will not be displayed"); } else { document.write("This will be displayed"); } |
Else If
Else if statements के द्वारा आप एक से अधिक conditions को check कर सकते है और सभी condition के false होने पर else part को execute करवा सकते है।
इसका general syntax निचे दिया जा रहा है।
if(condition 1) { // Will be executed if above condition is true } elseif(condition 2) { // Will be executed if 1st condition is false and this condition is true. } .... .... .... else if(condition N) { // Will be executed if all the conditions above it were false and this condition is true. } else { // Will be executed if all the above conditions are false } |
if(5>7) { document.write("This will not be executed!"); } elseif(5>6) { document.write("This will not be executed!"); } else { document.write("This will be executed!"); } |
Nested If
if(condition) { if(condition) { // Statement to be executed } } else { // Statements to be executed } |
जैसा की आप ऊपर दिए गए syntax में देख सकते है एक if condition के अंदर दूसरी if condition define की गयी है। आप चाहते तो nested if में else part भी add कर सकते है। आइये अब इसे एक उदाहरण से समझने का प्रयास करते है।
if(5>3) { if(5>6) { document.write("This will not be executed"); } else { document.write("5 is greater than 3 but not 6"); } } else { document.write("5 is not greater than 3"); } |
Switch Case
इसका उदाहरण नीचे दिया जा रहा है।
var ch=2; // Passing choice to execute desired case switch(ch) { case 1: document.write("ONE"); break; case 2: document.write("TWO"); break; case 3: document.write("THREE"); break; default: document.write("Enter appropriate value"); break; } |
Looping Statements
While Loop
var num = 0; // While loop iterating until num is less than 5 while(num <5) { document.write("Hello"); num++; } |
इसलिए इस situation से बचने के लिए किसी भी प्रकार के loop में loop control variable को increment किया जाता है।
Do-While Loop
आइये इसे एक उदाहरण से समझते है।
var num=0; // Do-while loop do { document.write("hello"); num++; } while(num<5); |
For Loop
सभी loops में for loop सबसे easy और सबसे ज्यादा यूज़ किया जाने वाला loop है। इसमें आप single line में ही पुरे loop को define कर देते है। यदि condition true होती है तो block में दिए गए statements execute हो जाते है। इस loop का उदाहरण नीचे दिया गया है।// For loop running until i is less than 5 for(var i=0;i<5;i++) { document.write("This will be printed until condition is true"); } |
For loop में condition और increment दोनों एक साथ ही define किये जाते है। साथ ही इसमें loop control variable भी define किया जाता है। Condition के false होते ही loop terminate हो जाता है।
Jump Statements
Continue
for(var i=0; i<5;i++) { if(i==2) { // Skipping third iteration of loop continue; } document.write("This will be displayed in iterations except 3rd"); } |
Continue statement का यूज़ करने से compiler 3rd iteration को skip कर देगा और कोई भी statement execute नहीं किया जायेगा। इसके बाद next iteration शुरू हो जायेगी।
Break
for(var i=0;i<5;i++) { if(i==2) { // Breaking 3rd iteration of loop break; } document.write("This will be displayed 2 times only"); } |
उपर दिए गए उदाहरण में जैसे ही loop की 3rd iteration आती है तो break statement के द्वारा loop terminate हो जाता है और program का execution loop के बाहर से शुरू हो जाता है।