1
University of Management and Technology
Lahore Campus
========================================================
Session: Fall 2024
Instructor: Somia Waseem
Email: somia.waseem@umt.edu.pk
Course: Programming Fundamentals Lab
Course Code: CC111L
Department of Computer Science
School of Systems and Technology
Department of Computer Science (SST), UMT.
2
Basic C++ Concepts
1. cout (<<)
• cout is used to output various data types like characters, strings,
integers, floats, octals, and hexadecimal values onto the screen.
• Syntax: cout<<” The data you want to display.”;
2. Escape Character (\)
• Escape characters start with a backslash \ and allow the use of special
characters that control the format of the output.
• Escape sequences do not appear as characters in the output but change
the formatting or behavior.
Common Escape Sequences:
Escape Sequences Character
\b Backspace
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\? Question mark
\0 Null character
Department of Computer Science (SST), UMT.
3
3. cin (>>)
• cin is used to take input from the user, which is then stored in a variable.
• The user types a value, then presses Enter, and cin assigns this value to the
specified variable.
• Syntax: cin>>variable;
4. Single-Line Comments (//)
• Comments after // are ignored by the compiler.
• Used to add explanations or notes in code.
5. Multi-Line Comments (/* ... */)
• Text between /* and */ is ignored by the compiler.
• Ideal for longer comments or explanations.
Example Program
Here's a program demonstrating cout, cin, escape sequences, and comments:
#include <iostream>
using namespace std;
int main() {
// Single-line comment: Output greeting message
cout << "Hello, World!\n"; // \n moves to the next line
// Escape sequences in action
cout << "Special Characters:\n";
cout << "Backspace: Helo\b\b\b Hello\n"; // \b removes last letters
Department of Computer Science (SST), UMT.
4
cout << "Tab Space:\tThis is tabbed text\n"; // \t adds a horizontal tab
cout << "Alert Sound:\a (if sound is enabled)\n"; // \a triggers an alert sound
cout << "Quotation Marks: \"This is in quotes\"\n"; // \" for double quotes
cout << "Single Quote: \'Single quoted text\'\n"; // \' for single quotes
cout << "Backslash: C:\\ProgramFiles\n"; // \\ displays a backslash
// Taking user input with cin
int age;
cout << "Enter your age: ";
cin >> age; // User types in an integer
cout << "Your age is: " << age << "\n"; // Display the entered age
/* Multi-line comment:
This program demonstrates basic input, output,
escape sequences, and comments in C++.
*/
return 0;
Department of Computer Science (SST), UMT.
5
Variables:
Variables are just like containers to store data values and are named storage
locations in memory.
Rules for Naming C++ Variable:
• Variable name must begin with letter or underscore.
• Variables are case sensitive.
• They can be constructed with digits, letters.
• Keywords cannot be used.
• Whitespace and special symbols are not allowed other than underscore.
• sum, height, _value are some examples for variable name.
1. Declaring & Initializing C++ Variable:
• Variables should be declared in the C++ program before to use.
• Memory space is not allocated for a variable while declaration. It happens only
on variable definition.
• Variable initialization means assigning a value to the variable.
• Syntax to declare a variable: data_type variable_name;
• Syntax to initialize a variable:
data_type variable_name ;
Department of Computer Science (SST), UMT.
6
variable_name = value;
OR
data_type variable_name =value;
• Example:
int age;
age= 19;
OR
int age =19;
Data Types:
• Data types specify the type of data a variable can hold.
• Common data types:
o int: Integer values (e.g., 5, -10)
o float: Floating-point numbers with decimals (e.g., 3.14, -2.5)
o double: Double-precision floating-point numbers (more precise than
float)
o char: Single character (e.g., 'a', 'Z')
o bool: Boolean values (true or false)
o string: Sequence of characters (e.g., "Hello")
• Constants:
o Constants are fixed values that do not change during the program.
o Syntax: const data_type variable_name = value;
o Example: const int DAYS_IN_WEEK = 7;
Department of Computer Science (SST), UMT.
7
Examples:
1: This example demonstrates declaration and initialization of variables.
Program Output = 4.
2: This program illustrates different techniques to initialize a variable.
Department of Computer Science (SST), UMT.
8
Program Output = 6.
3: This program illustrates use of strings.
Here’s the program’s Output:
Department of Computer Science (SST), UMT.
9
Operators in C++ can be classified into 6 Types
a. Arithmetic Operators.
b. Relational Operators.
c. Logical Operators.
d. Bitwise Operators.
e. Assignment Operators.
f. Ternary or Conditional Operators.
a. Arithmetic Operators:
These operators are used to perform arithmetic or mathematical operations on
the operands. To solve most programming problems, you will need to write
arithmetic expressions that manipulate type int and double data.
Arithmetic Operators can be classified into 2 Types
i. Binary Operators: These operators operate or work with two operands (+,
-, /, *, %).
ii. Unary Operators: These operators operate or work with a single operand
(Increment++, Decrement--).
Department of Computer Science (SST), UMT.
10
Binary Operators
(+, -,*, /, %)
Purpose: Perform basic arithmetic operations.
Example:
int a = 10, b = 5;
cout << "Addition: " << (a + b) << endl; // 15
cout << "Subtraction: " << (a - b) << endl; // 5
cout << "Multiplication: " << (a * b) << endl; // 50
cout << "Division: " << (a / b) << endl; // 2
cout << "Modulus: " << (a % b) << endl; // 0
Unary Operators
(++,--)
Purpose: Increase or decrease a variable’s value by 1.
Example:
int x = 5, y=6;
cout << "Post-increment: " << x++ << endl; // 5 (x becomes 6) Post Increment
cout << "Pre-increment: " << ++x << endl; // 7 Pre Increment
cout << "Post-decrement: " << y-- << endl; // 6 (y becomes 5) Post Decrement
cout << "Pre-decrement: " << --y << endl; // 4 Pre Decrement
b. Relational Operators:
These operators are used for the comparison of the values of two operands. It
includes (>, <, >=, <=, !=, ==).
For example: ‘>’ checks if one operand is greater than the other operand or not,
Department of Computer Science (SST), UMT.
11
etc. The result returns a Boolean value, i.e., true (1) or false (0).
Example: Suppose A=5 and B=10.
OPERATORS DESCRIPTION EXAMPLE
> Checks which operand is (A>B) False
greater than the other. (B>A) True
<
Checks which operand is (A<B) True
smaller. (A<B) False
>= Checks whether the operand is (A>=B) False
greater than or equal to the other (B>=A) True
operand.
<= Checks whether the operand is (A<=B) True
smaller than or equal to the other (B>=A) False
operand.
!= Checks whether both the
operands are not equal. (A!=B) True
== Checks whether both the
operands values is equal. (A==B) False
c. Logical Operators:
These operators are used to combine two or more conditions or constraints or to
complement the evaluation of the original condition in consideration.
It includes (&&, ||, !). The result returns a Boolean value, i.e., true (1) or false
(0).
Example: Suppose if X=10 and Y=5.
OPERATORS DESCRIPTION EXAMPLE
AND TRUE: If both the conditions (X==10 && Y==5) TRUE
are true.
Department of Computer Science (SST), UMT.
12
|| True: If only one condition is (X==10 && Y==8) TRUE
true.
! It reverses the state of its !(X==Y) TRUE
operand.
d. Bitwise Operators:
These operators are used to perform bit-level operations on the operands. The
operators are first converted to bit-level and then the calculation is performed on
the operands. Mathematical operations such as addition, subtraction,
multiplication, etc. can be performed at the bit level for faster processing.
It includes (Binary AND -> &, Binary OR -> |, Binary XOR ^, Left Shift <<,
Right Shift >>, One’s Complement ~).
Note: Only char and int data types can be used with Bitwise Operators.
e. Assignment Operators:
These operators are used to assign value to a variable. The left side operand of
the assignment operator is a variable and the right-side operand of the
assignment operator is a value. The value on the right side must be of the same
data type as the variable on the left side otherwise the compiler will raise an
error.
It includes (=, +=, -+, *=, /+).
Department of Computer Science (SST), UMT.
13
f. Ternary or Conditional Operators:
This operator returns the value based on the condition.
Condition? Expression1: Expression2
The ternary operator ‘?’ determines the answer on the basis of the evaluation
of Condition. If it is true, then Expression1 gets evaluated.
If Condition is false, then Expression2 gets evaluated.
This operator takes three operands; therefore, it is known as a Ternary Operator.
Syntax: condition ? expression_if_true : expression_if_false;
Example Program
Here’s an example to find the maximum of two numbers using the ternary
operator:
#include <iostream>
Department of Computer Science (SST), UMT.
14
using namespace std;
int main() {
int num1 = 10;
int num2 = 20;
int max;
// Using ternary operator to find the maximum
(num1 > num2) ? max = num1 : max = num2;
cout << "The maximum number is: " << max << endl;
return 0;
}
Output:
Arithmetic Expressions:
Arithmetic expressions contain a combination of the arithmetic operations
including brackets:
x = z – (a + b / 2) + w * y
To evaluate the expression, we need to follow the precedence rule which is as
follows:
• ( ) expression within parentheses.
• +, - unary operators (plus and minus sign).
Department of Computer Science (SST), UMT.
15
• *, /, % multiplication, division and remainder are evaluated from left to
right.
• +, - addition and subtraction are evaluated from left to right.
Department of Computer Science (SST), UMT.
16
Examples:
Here are the example programs:
Understanding Precedence
Output:
Department of Computer Science (SST), UMT.
17
Understanding Associativity
Output:
Department of Computer Science (SST), UMT.
18
Lab Tasks:
Task 1: Write a C++ program that uses cin and cout to display your name, favorite
color, and a simple message. (also use comments and any two escape characters)
Task 2: Write a program to Swap two numbers in C++.
Task 3: Write a program that take six subjects marks from User and Display the
sum of all marks. (also use const like fixed value that total marks = 600)
Task 4: Write a C++ program taking two values as inputs from the user and display
the results for all the basic arithmetic operations performed on them.
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modulus (%)
Task 5: Write a C++ program to find the minimum of two numbers that are taken
from user by using ternary operator.
Task 6: What will be the output of the following both program: (Do it on Paper
don’t use compiler)
Department of Computer Science (SST), UMT.
19
Home Tasks:
Task 1: Write a C++ program to find the Area and Perimeter of a Rectangle.
Task 2: Write a C++ program that converts a temperature from Celsius to
Fahrenheit. Use the following formula:
F = 1.8 x C + 32.
Task 3: Write a C++ program that reads three integers representing hours, minutes,
and seconds of a time. Then it should calculate the equivalent time in seconds.
Task 4: What will be the output of the following program: (Do it on Paper don’t use
compiler)
Department of Computer Science (SST), UMT.
20
(1)
(2)
(3)
Department of Computer Science (SST), UMT.
21
Department of Computer Science (SST), UMT.