Skip to main content

Command Palette

Search for a command to run...

A Basic Guide to Coding Pt. 1: Variables & Conditionals

By: Varsha Kumar

Published
4 min read

       If you have just started on your coding journey or if you are an advanced coder all the way, you should be familiar with variables, conditionals, and loops, which are essential parts of basic programming. So, what are they and why are they important?

Variables

              Variables are reserved spaces of memory, usually RAM, in order to store any type of data. For example, if you want to store the word “data” in Python, you would type:

# In Python…

word = "data"

              However, with variables, they have data types, and in many languages, such as Java and C++, you must declare them. This is because while some languages, like Python, have memory that is dynamically allocated, sometimes it needs to be given space manually. Common data types include int (integer values), doubles or floats (depending on the language and whether they are decimals), Booleans (values true or false), and strings (words, but must include quotation marks to do so successfully in many languages).

              For example, in C++, this might look different, for example, 

#include <string> // usually have to include this header in C++ to declare variables in C++!
...
string word = "data";

 Variables are flexible, meaning that with numerical data types, you can change them through addition, subtraction, multiplication, division, and so on, and can be gathered through user input, allowing your program to be interactive.

Conditionals

              What if you have two paths for something? For example, if you want your program to output it’s warm if it’s a warm temperature, while it’s cold in a cold temperature, conditionals are what you need! Conditionals allow users to choose multiple possibilities, allowing for if you win or lose in a game program, turn the music on or off, etc.

For example,

// Assume all variables have been declared & are in Fahrenheit
// In the programming language C++!
if (temp >= 70)
{
              cout << "It's warm!";
}
else
{
              cout << "It's cold!";
}

              Now, you might be confused about the “if” and the “else”. The “if” is the first conditional, or true or false statement. If the temp variable is greater than or equal to 70 degrees, then the program would output “It’s warm!”. However, if that “if” is false, it would output the other else statement.

              There are also else-if statements that you can add onto. Essentially, if the first conditional is not true, then it would move to the next else-if statement and so on in chronological order. Therefore, remember that the order matters and evaluate in the context of that to avoid logic errors. For example,

if (temp >= 20) {
              cout << "It's in the 20's! It's cold!";
}
else if (temp >= 30) {
              cout << "It's in the 30's! Still very cold!"
}
... // so on

Technically, if you have a temperature of 30 degrees, it would pass the first condition anyway. Keep this in mind when structuring your conditions.

Another thing to note is that the statement is evaluated by Boolean statements, or if that condition is true or false, and if true, it executes the statement, and if false, it does not. That is essentially the entire logic of conditionals!

Hopefully, if you are a beginner self-learner or programmer, you found this article helpful. Variables and conditionals are key to writing good programs and making programs the way they are (e.g., user input, if you win or lose in a game, etc.)