C++ Lec2-Comments & Variables

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1

C++ Comments

Comments can be used to explain C++ code, and to make it more readable. It can also be used to
prevent execution when testing alternative code. Comments can be singled-lined or multi-lined.

Single-line comments start with two forward slashes (//).

Any text between // and the end of the line is ignored by the compiler (will not be executed).

This example uses a single-line comment before a line of code:

Example

#include <iostream>
using namespace std;
int main() {
// This is a comment
cout << "Hello World!";
return 0;
}

This example uses a single-line comment at the end of a line of code:

Example

#include <iostream>
using namespace std;
int main() {
cout << "Hello World!"; // This is a comment
return 0;
}

Programming C++ Samir Bilal Practical & Theoretical


2

C++ Multi-line Comments

Multi-line comments start with /* and ends with */. Any text between /* and */ will be ignored
by the compiler:

Example

#include <iostream>
Single or multi-line comments?
using namespace std;

int main() { It is up to you which you want to


use. Normally, we use // for short
/* The code below will print the words Hello World!
comments, and /* */ for longer.
to the screen, and it is amazing */

cout << "Hello World!";

return 0; }

C++ Variables

Variables are containers for storing data values.

In C++, there are different types of variables (defined with different keywords), for example:

int - stores integers (whole numbers), without decimals, such as 123 or -123

double - stores floating point numbers, with decimals, such as 19.99 or -19.99

char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes

string - stores text, such as "Hello World". String values are surrounded by double quotes

bool - stores values with two states: true or false

Programming C++ Samir Bilal Practical & Theoretical


3

The general rules for constructing names for variables (unique identifiers) are:

 Names(Variables) can contain letters, digits and underscores.


 Names must begin with a letter or an underscore (_).
 A variable name can only have alphabets, numbers, and the underscore _.
 Names are case sensitive (myVar and myvar are different variables).
 Names cannot contain whitespaces or special characters like !, #, %, etc or Arithmatic
Operaters (+,-,*,/).
 Variable names must not begin with numbers.
 Reserved words (like C++ keywords, such as int) cannot be used as names

Programming C++ Samir Bilal Practical & Theoretical


4

Declaring (Creating) Variables

To create a variable, you must specify the type and assign it a value:

Syntax

type variable = value;

Where type is one of C++ types (such as int), and variable is the name of the variable (such as x
or myName). The equal sign is used to assign values to the variable.

To create a variable that should store a number, look at the following example:

Example

Create a variable called Num of type int and assign it the value 15:

#include <iostream>

using namespace std;

int main() {

int Num = 15;

cout << Num;

return 0;

You can also declare a variable without assigning the value, and assign the value later:

Programming C++ Samir Bilal Practical & Theoretical


5

Example

#include <iostream>

using namespace std;

int main() {

int Num;

Num = 15;

cout << Num;

return 0;

Note that if you assign a new value to an existing variable, it will overwrite the previous value:

Example

#include <iostream>

using namespace std;

int main() {

int Num = 15; // Now Num is 15

Num = 10; // Now Num is 10

cout << Num;

return 0;

}
Programming C++ Samir Bilal Practical & Theoretical
6

Other Types

A demonstration of other data types:

Example

int Num = 5; // Integer (whole number without decimals)


double FloatNum = 5.99; // Floating point number (with decimals)
char Letter = 'D'; // Character
string Text = "Hello"; // String (text)
bool Boolean = true; // Boolean (true or false)

You will learn more about the individual types in the Data Types chapter.

Q1) what is c++ comments? What is single-line comment? What is multi-line comment?

Q2) what is c++ variables?

Q3) what are the types of variables?

Q4) what are the general rules for constructing names for variables?

Q5) what is the syntax of variable?

Programming C++ Samir Bilal Practical & Theoretical

You might also like