Saturday 31 March 2018

Control statements in javaScript

Control statements are used to control and execute the statements based on certain conditions or repeat a group of statements until certain specified conditions are met
There are different types of if statements in javaScript.
·                     if statement
·                     if-else statement
·                     nested if statement
·                     if-else-if ladder
if Statement:
if statement checks the condition. It is executed if condition is true.

Syntax:
    if(condition)
{    
   //statement
  } 
 Example:
   <script>  
    var x=50;  
    if(x>40){  
   document.write("it is greater than 40");  
    }  
   </script>  


if-else Statement
If-else statement checks the condition. It executes if condition is true otherwise it comes to else block and else block will be executed.
 Syntax:
     if(condition)
{    
     //statement
  }
     else
{    
        //statement
   }    

Example: 
   <script>  
  var x=30;  
   if(x>0)
   {  
  document.write("+ve number");  
   }
  else{  
   document.write("-ve number");        
    }
  </script> 

If-else-if ladder Statement
if-else-if ladder statement executes one condition from multiple conditions.It will check the conditions one by one ,if the condition is true then print the output otherwise it comes to  else block and else block will be executed.
Syntax:
   if(condition1){   
     / /statement
        }
    else if(condition2){    
     //statement
          }
     else if(condition3){    
      //statement
            }
    ...........................    
     else{    
     //statement
        }
   Example:
   <script>  
  var x=30;  
   if(x>0){  
  document.write("+ve number"); 
   }  
  else if(x<0){  
  document.write("-ve number");  
   }  
  else if(x==0){
  document.write("zero");  
   }
  else
  {
  document.write("please enter a number"); 
   }

  </script> 

switch
switch statement executes one statement from multiple conditions. It will check the conditions one by one. If it finds true, it will print the output otherwise it comes to default and default statement will be executed. It is just like if-else-if ladder
Syntax:
switch(expression){      
case value1:      
 //statement
 break;    
case value2:      
 //statement
 break;    
......      
      
default:       
 //statement
}    

Example:

<script>
var month =parseInt(prompt("Enter month(1-12)"));
 switch(month)
    {
        case 1:
            alert("31 days");
            break;
        case 2:
            alert("28/29 days");
            break;
        case 3:
            alert("31 days");
            break;
        case 4:
           alert("30 days");
            break;
        case 5:
            alert("31 days");
            break;
        case 6:
            alert("30 days");
            break;
        case 7:
            alert("31 days");
            break;
        case 8:
           alert("31 days");
            break;
        case 9:
            alert("30 days");
            break;
        case 10:
            alert("31 days");
            break;
        case 11:
           alert("30 days");
            break;
        case 12:
            alert("31 days");
            break;
        default:
            alert("Invalid month! Please enter month number between 1-12");
}
</script>


1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...