Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 147 - 2615_KEC
P. 147

    Performing Calculations

                     Variables can be used in mathematical operations, making programs more interactive.

                     Example: To perform calculations.

                         var num1 = 10;
                         var num2 = 5;

                         var sum = num1 + num2;

                     Controlling Program Behaviour

                     Variables help control decision-making using if or if-else statements.

                     Example: To control program behaviour using if block.

                     if (score >= 10) {
                         setText("message", "You won!");

                     }
                     Storing User Input

                     Variables store user responses from input fields, making apps interactive.
                     Example: To store user input.

                         var userName = getText("nameInput");

                         setText("greeting", "Hello, " + userName + "!");

                     Function Parameters and Return Values

                     Functions use variables as parameters and return values.

                     Example: To use functions to return value.

                     function addNumbers(a, b) {

                         return a + b;
                     }

                     var total = addNumbers(4, 6);
                     setText("outputLabel", "The sum is: " + total);

                                                                              Working with Variables and Conditional Logic  145

| 142 143 144 145 146 147 148 149 150 151 152 |