Simple Intro
C++ is an object-oriented language, but still reads in a sequential manner. If you've ever done any HTML or GUI programming, you understand that, that type of programming relies on an event like a button click to occur. But C++ like Java or C is all executed at compile and run time.
So in order to program anything, we must first make a plan of what we want to do. This is called an algorithm. An algorithm can be written in structured English, in a flowchart or in pseudo code which is half code half English.
There are some other key items to go over before we dive right in. First is to understand what a variable is. A variable is just a temporary space in memory to hold data. So if we declare a variable, we are allocating a temporary space in memory for that data. Variables can be dynamic and change with the user's input or we can have constant variables that never change value once they've been declared.
The next item to note is the flow of assigning. To assign anything to a variable, the flow is right to left. Everything on the right of the assignment operator or the equal sign is done first. Then the data is moved from right to left. We could never say pizza equals x. Although that makes sense in our mind, in the computer, it only knows how to assign data in one direction.
The last item to note is that variables cannot be equations and will not be updated with a new value unless you reassign them. For example,
x = y + 3
y = 10
print x
Output:
3
In the first sentence y is zero because it hasn't been assigned a value yet so x becomes zero plus three or three and the three gets stored in the x memory spot. If you change y later, x will not get updated unless you say x is equal to y plus three again. It only moves down through the code, never going back up to update unless it's told to do so.
Simple Algorithm
Let's look at creating an algorithm for a simple pizza shop ordering system. If we want the customer to be able to enter a name and the number of pizzas they want.
- input customerName
- input quantity
- pizzaCost = 5.00
- totalCost = pizzaCost * quantity
- output CustomerName + " Your Total is " + totalCost
If we did any of that in a different order, it may work but it may not make sense. So it may be syntactically correct, but logically or semantically it is incorrect.
Write your first C++ Program
// Hello, World!
/*
Name:
Time:
*/
#include<iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
}
Output:
Hello, World!
C++ uses the semicolon to denote end of executable statements. In C++, we must include some libraries in order to input and output. These libraries are just pre-written code segments that we can use so we don't have to rewrite or define how to use the inputs and outputs.
The output operator <<
is used in between different data types when outputting them to the console. The endl
or \n
will add a new line to the console.
Now let's add a variable for a person's name.
// Hello, World!
/*
Name:
Time:
*/
#include<iostream>
using namespace std;
int main()
{
string name;
cout << "Please Enter Your Name: ";
cin >> name; // >> is the input operator
cout << "Hello, " << name << \n;
}
OutPut:
Please Enter Your Name:
Please Enter Your Name: Ahmed
Hello, Ahmed
Top comments (0)