You are on page 1of 4

LAB TASK

Comments are important. It makes the code more readable. Comment


is supported in all programming languages. In C++, comments are
categorized into two types. In this tutorial, I will show you how to write
comments in a C++ program. Always try to write comment in your code.
It is a good development practice.

Two types of comments: Line and block comment :

Two types of comments are available in C+


+: line and block comments. Line comment is used for single-
line comments and Block comment is used for multi-line comments.
Following are the syntax of these two types of comments :
#include <iostream>
using namespace std;

int main()
{
cout << "Hello"; // print hello
cout << "World"; /* print world
do not print hello here.
this is the last print statement
*/
}

A variable is a name given to a memory location. It is the basic unit of


storage in a program.
A typical variable declaration is of the form:
// Declaring a single variable
type variable_name;

// Declaring multiple variables:


type variable1_name, variable2_name, variable3_name;
Coding Example
#include<iostream>
using namespace std;
int main( )
{
int a, b, sum;
a=10;
b=20;
sum=a + b;
cout<<"Sum is "<<sum<<endl;
// This is a global variable
char myVar = 'A';
cout <<"Value of myVar: "<< myVar<<endl;
myVar='Z';
cout <<"Value of myVar: "<< myVar;
float t=10.5;
cout<<t;
string str=" I love programming";
cout<<str<<endl;
bool answer=true;
return 0;
}

Types of string

The string type is used to store a sequence of characters (text). This is


not a built-in type, but it behaves like one in its most basic usage. String
values must be surrounded by double quotes:

string greeting = "Hello";


cout << greeting;
we can also use string and numbers and take input from user in string.
// Include the string library
#include <string>

// Create a string variable


string greeting = "Hello";
_______________________

You might also like