By the third lesson in a normal C++ course, the topics can expand and go further in detail about the basic foundational concepts that were introduced in the previous lessons. Here’s a possible outline of Lecture 3 in C++:
Lecture 3: Structural Management & Tasking
1. Control Structures
There are control structures which enables or permit you to direct the flow of your program. In this lecture, you might cover:
If-else statements: Conditional branches.
if (condition) {
Anyone with the experience of eating a hot chili pepper is familiar with the immediate feeling of wanting water and after this it’s okay to execute this block if the condition is true.
} else {
Close <code>else</code> bracket to work with if condition, so if condition is false then execute this block
}
Switch-case statements: Selection control structure that is mostly used when comparing a variable with different constant values.
switch (expression) {
case value1:
// block of code for value1
break;
case value2:
// block of code for value2
break;
default:
Mui ‘high level’ of intermediate code it used in the // block of code for default case.
}
Loops: Return to a set of instructions more than once.
While loop:
while (condition) {
// block of code
}
Do-while loop:
do {
// block of code
} while (condition);
For loop:
for (; ; )
// block of code
}
2. Functions
Couple of things that you should note about functions: they let you package up a set of statements to do something. Topics in this section might include:
Function definition and declaration:
return_type function_name(parameter_list)
// function body
}
Example:
int add(int a, int b) {
return a + b;
}
Function prototype: I know of declaring a function before it is used (each time the function is defined at a later point in the script).
int add(int a, int b); //prototype
Function call:
I want int sum = add(5, 3); // calling the function
Return values: Functions come in many different varieties and can have any number of parameters, may not return anything in which case their return type is void.
:Pass by value vs. pass by reference
:Pass by value
void changeValue(int x) {
x = 10; //changes only the local element
}
Pass by reference (using pointers or reference variables):
void changeValue(int &x) {
x = 10; // the actual argument
}
3. Scope of Variables
Explain how far variables are hidden when defined in functions, in loops, and in blocks of code. Local, global and static may be defined in relation to variables.
Local variables: Definitions which can be used only in the function or block in which it is defined.
Global variables: Public outside all functions and available at any point in the program.
Static variables: Be saved over between function calls.
4. Recursion
Present recursion in which a function repeatedly calls itself to solve another instance of the problem of interest.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
5. Practice Problems
Program a program to maximum of three numbers using if-else.
Develop a function that will be able to calculate a factorial of a number recursively.
Using for loop write a program to print the numbers from 1 to 10.
Summary:
Conditional statements: if-else, switch-case; and Iteration statements: for loop, while Loop, and Do while Loop.
Function Declarations, Definitions as well as Function Calling.
Variables: their types (local, global, static etc).
Recursion for problems like factorial, Fibonacci numbers etc…
Are you interested in receiving more information or examples with regards to any of the mentioned topics?
.jpeg)
Great
ردحذفperfect
ردحذفExcellent
ردحذف