Learn to Code via Tutorials on Repl.it

← Back to all posts
C++ An Introduction: Part 3
ipastrano (95)

C++: An Introduction: Part 3

  • Note: I will be using a changelog from now on, to document changes to every post of this series.
    ChangeLog:
  • 2-23-2020 1750: fixed typo that @Highwayman found!
  • Thanks for the support people!
  • Questions? Comments? Errors to point out? Tag me in the comments(@ipastrano) and upvote if you like!

This post will focus on variables(easier than it sounds).

Variables

  • Variables hold pieces of information such as integers, decimals, characters, words, phrases, etc. The variables can be operated on (arithmetic, printing to the screen, etc.)
  • Each variable holds a certain amount of space in the computer, but it's unlikely to fill up your computer unless there are a LOT. (I have made ~35-50 programs on repl all in c++, and they total less than 1MB!)
  • Variables cannot hold information that they aren't meant to(ex. an integer variable cannot hold a character, etc.)
  • Here, I will introduce you to numeric variables.
  • To create an integer variable, use int in this fashion:
int x;
  • Just so you know, x is not really a good variable name. Make the name useful.
  • Here are a few examples:
  • I'm holding the square footage of a house: use squareFeet or better yet, houseSquareFeet.
  • I am holding the number of apples to buy from the store: use apples, or numApples.
  • I am holding the number of people who read this post. Use numPeople or peopleRead.
  • Note that the variable name cannot start with a digit, or be a keyword such as if, int, else, for, while, do, float, double, char... the list goes on.
  • No special characters such as !, @, #, $, %, ^, &, *, (, ), =, or +.
  • There are two widely accepted ways to define variables: snake_case and camelCase. I prefer camel case, but it is ultimately your choice!
  • Here are the different numeric variables(integers only).
  • short = for short integers.
  • unsigned short = for short whole numbers.
  • int - for integers.
  • unsigned int - for whole numbers.
  • long = for long integers.
  • unsigned long = for long whole numbers.
  • long long = for REALLY LONG integers.
  • unsigned long long = for REALLY LONG whole numbers.

Assigning

  • To assign, put the variable name on the left hand sign of an equals(=) and put the value on the other.
  • Ex.
int apples;
apples = 5;
  • Before assignment, the variable apples had a garbage value such as 1945738. Then I put the value 5 into apples.
  • To display the number, use cout in this fashion:
cout << apples;
  • This will display 5. nothing more, nothing less.
  • To add some context, use text:
cout << "This apple pie recipe requires " << apples << " Fuji apples.\n";

This displays:
This apple pie recipe requires 5 Fuji apples.

That's all folks: Tune in next Sunday!

Note to self: Insert jazz music here I won't

Commentshotnewtop
Highwayman (974)

Oh oops, disregard my last ping then lol.

Edit: also: yay!

ipastrano (95)

@Highwayman thanks. I don't think anyone saw the second one other than us, but at least its here. The first is what grabbed attention.

BTW, How did you find this so fast?!

Highwayman (974)

@ipastrano your tutorial or your first comment? The first comment I found super fast bc I watch my pings like a hawk generally bc of ongoing questions and the tutorial because when you commented I started to wonder why you were in the learn section. :P

ipastrano (95)

@Highwayman hehehe clever... I meant the tutorial :P

Highwayman (974)

@ipastrano also found a small typo: you forgot the space between the hashes and the word "assigning" for your assigning header

Highwayman (974)

@ipastrano 🤷‍♂️ Welp, got it on the second guess lol, at least I got some bragging in XD