LEC IV
REPETITION LOGIC
These structures play a crucial role in determining how a program flows based on specific
conditions. Here are the key concepts and examples:
1. Sequential Logic (Sequential Flow):
- Sequential logic follows a linear flow, executing instructions in the order they are given.
- Modules are executed one after the other unless new instructions alter the sequence.
- This basic flow pattern is common for most processing tasks.
- Example: A simple program that reads input, processes data, and produces output.
#include <iostream>
using namespace std;
int main() {
string name;
int age;
// Step 1: Read input from the user
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
// Step 2: Process the data
int birth_year = 2024 - age;
// Step 3: Produce output
cout << "Hello, " << name << "! You were born around " << birth_year << "." << endl;
return 0;
In this C++ program:
1. We declare a `string` variable `name` to store the user's name and an `int` variable `age`
to store their age.
2. We read input using `cin` (similar to Python's `input()`).
3. The calculation of the birth year remains the same.
4. We use `cout` to print the output message.
2. **Selection Logic (Conditional Flow)**:
- Selection logic involves conditions that determine which module to execute.
- Conditional structures include:
- **Single Alternative**:
- If a condition is met, execute a specific module.
- Example: Implementing an `if` statement in C/C++ or Java.
- **Double Alternative**:
- Choose between two modules based on a condition.
- Example: Using an `if-else` statement in C/C++ or Java.
- **Multiple Alternatives**:
- Select from multiple modules based on different conditions.
- Example: Employing an `if-else if` ladder in C++
#include <iostream>
using namespace std;
int main() {
int i = 20;
if (i == 10)
cout << "i is 10";
else if (i == 15)
cout << "i is 15";
else if (i == 20)
cout << "i is 20";
else
cout << "i is not present";
return 0;
In this example:
- We initialize `i` to 20.
- The `if-else if` ladder checks the conditions in order.
- Since `i` is equal to 20, the third condition (`i == 20`) evaluates to true.
- The message "i is 20" is printed.
Here's another example with a range-based condition:
#include <iostream>
using namespace std;
int main() {
int i = 25;
if (i >= 0 && i <= 10)
cout << "i is between 0 and 10" << endl;
else if (i >= 11 && i <= 15)
cout << "i is between 11 and 15" << endl;
else if (i >= 16 && i <= 20)
cout << "i is between 16 and 20" << endl;
else
cout << "i is greater than 20" << endl;
return 0;
In this second example, the program checks the value of `i` and prints the corresponding
message based on the specified
3. **Iteration Logic (Repetitive Flow)**:
- Iteration logic involves loops that repeat a set of instructions.
- Two common loop structures:
- **Repeat-For Structure**:
- Form: `Repeat for i = A to N by I: [Module]`
- The loop executes the module from `A` to `N`, incrementing by `I`.
- Example: Using a `for` loop in C/C++ or Java.
- **Repeat-While Structure**:
- Form: `Repeat while condition: [Module]`
- The loop continues as long as the specified condition holds true.
- Example: Implementing a `while` loop in C++.
#include <iostream>
int main() {
int count = 1; // Initialize a counter
// Execute the loop while the condition (count <= 5) is true
while (count <= 5) {
std::cout << "Iteration " << count << ": Hello, World!" << std::endl;
count++; // Increment the counter
return 0;
In this example:
- We initialize a counter variable `count` to 1.
- The `while` loop runs as long as `count` is less than or equal to 5.
- Inside the loop, we print a message and increment the counter.
- The loop will execute 5 times, printing "Hello, World!" for each iteration.
HOMEWORK:
What is the difference between a while loop and a do-while loop?
FUNCTIONS
1. **Functions**:
- Functions are subprograms designed to perform a specific task and return a value.
- They can take input parameters (arguments) and compute a result.
- Example function definition:
int times(int x, int y) {
int p = 0;
while (y > 0) {
if (y % 2 == 0) {
y /= 2;
x *= 2;
} else {
p += x;
y--;
return p;
```
- Functions are used as operations within expressions:
int i = times(3, i + 2) + 1;
2. **Procedures**:
- Procedures are subprograms designed to perform a series of commands without returning a
value.
- They can also take input parameters.
- Example procedure definition:
void factors(int x) {
int f = 2;
while (x != 1) {
if (x % f == 0) {
cout << f << endl;
x /= f;
} else {
f++;
```
- Procedures are used as statements:
factors(i);
3. **Parameter Passing**:
- Parameters can be passed by value (call-by-value) or by reference (call-by-reference).
- Call-by-value makes a copy of the argument, while call-by-reference passes the memory
location.
- Example:
void p(int x, int& y) {
// Code for p
4. **Benefits of Subprograms**:
- Increases readability: Programs are better structured and easier to understand.
- Enables program design abstraction.
- Facilitates code reuse.
Functions allow us to break down complex tasks into smaller, reusable chunks of code. We'll
cover both basic function examples and more advanced concepts.
1. **Basic Function Example**:
- A simple function that displays a text message:
#include <iostream>
using namespace std;
// Function declaration
void greet() {
cout << "Hello there!";
int main() {
// Calling the function
greet();
return 0;
- Output:
Hello there!
2. **Function with Parameters**:
- A function that takes parameters (arguments):
#include <iostream>
using namespace std;
// Function to display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1 << endl;
cout << "The double number is " << n2 << endl;
int main() {
int num1 = 5;
double num2 = 5.5;
// Calling the function with arguments
displayNum(num1, num2);
return 0;
- Output:
The int number is 5
The double number is 5.5
3. **Function with Return Value**:
- A function that computes the sum of two integers and returns the result:
#include <iostream>
using namespace std;
// Function to add two numbers
int add(int a, int b) {
return a + b;
int main() {
int x = 10, y = 20;
// Calling the function and storing the result
int result = add(x, y);
cout << "Sum: " << result << endl;
return 0;
- Output:
Sum: 30
4. **Function Templates**:
- A template function that works with different data types:
#include <iostream>
using namespace std;
// Template function to find the maximum of two values
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
int main() {
int intMax = max(10, 20);
double doubleMax = max(3.14, 2.71);
cout << "Max int: " << intMax << endl;
cout << "Max double: " << doubleMax << endl;
return 0;
- Output:
Max int: 20
Max double: 3.14
HOMEWORK:
i. What is the difference between a function and a procedure?
ii. Can you give me an example of passing parameters by reference in C++?
iii. How can I use subprograms to improve my code's efficiency?
// END OF LEC IV