Compile and Execute: G++ Hello - CPP
Compile and Execute: G++ Hello - CPP
Compile and Execute: G++ Hello - CPP
"Hello World!\n" is what’s being outputted here. You need double quotes
around text. The \n is a special character that indicates a new line.
; is a punctuation that tells the computer that you are at the end of a
statement. It is similar to a period in a sentence.
A single line comment will comment out a single line and is denoted with
two forward slashes // preceding it:
Introduction to Variables
The "Hello World!" program simply writes to the screen. It does not read anything, calculate
anything, or allow for user input. That’s no fun!
Real programs tend to produce results based on some input that the user of the
program gives, rather than just outputting the same thing every time.
To read something from the keyboard, we first need somewhere in the computer’s
memory to store data. That is where variables come in.
A variable is simply a name that represents a particular piece of your computer’s
memory that has been set aside for you to store, retrieve, and use data.
In this lesson, we will learn about some of the basic data types:
Every variable has a type, which represents the kind of information you can store inside
of it. It tells your compiler how much memory to set aside for the variable, and it defines
what you can do with the variable.
"Every variable in C++ must be declared before it can be used!"
Suppose we are building a game and we want to keep track of a player’s score
that goes from 0 to 10. We need a variable!
int score;
In C++, variable names consist only of upper/lower case letters, digits, and/or
underscores.
score = 0;
Note: In C++, a single equal sign = does not really mean “equal”. It means “assign”. In
the code above, we are assigning the score variable a value of 0.
Combining Step 1 and Step 2
We can both declare and assign a value to a variable in a single initialization statement.
// Declare a variable
int score;
// Initialize a variable
score = 0;
We can actually combine these two lines into a single line of code:
Note: We only need to declare a variable one time! And it is highly suggested to
initialize a variable before using it later.
Arithmetic Operators
Computers are incredible at doing calculations. Now that we have declared variables,
let’s use them with arithmetic operators to calculate things!
+ addition
- subtraction
* multiplication
/ division
% modulo (divides and gives the remainder)
For example:
int score = 0;
// score is 0
You can output the value by simply adding this code underneath:
Yep! You can use multiple << operators to chain the things you want to output.
std::cout << "Hello, I am " << age << " years old\n";
This is called chaining.
User Input
Like we mentioned in the introduction, another way to assign a value to a variable is
through user input. A lot of times, we want the user of the program to enter information
for the program.
The mad scientist Kelvin has mastered predicting the weather in his mountain-side
meteorology lab.
Recently, Kelvin began publishing his weather forecasts on his website, however, there’s
a problem: All of his forecasts describe the temperature in Fahrenheit degrees.
C = (F - 32) / 1.8C=(F−32)/1.8