Saturday, December 23, 2017

JavaScript - If, Else,Else if , Switch Conditions - Day 21

Conditions are used to do particular conditions several times.

Following are some conditions used in JavaScript

if condition will execute a block of code.., if is used when statement is whether true or false.
else condition used when a block of statement is false
else if condition used to check more & more statements whether they are true
switch to specify many alternative blocks of code to be executed

If condition

if (condition) {
    block of code to be executed if the condition is true}

if should be in lower case... if is in uppercase then JavaScript will show error.

<p id="demo"></p>

<script>
if (5 == 5) {
    document.getElementById("demo").innerHTML = "True";
}
</script>

Output : True

Else Condition

<p id="demo"></p>

<script>
if (5 == 7) {
    document.getElementById("demo").innerHTML = "True";
}else {
    document.getElementById("demo").innerHTML = "False";
}
</script>

Output: False

Else if Statement

if (condition1) {
   statement
} else if (condition2) {
   statement
} else {
   statement
}

Switch Statement

switch(expression) {
    case n:
        code
        break;
    case n:
        code
        break;
    default:
     code
}

Break Keyword : When execution reached Break keyword the program will run out of Switch statement. If we don't use Break all codes will be read by device. 

Default Keyword : The default keyword specifies the code to run if there is no case match

No comments:

Post a Comment