Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 146 - 2615_KEC
P. 146

    WORKING WITH VARIABLES                                                                 CHAPTER

                      AND CONDITIONAL LOGIC                                                                 C3

                         learning outcomes

                         The student will be able to:
                            understand the concept and purpose of variables in programming.
                            declare, update and use variables for calculations and user input.
                            apply arithmetic and Boolean operators in programs.
                            use if and if-else statements for decision-making.
                            create interactive apps using variables and conditions.

                                INTRODUCTION TO VARIABLES AND THEIR FUNCTIONS

                      In programming, variables are used to store and manipulate data. Think of a variable as
                      a labelled box where you can keep information that can change as the program runs. In
                      Code.org's App Lab, variables allow us to manage user input, perform calculations and
                      control program behaviour dynamically.

                      DECLARING VARIABLES IN APP LAB

                      To create a variable in App Lab, we use the var keyword:
                         score is a numerical variable storing a value of 0.

                         playerName is a string variable that stores text ("Alex").
                      Example: to declare and update variable.

                        var score = 0;
                        var playerName = "Aman";
                      Once declared, variables can be updated throughout the program:

                        score = score + 1;

                        playerName = "Simran";

                      FUNCTIONS OF VARIABLES

                      Variables help make programs more interactive and dynamic. For example:

                      144   Premium Edition-V

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