How is it possible for a variable such as int a = 100; with a value as 100 be able to change to a = 50; with a value of 50? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

How is it possible for a variable such as int a = 100; with a value as 100 be able to change to a = 50; with a value of 50?

This was taken from lesson 8 on Basic Concepts. It says a variable's value can change many times but i'm trying to wrap my head around how. What was the point of assigning the value 100 in the first place?

7th Jun 2017, 3:36 PM
Carlos DV
Carlos DV - avatar
2 ответов
+ 5
The point was to show you whats happening. int a = 100; This assigns the value of 100 to the variable a of type int. a = 50; This re-assigns the variable, assigning it to the value of 50. The value of 100 is now lost, but you can keep modifying the value of variables as much as you want. This is what the lesson is trying to say: This is allowed: int a = 100; a = 75; a = 45; a = 25; a = 18881; Doesn't matter, I'm just re-assigning the value. Further in the lesson you may learn that you can actually use the variable itself to assign a new value. int a = 100; a = a - 25; This assigns the value of a - 25 to the variable a. What's the value of a - 25? Well, a is 100. So a = 100 - 25; So, the new value of a is 75. This is where computer science's equals sign (=) differs a bit from mathematics.
7th Jun 2017, 3:43 PM
Rrestoring faith
Rrestoring faith - avatar
0
That makes a lot of sense, thank you.
7th Jun 2017, 3:57 PM
Carlos DV
Carlos DV - avatar