Javascript part-4
๐Javascript control statement's..
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 เคเคฐเคตा เคธเคเคคे เคนै।
เคเคธเคे เคฒिเค เคเคช 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 เคे เคฌाเคนเคฐ เคธे เคถुเคฐू เคนो เคाเคคा เคนै।
Superb sir
ReplyDelete