icp/oer/courses/c-newcomers/sections/01-introduction/03-variables/content.md

1.1 KiB

As you could see, it is pretty simple to print out some basic calculations. But sometimes you want do more complicated ones. Then it is a good idea to save parts of the calculations in so called variables.

Knowledge

Variables are like drawers, where you can store and label your solutions, but also update and replace them. You then can use the name of the variable just like a number, but it holds the data of what you have put into it before.

To declare these variables you first write the type, e.g., float for decimals and int for whole numbers, then the name of the variable. This is needed that the computer can understand the program. To assign a value to a variable, you write equals and then the value or mathematical expression it should compute before it assigns the data.

A very simple example is:

float x = 4.0;
float y = 3.0;
float result = x * y + 3.0;

You can also declare first what the variable is and then change it's value:

float x;
x = 3.0; // now the value is 3.0
x = 4.0; // now the value of x is 4.0

Task

Your task is to rewrite the program to compute the BMI to use variables with height and weight.