PF Lec 3 To 7
PF Lec 3 To 7
1. Introduction to Variables 2. The if-else Statement:if (marks >= 50) cout Differences between if-else statements and
<< "Pass";else cout << "Fail"; switch statements in terms of use cases.
Variables are containers for storing data values.
Common pitfalls and mistakes when using if-
Every variable in C++ must be declared with a else statements in C++.
data type before use.
Exercises to practice writing if-else statements
Syntax:int age = 25; for various conditions.
2. Rules for Naming Variables Debugging common errors encountered with if-
else statements
Must begin with a letter or underscore
Understanding the concept of nested if
No special characters or spaces statements and their syntax.
6. Real-life Example:int units = 350; oat rate = 5. The switch Statement:switch(choice) { case 6. Real-Life Example: Result Calculator: oat
7.5; oat total = units * rate; 1: cout << "Tea"; break; case 2: cout << "Coffee"; calculatePercentage(int totalMarks, int
break; default: cout << "Invalid";} obtainedMarks) { return (obtainedMarks *
7. Scope of Variables 100.0) / totalMarks;}
6. Real-life Analogy: Exam Grading based on
Local variables are accessible only within the marks. 7. Recursion in Functions
block they are de ned.
7. Short-circuit Evaluation A function that calls itself to solve a problem.
Global variables can be accessed from any part
of the program. In logical expressions, evaluation stops as soon Useful for tasks like calculating factorials or
as the result is determined. Fibonacci sequences.
Programming
Lecture 3: Variables and Data Types
Fundamentals Lecture in C++
Lecture 4: Operators in C++ Lecture 5: Conditional Statements Lecture 6: Loops in C++ Lecture 7: Functions in C++
3 to 7
1. What Are Operators? 1. What is Looping?Repeating a block of code
multiple times.
Symbols that perform operations on variables
and values. 2. The for Loop:for (int i = 0; i < 5; i++) { cout << i
<< " ";}
2. Types of Operators
3. The while Loop:int i = 0;while (i < 5) { cout << i
Understanding the use of arithmetic operators << " "; i++;}
in various scenarios, such as calculating
Exaples and scenarios were discussed during averages and totals. 4. The do-while Loop:int i = 0;do { cout << i << "
Rough notes for Lecture 3,4,5,6,7 "; i++;} while (i < 5);
the class for each of the lectures
Exploring the concept of operator precedence
and how it affects the outcome of expressions 5. Infinite Loops:while (true) { ... }
involving multiple operators.
6. Loop with User Input:int num;cin >>
Practical exercises involving the use of num;while (num != 0) { cout << "You entered: "
arithmetic operators to solve real-world << num << endl; cin >> num;}
problems, such as budgeting or calculating
distances. 7. Loop Control Statements
Examples of using the modulus operator (%) to break and continue can alter the flow of loops.
a) Arithmetic Operators:+, -, *, /, %int a = 10, b =
determine even or odd numbers based on user
3;cout << a % b; // Output: 1 break exits the loop, while continue skips to the
input.
next iteration.
Code snippets demonstrating the use of
arithmetic operators in conjunction with
variables and user input.