So far we have seen our first program. Now let’s dig into some simple tools and functionalities.
Comments
Comments are used in a program to explain the working of certain block of code. For example:
A single line comment starts with // (double slash).
//This is a comment
A multi-line comment looks like this:
/* Hey, this a multi-line comment
to explain the working of comments
inside a program */
The comments can be used anywhere between the program and are ignored by the compiler.
Here is an example of comments is in use:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| // <iostream> header included. | |
| using namespace std; | |
| int main() //The main function | |
| { | |
| cout << "Hello world!" << endl; | |
| /* I have printed Hello world. | |
| This is fun */ | |
| return 0; | |
| } |
You can copy paste this code and run it inside Code::Blocks. You will find that the comments are not printed.
Variable
Variables are used to store certain values. But in C++ you first need to define the data type of that variable.
A data type tells the compiler about the type of data that we need to store inside the variable.
Integer, a built-in data type, represents whole number system. It can be used by the keyword int.
For defining variable we need to give them a name which is called an identifier.
For example:
int var = 42;
var is the name of the variable. There are certain rules for variable names in C++.
- A variable name can contain letters (A-Z, a-z).
- A variable name can contain digits (0-9). But variable name cannot start with a digit.
- A variable name can also contain an (_) underscore.
Apart from this if any other symbol is used, the compiler will give an error.
Q.1 The correct variable names are:
a) Variable_
b) var89
c) 4var
d) __5ty
e)$var
Let us define some variable and print them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| int var; | |
| var = 48; | |
| cout<<var<<endl; | |
| // var will get printed | |
| int a = 44; | |
| int b = 42; | |
| int sum = a+b; | |
| cout<<sum; | |
| /* Addition, Subtraction and other operations can also be applied | |
| on varibles of same data type */ | |
| return 0; | |
| } |
Just don’t forget to define variables before using them.
Maths
As I have said before, all the arithmetical operations can be done on the variables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| int main() | |
| { | |
| int a = 10; | |
| int b = 4; | |
| int sum = a+b; | |
| int sub = a-b; | |
| int mult = a*b; | |
| int div = a/b; | |
| cout<<sum<<endl; | |
| cout<<sub<<endl; | |
| cout<<mult<<endl; | |
| cout<<div<<endl; | |
| return 0; | |
| } |
But when you run the above code you will find that the value of z is div 2 instead of 2.5.
This is because int data type only prints and uses integers. To print decimal numbers there is an another data type known as float.
If you use:
float z = x/y;
You will get the desired output.
There is one more arithmetic operator like +,-,*,/ which also plays an important role in calculations. It is ‘%’ (Modulus operator). It is used as:
int x = a%b;
Modulus operator gives the remainder when a is divided by b. Thus
int x = 10%4
// x will be 2
This is enough for today. Don’t forget to get into the practice of all the things you have learned today. Play with them as much as you can. You can also paste your creative code in comments for others to look at. Happy experimenting.
Next: The Basics of Input & Output>>

One thought on “Comments, Variables and some Maths”