Lab Manual No 01
Lab Manual No 01
Experiment No 01
Dated:
Week 2
Semester:
Spring 2020
What is C++
C++ history
Let's see the programming languages that were developed before C++ language.
C++ Features:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
When the above program is compiled, linked and executed, the following output
is displayed on the VDU screen.
Hello World!
Comments
First three lines of the above program are comments and are ignored by the
compiler. Comments are included in a program to make it more readable. If a
comment is short and can be accommodated in a single line, then it is started
with double slash sequence in the first line of the program. However, if there are
multiple lines in a comment, it is enclosed between the two symbols /* and */
#include <iostream>
The line in the above program that start with # symbol are called directives and
are instructions to the compiler. The word include with '#' tells the compiler to
include the file iostream into the file of the above program. File iostream is a
header file needed for input/ output requirements of the program. Therefore,
this file has been included at the top of the program.
int main ( )
The word main is a function name. The brackets ( ) with main tells that main ( ) is
a function. The word int before main ( ) indicates that integer value is being
returned by the function main (). When program is loaded in the memory, the
control is handed over to function main ( ) and it is the first function to be
executed.
cout<<"Hello World!";
This statement prints our "Hello World!" message on the screen. cout
understands that anything sent to it via the << operator should be printed on the
screen.
return 0;
This is a new type of statement, called a return statement. When a program
finishes running, it sends a value to the operating system. This particular return
statement returns the value of 0 to the operating system, which means
“everything went okay!”.
If bytes flow from main memory to device like printer, display screen, or a network
connection, etc, this is called as output operation.
If bytes flow from device like printer, display screen, or a network connection, etc to main
memory, this is called as input operation.
<iostream> It is used to define the cout, cin and cerr objects, which correspond
to standard output stream, standard input stream and standard error
stream, respectively.
<iomanip> It is used to declare services useful for performing formatted I/O, such
as setprecision and setw.
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. string abc = "Welcome to C++ tutorial";
5. cout << "Output is: " << abc << endl;
6. }
Output:
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. int age;
5. cout << "Enter your age: ";
Output:
C++ Variable
A variable is a name of memory location. It is used to store data. Its value can be changed
and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
1. type variable_list;
1. int x;
2. float y;
3. char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
1. int a; 1. int 4;
2. int _ab; 2. int x y;
3. int a30; 3. int double;
The memory size of basic data types may change according to 32 or 64 bit operating
system.
Let's see the basic data types. It size is given according to 32 bit OS.
float 4 byte
double 8 byte
C++ Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name etc. A
list of 32 Keywords in C++ Language which are also available in C language are
given below.
A list of 30 Keywords in C++ Language which are not available in C language are
given below.
C++ Operators
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise etc.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
o Misc Operator
1. int data=5+10*10;
The "data" variable will contain 105 because * (multiplicative operator) is evaluated before +
(additive operator).
C++ if-else
In C++ programming, if statement is used to test the condition. There are various types of if
statements in C++.
o if statement
o if-else statement
o nested if statement
o if-else-if ladder
C++ IF Statement
The C++ if statement tests the condition. It is executed if condition is true.
1. if(condition){
2. //code to be executed
3. }
C++ If Example
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. int num = 10;
6. if (num % 2 == 0)
7. {
8. cout<<"It is even number";
9. }
10. return 0;
11. }
Output:/p>
It is even number
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
5. if (num % 2 == 0)
6. {
7. cout<<"It is even number";
8. }
9. else
10. {
11. cout<<"It is odd number";
12. }
13. return 0;
14. }
Output:
It is odd number
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number
21. }
22. else if (num >= 70 && num < 80)
23. {
24. cout<<"B Grade";
25. }
26. else if (num >= 80 && num < 90)
27. {
28. cout<<"A Grade";
29. }
30. else if (num >= 90 && num <= 100)
31. {
32. cout<<"A+ Grade";
33. }
34. }
Output:
Output:
1. if(condition1){
2. if(condition2){
3. //code to be executed if condition1 and condition2 is true
4. }
5. else{
6. //code to be executed if condition1 is true but condition2 is false
7. }
8. }
9. else{
10. if(condition3){
11. //code to be executed if condition1 is false but condition3 is true
12. }
13. else{
14. //code to be executed if condition1 and condition3 is false
15. }
16. }
3. int main()
4. {
5. int age;
6. cout<<"Enter your age: ";
7. cin>>age;
8. cout<<"Have you CNIC. Enter 1 for Yes and 0 for No: ";
9. cin>>cnic;
10. //condition to check voting eligility
11. if(age>=18)
12. {
13. if(cnic==1)
14. cout<<"You are eligible for voting"<<endl;
15. else
16. {
17. cout<<"You are not eligible for voting because you don’t have CNIC."<<endl;
18. }
19. }
20. else
21. { if(age>=0)
22. cout<<"You are not eligible for voting because you are underage."<<endl;
23. else
24. {
25. cout<<"Invalid Inputs."<<endl;
26. }
27. }
28. return 0;
29. }
C++ switch
The C++ switch statement executes one statement from multiple conditions. It is like if-else-
if ladder statement in C++.
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break;
5.
6. case value2:
7. //code to be executed;
8. break;
9.
10. ......
11.
12. default:
13. //code to be executed if all cases are not matched;
14. break;
15. }
18. case 7:
19. cout<<" YOUR GRADE IS C";
20. break;
21. case 6:
22. cout<<" YOUR GRADE IS D";
23. break;
24. case 5:
25. cout<<" YOUR GRADE IS F";
26. break;
27. case 4:
28. cout<<" YOUR GRADE IS F";
29. break;
30. case 3:
31. cout<<" YOUR GRADE IS F";
32. break;
33. case 2:
34. cout<<" YOUR GRADE IS F";
35. break;
36. case 1:
37. cout<<" YOUR GRADE IS F";
38. break;
39. case 0:
40. cout<<" YOUR GRADE IS F";
41. break;
42. default:
43. cout<<"Invalid Input";
44. break;
45. }
46. }
Output:
Enter a number:
10
It is 10
Output:
Enter a number:
55
Not 10, 20 or 30
C++ Loops
There may be a situation, when you need to execute a block of code several number of
times. In general, statements are executed sequentially: The first statement in a function
is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more
complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times
and following is the general from of a loop statement in most of the programming
languages
C++ programming language provides the following type of loops to handle looping
requirements.
1. while Loop
2. do while Loop
3. for Loop
Syntax
The syntax of a while loop in C++ is −
while(condition) {
statement(s);
}
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value. The loop iterates
while the condition is true.
When the condition becomes false, program control passes to the line immediately
following the loop.
Flow Diagram
Here, key point of the while loop is that the loop might not ever run. When the condition
is tested and the result is false, the loop body will be skipped and the first statement after
the while loop will be executed.
Example
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Syntax
The syntax of a do...while loop in C++ is −
do {
statement(s);
}
while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s)
in the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in
the loop execute again. This process repeats until the given condition becomes false.
Flow Diagram
Example
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
do {
cout << "value of a: " << a << endl;
a = a + 1;
} while( a < 20 );
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Syntax
The syntax of a for loop in C++ is −
for ( init; condition; increment ) {
statement(s);
}
Here is the flow of control in a for loop:
• The init step is executed first, and only once. This step allows you to declare and initialize any
loop control variables. You are not required to put a statement here, as long as a semicolon
appears.
• Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the
body of the loop does not execute and flow of control jumps to the next statement just after
the for loop.
• After the body of the for loop executes, the flow of control jumps back up to
the increment statement. This statement can be left blank, as long as a semicolon appears
after the condition.
• The condition is now evaluated again. If it is true, the loop executes and the process repeats
itself (body of loop, then increment step, and then again condition). After the condition
becomes false, the for loop terminates.
Flow Diagram
Example
#include <iostream>
using namespace std;
int main () {
// for loop execution
for( int a = 10; a < 20; a = a + 1 ) {
cout << "value of a: " << a << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Lab Task 1:
Write, compile and run a program to print Hello World on the screen.
Lab Task 2:
Write, compile and run a program to print your name and “Proud to be an
Engineer” on the screen.
Write, compile and run a program to evaluate the following expressions and
print the output on the screen.
1+2*4/2
(1 + 2) * 4 / 2
1 + 2 * (4 / 2)
9%2+1
(1 + (10 - (2 + 2)))
Lab Task 3:
Lab Task 5:
Write a C++ program to generate the multiplication table of a number (entered
by the user) using while loop and for loop.
Lab Report Evaluation: (To be submitted in printed form next week before lab)
➢ Home Task
Write all the lab task with their steps, code and the output.
Important note: Lab Report must be according to the Lab Report Format available on LMS and
must be submitted on time. Two similar reports will get zero marks. So, don’t try to copy your
fellow reports. Lab report must be submitted in group maximum of five.