Skip to main content

Chapter 7 If-Else Operators in JavaScript - Technology369kk

 IF -ELSE

Now, discuss about If-Else in JavaScript, this is most important chapter for every programmer. while writing a program, there may be a situation when you need to adopt one out of a given set of paths. In such cases, you need to use conditional statements that allow your program to make correct decision and perform right actions. 

JavaScript supports conditional statments which are used to perform diffrent actions based on diffrent conditions. Here we will explain the if..else. 





Flow Chart of if-else: 

  • The following flow chart shows how the if-else statments works: 



  • JavaScript supports the following forms of el...else statements:  
  • If Statement
  • If....else stement 
  • if..else If... Statement

If Statement:  

The if statement is the fundamental control statement  that allows JavaScript to make decisions and execute statements conditionally 

Syntax: 

The syntax for a basic if statement is a follows: 


if(expression){
    Statement(s) to be executed if expression  is true.
}


  • Here a JavaScript expression is evaluted. If the resulting value is true, the given statement(s)  are executed. If the expression is false, then no statement would be not executed. Most of the times, you will use comparison operators while making decisons. 
  • Here a javaScript expression is evaluted. If the resulting value is true, the given statement(s)  are executed. Most of the times, you will use comparison operators while making decisions. 

Example :  

Try the following example to understand how the if statment works: 


<script>
    var age = 20;
    if(age > 18){
        document.write("<b> Qulifies for driving </br>");
    }

// You can use diifrent value on variable

</script>


OUTPUT:

Oualifies for driving
You can use diifrent value on variable


if....else stements: 

  • The if...else statement is the next  form of control statment that allows javascript to execute statments in a more controlled way. 

Syntax: 

  • The syntax of an if else statement is a follows:  


    if(expression){
        statement(s)  to be executed if expression is true.
    }else{
        statement(s) to be executed expression is true.
    }
   

  • Here JavaScript expression is evaluated. If the resulting value is true, the given statement(s) in the ‘if’ block, are executed. If the expression is false, then the given statement(s) in the else block are executed.
Example:  

  • Try the following code to learn to implement an if statement in JavaScipt:
 
    var age = 10;
    if(age > 18){
        document.write("<b> Qulifies for driving </br>");
        }else{
        document.write("<b> Not Qulifies for driving </br>");  
    }


OUTPUT:

Not, Oualifies for driving: 


if...else if...stements

  • The if...else statement is an advanced from if...else that allows javascript to make a correct decision out of several conditions. 
Syntax: 
  • The syntax of an if else if statement is as follows:  

Syntax:
<p>
    if(expression 1){
        statement(s)  to be executed if expression1 is true.
    }else if(expression 2){
        statement(s) to be executed of expression 2 is true.
    }else if(expression 3){
        statement(s) to be executed if expression 3 is true..
    }
    else{
        statement(s) to be executed expression is true.
    }

</p>


  • There is nothing special about this code. It is just a series of if statements, where each if is a part of the else clause of the previous statement. Statement(s) are executed based on the true condition, if none of the conditions is true, then the else block is executed.
  • Try the code for the topics:  
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Conditional Statement </title>
</head>
<body>

    <script>
       
    var book= "maths";
    if (book == "history"){
        document.write("<b> History Book </b>");
    }else if(book == "maths" ){
        document.write("</b> Maths Book </b>");
    }else if(book =="english"){
        document.write("</b> English Book </b>");
    }else{
        document.write("</b> Sorry!!!, Unknown Books");
    }
   
    </script>
   
</body>
</html>


OUTPUT:
Not, Oualifies for driving: 


I hope this is helpful for you. 


Java Complete Course:



Comments

Popular posts from this blog

Assignment of ITA/ Information Technology and Application BCA- Technology369kk

Q1. What is  computer Explain basic computer architecture and Difference components.  2. Discuss the use of memory in computer system, Explain memory hierarchy  in details. 3. What is software? Explain difference types of software with explain. 4. Write short notes on the given:- (I) Internet. (II) LAN (Local area network ) (III) Search engine (IV) Web browser  Q 1.What is computer Explain basic computer architecture, Difference components of computer.   Computer :- Computer is defined as an electronic device that takes input data and instructions from the user and after processing them, it generates useful and desired output quickly.   A computer is designed to execute applications and provides a variety of solutions through integrated hardware and software components.                            It is fast and automatic device. It works with the help of programs and represents the d...

C++ and Java Practical All Questions Answers - BCA -Technology369kk

C++ and Java  In this post see most important questions for practical questions given by college all questions with answers . Guys I want to say that this is only for suggested post for your practical please request to you change same alphabets, words or anything  methods name and variables name because if you write all words same then this is copy paste for another peoples.  Used Topics:  Keywords, Variables, Condition Statements, Function , Array, Structure, Pointer.                           In OOPs, Class and Objects, Constructor, Poly morph, Encapsulation, Access Specifiers,                               Inheritance etc.  So, Without Time Lose Come to the Points, let's go start Now:        *************************************************************************  C++ 12 ...

Assignment of PMO (Principal of Management and Organization) - Technology369kk

 ** Assignment Of PMO ** Agenda: -  4 Questions discuss in this post. Question 1. Write a d etails note on selection why it Called. negative process.  Question 2. Write a details note on 'span of control. Question 3. Planning is an essential process, do you agree ? Discuss  Question 4. Write a note on management function. Q 1. Write a d etails note on selection why it called negative process.  Ans :-  Selection is the process of choosing the most suitable candidates out of the several candidates available.          Selection is a negative process because there may be more rejected then those selected in most of the candidates that is called selection is a negative process. → Selection process has the following steps:-  [ A .] Screening of applicants - Based on the screening of applicants only those candidates. It Called further process of selection. Who are found eligible for the job Standards of the the organization. [ B .] S...