CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

04 January, 2019

Introduction to JavaScript Control Statements

 Programing Coderfunda     January 04, 2019     JavaScript, JavaScript Control Statements     No comments   

JavaScript Control Statements In Hindi

Control statements program के flow को control करते है। जैसे की आप control statements की मदद से choose कर सकते है की आप कौनसा statement execute करवाना चाहते है और कौनसा नहीं करवाना चाहते है। Control statements की मदद से logic perform किया जाता है। 

उदाहरण के लिए आप 1 से लेकर 100 तक के numbers में सिर्फ even numbers print करवाना चाहते है। इस situation में आप control statements की मदद से पता कर सकते है की number 2 से पूरी तरह divide हो रहा है या नहीं। यदि number 2 से divide किया जा सकता है तो वह even है और उसे print कर दिया जाता है। 
if(num%2==0)
{
    document.write(num,"is even");
}
  
Control statements के बिना program में कोई logic perform नहीं किया जा सकता है। अगर दूसरे शब्दों में कहें तो आप choose कर सकते है की कौनसे statements किस situation में execute होंगे। और साथ ही आप control statements की मदद से एक statement को कई बार भी execute कर सकते है।     
   

Types of Control Statements 

Control statements को 3 categories में divide किया गया है। ये categories control statements के tasks को भी define करती है। इन categories के बारे में नीचे दिया जा रहा है। 
Selection statements - इस category के control statements program में statements को situation के according select करके execute करने के लिए यूज़ किये जाते है। इस category में नीचे दिए गए statements आते है। 
  • If
  • If-Else
  • Nested-If
  • Switch case   
Looping statements - इस तरह के statements program में particular statements को बार बार execute करने के लिए यूज़ किये जाते है। इस category के control statements नीचे दिए गए है। 
  • For 
  • Do-While
  • While 
Jump statements - इस तरह के statements program में एक जगह से दूसरी जगह jump करने के लिए यूज़ किये जाते है। इस category के statements नीचे दिए गए है। 
  • Break
  • Go to     

Selection Statements 

जैसा की मैने आपको पहले बताया selection statements logic के द्वारा कुछ particular statements को execute करते है। आइये अब selection statements को उदाहरण से समझने का प्रयास करते है।

if Statement 

If statement किसी condition को test करता है यदि condition true होती है तो brackets में दिए हुए statements execute कर दिए जाते है और यदि condition false है तो ये block skip कर दिया जाता है। 
Example 1
   
if(5>3)
{
    document.write("This will be displayed");
}
  
जैसा की आप ऊपर दिए हुए उदाहरण में देख रहे है दी हुई condition true है इसलिए brackets के अंदर का statement execute होगा। आइये इसका एक और उदाहरण देखते है।

Example 2

if(3>5)
{
    document.write("This will not be displayed");
}
   
इस उदाहरण में condition false है इसलिए brackets में दिया हुआ statement execute नहीं होगा।

If else 

If else statement भी if statement की तरह ही होता है। बस इसमें else part और add कर दिया जाता है। Else part में आप वो statements लिखते है जो condition false होने पर execute होने चाहिए। आइये इसका उदाहरण देखते है। 
 if(10>15)
{
   document.write("This will not be displayed");
}
else
{
   document.write("This will be displayed");
}

Else If

यदि आप चाहते है की एक condition के false होने पर else part को execute ना करके किसी दूसरी condition को check किया जाये तो इसके लिए आप else if statements use कर सकते है।

Else if statements के द्वारा आप एक से अधिक conditions को check कर सकते है और सभी condition के false होने पर else part को execute करवा सकते है। 
इसके लिए आप elseif keyword यूज़ करते है। First condition को normal if else statement की तरह execute किया जाता है। इसके अलावा आप जितनी भी conditions add करना चाहते है उन्हें if और else part के बीच elseif keyword के द्वारा डिफाइन करते है।

इसका 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 भी डाल सकते है। इसका structure निचे दिया जा रहा है।
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 

Switch case बिलकुल if statement की तरह होता है। लेकिन इसमें आप एक बार में कई conditions को check कर सकते है। Switch case में cases define किये जाते हैं। बाद में एक choice variable के द्वारा ये cases execute करवाए जाते है। Choice variable जिस case से match करता है वही case execute हो जाता है।

इसका उदाहरण नीचे दिया जा रहा है। 
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;
}  
     
जैसे की आप देख सकते है हर case के बाद में break statement यूज़ किया गया है। यदि आप break statement यूज़ नहीं करते है तो सभी cases one by one execute हो जाते है। इस उदाहरण में variable की value 2 है इसलिए second case execute होगा और TWO display हो जायेगा।

Looping Statements 

Looping statements particular statement को बार बार execute करने के लिए यूज़ किये जाते है। ये 3 प्रकार के होते है। इनके बारे में नीचे दिया जा रहा है।

While Loop 

इस loop में आप एक condition देते है जब तक condition true होती है block में दिए गए statements execute होते रहते है। Condition false होते ही loop terminate हो जाता है और program का execution continue रहता है। 
var num = 0;

// While loop iterating until num is less than 5
while(num <5)
{
    document.write("Hello");
    num++;
} 
इस उदाहरण में जब तक num 5 से कम है तब तक loop का block execute होगा। एक चीज़ यँहा पर notice करने की ये है की हर बार num को increment किया जा रहा है ताकि कुछ steps के बाद loop terminate हो जाये। यदि यँहा पर ऐसा नहीं किया जाये तो loop कभी terminate ही नहीं होगा infinite time तक चलेगा।

इसलिए इस situation से बचने के लिए किसी भी प्रकार के loop में loop control variable को increment किया जाता है।

Do-While Loop 

Do while loop भी while loop की तरह ही होता है। बस ये first time बिना condition check किये execute होता है और बाद में हर बार condition check करता है। यदि condition true होती है तो do block के statements execute कर दिए जाते है।

आइये इसे एक उदाहरण से समझते है।
var num=0;
// Do-while loop 
do
{
    document.write("hello");
    num++;
}
while(num<5);
    
जैसा की आप देख सकते है पहले do block execute होगा और उसके बाद condition check की जाएगी। इस loop की विशेषता ये है की चाहे condition true हो या false loop एक बार तो जरूर execute होगा। यदि condition true होती है तो loop further execute होता है नहीं तो terminate हो जाता है। 

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 

Jump statements program के execution को एक जगह से दूसरी जगह transfer करने के लिए यूज़ किये जाते है। इन statements को special cases में यूज़ किया जाता है। इनके बारे में नीचे दिया जा रहा है। 

Continue 

Continue statement के द्वारा आप किसी भी loop की कोई iteration skip कर सकते है। जैसे की आप चाहते है की 3rd iteration skip हो जाये और compiler कोई action ना ले। ऐसा आप निचे दिए हुए example की तरह कर सकते है।

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      

Break statement compiler के execution को stop करने के लिए यूज़ किया जाता है। Break statement आने पर compiler execution को उस block से बाहर ला देता है। इसको एक loop के example से आसानी से समझा जा सकता है। 
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 के बाहर से शुरू हो जाता है।
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Introduction to JavaScript Strings  JavaScript Strings JavaScript में strings objects होती है। एक string object में characters की sequence store की जाती है। जैसे की Best Hin… Read More
  • JavaScript Operators JavaScript Operators Variables में values सिर्फ उन्हें बाद में display करवाने के लिए ही नहीं बल्कि इसलिए भी store की जाती है  ताकि उनके साथ… Read More
  • JavaScript Variables Introduction to JavaScript Variables यदि आपको कोई value store करनी है तो इसके लिए आपको variable create करना होता है। ये concept सभी programming … Read More
  • Introduction to JavaScript Control Statements JavaScript Control Statements In HindiControl statements program के flow को control करते है। जैसे की आप control statements की मदद से choose कर सकते ह… Read More
  • Introduction to JavaScript ArraysJavaScript Arrays in HindiArrays एक ही तरह की values का collection होता है। एक तरह की value से मतलब है या तो केवल strings या केवल integers या फिर flo… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Spring boot app (error: method getFirst()) failed to run at local machine, but can run on server
    The Spring boot app can run on the online server. Now, we want to replicate the same app at the local machine but the Spring boot jar file f...
  • Log activity in a Laravel app with Spatie/Laravel-Activitylog
      Requirements This package needs PHP 8.1+ and Laravel 9.0 or higher. The latest version of this package needs PHP 8.2+ and Laravel 8 or hig...
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh
    I had follow these steps to install an configure firebase to my cordova project for cloud messaging. https://medium.com/@felipepucinelli/how...
  • Laravel auth login with phone or email
          <?php     Laravel auth login with phone or email     <? php     namespace App \ Http \ Controllers \ Auth ;         use ...
  • Vue3 :style backgroundImage not working with require
    I'm trying to migrate a Vue 2 project to Vue 3. In Vue 2 I used v-bind style as follow: In Vue 3 this doesn't work... I tried a...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Arr::from() Method in Laravel 12.14 - 5/14/2025
  • Streamline API Resources with Laravel's Fluent Methods - 5/13/2025
  • Customize URL Handling with Laravel's Macroable URI Class - 5/13/2025
  • Use Passkeys in Your Laravel App - 5/13/2025
  • Laravel Seeder Generator - 5/12/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda