Page 80 - PortGPT_V2.1_C8_Flipbook
P. 80

6                     Conditional Statements



                                                              in JavaScript











                      Let’s surf
                           Conditional Statements






                   Let’s PLug-in


                     If a = 8, b = 2 and c = 20, then what will be the output of the following statements:
                     1.   document.write(a > b && b < c)      =    ____________________
                     2.   document.write (a * b + c)          =    ____________________
                     3.   document.write (a/b + c)            =    ____________________
                     4.   document.write (a > b || b > c)     =    ____________________

                     5.   document.write (a * b - 2 + c)      =    ____________________





                  There are situations in life when we need to decide something based on a situation. For example, if it
                  rains, I will stay at home or else I shall visit the market. Similar situations arise in programming as well,
                  where we need to make some decisions and based on these decisions, execute a set of commands.
                  We need to determine which action is to be taken and which statement is to be executed, if the
                  outcome is true or false. In this chapter, we will learn about conditional statements of JavaScript.


                         CONDITIONAL STATEMENTS


                  Decision making in JavaScript is done by called conditional statements which decide the flow of
                  program execution. The following conditional statements are available in JavaScript:
                  •   if statement

                  •   if...else statement

                  •   Nested if statement
                  •   Switch statement







                  78    Premium Edition-VIII
   75   76   77   78   79   80   81   82   83   84   85