| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
3-5: Variable Declarations (continued)
Initialising a Variable
Assigning the correct initial value to a variable is a very common task that is often overlooked by beginning programmers. In this exercise we have two variables that must be initialised. The Year variable should be set to zero and and since this happens by default when it is declared we have not explicitly assigned it an initial value.
The two statements
interestAmount = capital * interestRate
capital =
capital + interestAmount
assume that the variable capital has been assigned the correct initial value. That is, when the NextYr button is clicked for the very first time the capital variable needs to have already been assigned a value - namely the value of the Text property from the Initial Capital textbox. However, by default at declaration it will have the value 0, which is not what we want.
The initial value for the variable capital can't be assigned until after the user has entered a value in the Initial Capital textbox. But the initial value needs to be assigned before the code in the btnNextYr_Click event is executed for the first time.
So the question is how to do this, of course!