Easy Tutorial: Computer Programming (part 2) for DUMMIES

in #programming8 years ago (edited)

My first tutorial on computer programming was somewhat of a success, so I decided to continue the tutorials! We will be continuing with the language C++. In the last tutorial, I explained how to write, compile, and run a simple program and explained each part of the program. I will be assuming you know how to do those things now, so they won't be included in this one. Here is the first one if you need to catch up:

https://steemit.com/programming/@charlie.wilson/easy-tutorial-computer-programming

In this tutorial, I will explain variables. Variables are placeholders for values, just like in algebra. The name of a variable is called its identifier. No two variables should have the same name. There are instances where they can have the same name (this has to do with scope, but we are not getting into that right now), but it is always a good idea to not repeat names. Here are rules for identifiers in C++:

  • Only alphabets, digits, and underscores are allowed
  • Identifier names cannot start with a digit
  • Key words cannot be used as a name
  • Upper case and lower case letters are distinct
  • Special characters are not allowed
  • Global Identifier cannot be used as “Identifier”.

Variables have different data types. Here are a list of a few data types and their purpose:

bool (boolean)- Stores either value true or false ( 0 - false, 1 - true)
char (character) - stores a character (this is an integer type)
int (integer) - integers
float (floating point) - numbers including decimal points
void - the absence of type
string - a "string" (list) of characters such as a word or a sentence

These are just a few of the most common data types. When you declare a variable, you have to type the data type, then the name, then you can set it equal to a value. Here is an example of what I mean by that:

int x = 5;

Note that you can decide the value later. This is equivalent to the line of code above:

int x;
x = 5;

When you want to use the variable, you just type in the name. Example:

cout << x;

This will put the value 5 on your screen. You can also declare several variables on the same line if they are the same type. Example:

char x, y, z;
x = 'g';
y = 'E';
z = '?';

Here is a program I wrote to show different things you can do with variables:

Here is the output:

Note: in order to use string as a type, you must include the string class (line 2)

I hope this was helpful! I will probably be continuing the series, so please let me know what you did or didn't like about this post.

Sort:  

I want to learn this but still a bit confused, thanks tho its a start

Feel free to ask any questions!

Coin Marketplace

STEEM 0.21
TRX 0.25
JST 0.038
BTC 95686.04
ETH 3320.33
USDT 1.00
SBD 3.08