Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 149 - 2615_KEC
P. 149

    CONDITIONAL STATEMENTS (IF-ELSE)

                     Conditional statements allow programs to make decisions based on conditions. The if
                     statement checks whether a condition is true and then executes code accordingly. The if-
                     else statement is used to control what happens depending on user input or calculations. It
                     executes one block if true and another if false.

                     Example: To check the voting eligibility.

                     var age = 16;

                     if (age >= 18) {

                         setText("outputLabel", "You can vote.");

                     } else {

                         setText("outputLabel", "You cannot vote yet.");
                     }

                     BOOLEAN LOGIC

                     Boolean logic deals with true/false values and is used in if-else statements for decision-
                     making. In App Lab, you can use Boolean operators (&&, ||, !) to make your program
                     smarter.. Some common comparison operators include:

                      Operator      Description                       Example

                      ==            Equal to                          x == 5

                      !=            Not equal to                      x != 10
                      >             Greater than                      score > 50

                      <             Less than                         age < 18

                      >=            Greater than or equal to          points >= 100
                      <=            Less than or equal to             level <= 10

                     Boolean operators such as AND (&&) or (||) and NOT (!) are used to combine multiple
                     conditions to make your program smarter.

                     Example: To check it's raining or not.

                     onEvent("checkWeatherButton", "click", function() {

                         var isRaining = true;

                                                                              Working with Variables and Conditional Logic  147

| 144 145 146 147 148 149 150 151 152 153 154 |