Javascript | Control Statement & It's Types By. Shivansh Sir

 JAVASCRIPT 

JAVASCRIPT | CONTROL STATEMENT | IF STATEMENT

If Statement में जब condition 'true' होती है तब if statement execute होता है |अगर condition false होती है तो statement execute नहीं होता |

Syntax

if(condition){

//if statement(s);

}

Source Code :

<script type="text/javascript">

var a = 10, b = 15;

if(a < b){

document.write("a is less than b");

}

</script>

Output

a is less than b.

JAVASCRIPT | CONTROL STATEMENT | IF ELSE STATEMENT

if_else Statement में जब condition 'true' होती है तब if का statement execute होता है अगर condition 'false' होती है तो else का statement execute होता है |

Syntax 

if(condition){

//if statement(s);

}else{

//else statement(s);

}

Source Code

<script type="text/javascript">

var a = 10, b = 15;

if(a > b){

document.write("a is greater than b");

}

else{

document.write("a is not greater than b");

}

</script>

Output

a is not greater than b.

JAVASCRIPT | CONTROL STATEMENT | ELSE IF STATEMENT

else_if Statement में अगर if की Condition true होती है तो if का statement execute होता है | अगर if का condition false होता है तो वो अगले condition पर जाकर check करता है | अगर वो condition true होता है तो वो उसका statement execute करता है | अगर कोई भी condition true नहीं होती तो वो else का statement execute करता है |

Syntax

if(condition){

//if statement(s);

}else if(condition){

//else if statement(s);

}else{

//else statement(s);

}

Source Code

<script type="text/javascript">

var a = 10, b = 15;

if(a > b){

document.write("a is greater than b");

}

else if(a < b){

document.write("a is less than b");

}

else{

document.write("a is equal to b");

}

</script>

Output

a is less than b.

JAVASCRIPT | CONTROL STATEMENT | SWITCH CASE STATEMENT

Switch case statement में expression होता है और उससे related कुछ cases होते है | जो case उस expression या declare किये हुए variable से match होती है तब वो execute होता है | अगर कोई भी case expression से match नहीं होती तो वो default का statement execute करता है | हर statement के बाद break लगाना पड़ता है, इसका मतलब वो उसके पहले का statement ही execute करेगा | अगर break नहीं लगाया जाता तो वो पहला और दूसरा ये दोनों statement को execute करेगा | default case के बाद break नहीं लगाते |

Syntax

switch(expression){

case 1 :

statement(s);

break;

case 2 :

statement(s);

break;

case n :

statement(s);

break;

default :

statement(s);

}

Source Code

<script type="text/javascript">

var date = new Date();

var day = date.getDay();

switch(day){

case 0 :

document.write("Today is Sunday.");

break;

case 1 :

document.write("Today is Monday.");

break;

case 2 :

document.write("Today is Tuesday.");

break;

case 3 :

document.write("Today is Wednesday.");

break;

case 4 :

document.write("Today is Thurday.");

break;

case 5 :

document.write("Today is Friday.");

break;

case 6 :

document.write("Today is Saturday.");

break;

default :

document.write("Day is Not Found.");

}

</script>

Output

Today is Saturday.

JAVASCRIPT | CONTROL STATEMENT | CONTINUE & BREAK STATEMENT

Continue Statement

Continue Statement का इस्तेमाल कुछ statements को skip करने के लिए इस्तेमाल किया जाता है |

Syntax

continue;

Source Code

<script type="text/javascript">

for(var i=0; i<10; i++){

if(i==7){

    document.write("Number " + i + " is skipped. <br />");

    continue;

    }

    document.write(i + "<br />");

}

</script>

Output

0

1

2

3

4

5

6

Number 7 is skipped. 

8

9

Break Statement

Break Statement में loops और switch case के execution का काम किसी condition पर stop कर देता है |

Syntax

break;

Source Code 

<script type="text/javascript">

var i = 0;

while(i < 20){

document.write("Value of i is " + i + "<br />");

i++;

if (i == 10){

break;

}

}

</script>

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


For Any Doubt Clear on Telegram Discussion Group

Join Us On Social Media

For Any Query WhatsApp Now

Comments

Popular posts from this blog

Javascript | Introduction / Syntax / External JS / Comment By. Shivansh Sir

Javascript | Javascript Operators & It's Types By. Shivansh Sir