Page 135 - KEC Khaitan C5 Flipbook
P. 135

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  133
   130   131   132   133   134   135   136   137   138   139   140