Concept Flow - Variable declaration and initialization
Start
Declare variable
Initialize variable
Use variable
End
First, we declare a variable to reserve space. Then we give it a value (initialize). Finally, we can use it in the program.
int age = 25; int height; height = 180; int sum = age + height;
| Step | Action | Variable | Value | Explanation |
|---|---|---|---|---|
| 1 | Declare and initialize 'age' | age | 25 | 'age' is created and set to 25 |
| 2 | Declare 'height' | height | undefined | 'height' is created but not initialized yet |
| 3 | Initialize 'height' | height | 180 | 'height' is assigned the value 180 |
| 4 | Declare and initialize 'sum' | sum | 205 | 'sum' is created and set to age + height (25 + 180) |
| 5 | End | - | - | All variables declared and initialized |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|---|
| age | undefined | 25 | 25 | 25 | 25 | 25 |
| height | undefined | undefined | undefined | 180 | 180 | 180 |
| sum | undefined | undefined | undefined | undefined | 205 | 205 |
Variable declaration reserves space: e.g., int x; Initialization assigns value: e.g., x = 5; You can combine: int x = 5; Use variables only after initialization to avoid errors. Variables hold data used in calculations or logic.