0% found this document useful (0 votes)
5 views

Lecture 5

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 5

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Programming

Fundamentals

By: Engr. Raheel Zaman


1. Arithmetic Operators
 These operators help you perform
mathematical calculations.
 Addition (+): Adds two numbers.
 Subtraction (-): Subtracts the second number
from the first.
 Multiplication (*): Multiplies two numbers.
 Division (/): Divides the first number by the
second. If both are integers, the result will be
an integer (integer division).
 Modulus (%): Gives the remainder of the
division. Works only with integers.
Examples
 #include <iostream>
 using namespace std;
 int main() {
 int a = 15, b = 4;
 cout << "Addition: " << (a + b) << endl;
 cout << "Subtraction: " << (a - b) << endl;
 cout << "Multiplication: " << (a * b) << endl;
 cout << "Division: " << (a / b) << endl; // 15 / 4 = 3
(integer division)
 cout << "Modulus: " << (a % b) << endl;
 return 0; }
 In division, if either operand is a float or double, the
result is a decimal.
 Example:
double result = 15.0 / 4; // Result: 3.75
 Modulus (%) cannot be used with floating-point
numbers
 Example:
// Invalid: double a = 15.0 % 4.0;
2. Logical Operators
 Logical operators are used to evaluate conditions,
returning true or false.
 AND (&&): True only if both conditions are true.
 OR (||): True if at least one condition is true.
 NOT (!): Reverses the logical value (true becomes
false, and vice versa)
Examples:
 #include <iostream>
 using namespace std;

 int main() {
 int x = 5, y = -3, z = 10;
 cout << "x > 0 AND y > 0: " << (x > 0 && y > 0)
<< endl; // False (0)
 cout << "x > 0 OR y > 0: " << (x > 0 || y > 0) <<
endl; // True (1)
 cout << "NOT (z < 0): " << !(z < 0) << endl; //
True (1)
 return 0; }
 Logical operators are commonly used in conditional
statements like if, while, and for.
 Example:

 if (x > 0 && y > 0) {


 cout << "Both x and y are positive." << endl;
 }
3. Increment and Decrement
Operators
 These operators are shorthand for adding or
subtracting 1 from a variable.
 Explanation:
 Pre-increment (++a): Increments the variable
first, then uses its new value.
 Post-increment (a++): Uses the variable’s current
value first, then increments it.
 Pre-decrement (--a): Decrements the variable first,
then uses its new value.
 Post-decrement (a--): Uses the variable’s current
value first, then decrements it.
Example:
 #include <iostream>
 using namespace std;
 int main() {
 int a = 5;
 // Pre-increment
 cout << "Pre-increment: " << ++a << endl; // a = 6, prints 6

 // Post-increment
 cout << "Post-increment: " << a++ << endl; // prints 6, then
a=7
 cout << "After post-increment: " << a << endl; // a = 7
 // Pre-decrement
 cout << "Pre-decrement: " << --a << endl; // a =
6, prints 6

 // Post-decrement
 cout << "Post-decrement: " << a-- << endl; //
prints 6, then a = 5
 cout << "After post-decrement: " << a << endl; //
a=5
 return 0;
 }
 Use pre- when the updated value is needed
immediately.
 Use post- when the current value is needed before
updating
4. Operator Precedence
 Operator precedence decides the order of evaluation
in complex expressions. Operators with higher
precedence are evaluated first.
Example of Precedence
 #include <iostream>
 using namespace std;
 int main() {
 int a = 5, b = 10, c = 15;
 // Without parentheses
 int result1 = a + b * c; // Multiplication first: result1 = 5 + (10
* 15) = 155
 // With parentheses
 int result2 = (a + b) * c; // Parentheses first: result2 = (5 + 10)
* 15 = 225
 cout << "Result1: " << result1 << endl; // 155
 cout << "Result2: " << result2 << endl; // 225
 return 0;
 }
 Use parentheses: Parentheses improve readability and
control the order of operations.
 Remember precedence rules: For example, * is
evaluated before +.
 Test increment/decrement carefully: Misunderstanding
pre- and post-increment can lead to unexpected
behavior.
Variables in C++

 For example:
 int age = 25;

 int tells the computer that the variable will


store an integer (whole number).
 age is the name of the variable.
 25 is the value stored in the variable.
Rules for Naming Variables
 The variable name should only contain letters,
numbers, and underscores (_).
 It cannot start with a number.
 You cannot use spaces in variable names.
 Variable names are case-sensitive: Age and
age are different.
 You cannot use reserved keywords like int,
float, for, etc., as variable names.
 ✅ Valid: myAge, price1, name_of_book
 ❌ Invalid: 1age, my age, float
Types of Variables
 C++ supports different types of variables
depending on the kind of data they store.
Data Type Description Example
Whole numbers
int int x = 10;
(positive/negative)
float Decimal numbers float price = 5.5;

double Larger decimal numbers double pi = 3.14159;


Single characters
char char grade = 'A';
(letters/symbols)
bool True or false values bool isHappy = true;
Sequence of characters
string string name = "John";
(words)
Declaring and Initializing
Variables
You can declare a variable (define its type and
name) and assign a value to it:
 Separate Declaration and Initialization:
 For Example:
 int age; // Declare the variable
 age = 25; // Assign value
Declaring and Initializing
Variables
 Combined Declaration and Initialization

 int age = 25; // Declare and initialize in one


step
Changing the Value of Variables

 You can change the value of a variable later in


the program:
 For Example:
 int age = 25;
 age = 30; // Now the value of 'age' is updated
to 30
Constants
 If you don’t want the value of a variable to
change, use the const keyword. This creates a
constant variable:
 For Example:
 const float pi = 3.14;
 pi = 3.14159; // ❌ Error! You cannot change a
constant variable.
Input and Output with Variables
 You can take input from the user and display
output using cin and cout.

 Example:
 #include <iostream>
 using namespace std;

 int main() {
 int age;
 cout << "Enter your age: ";
 cin >> age; // User inputs a value
 cout << "You are " << age << " years old."
<< endl;
 return 0;
 }
Output Example:
 Enter your age: 25
 You are 25 years old.

You might also like