Javascript part-4

๐ŸŒžJavascript control statement's..

Javascript

JavaScript in Hindi – Control Statements

  • Introduction to JavaScript control statements in Hindi

Introduction to JavaScript Control Statements 

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 เค•เคฐเคตा เคธเค•เคคे เคนै। 
Javascript

เค‡เคธเค•े เคฒिเค เค†เคช 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 เค•े เคฌाเคนเคฐ เคธे เคถुเคฐू เคนो เคœाเคคा เคนै।



Comments

Post a Comment