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

COSC 104 LEC IV FURTHER READING

The document explains the use of control structures in C++, specifically focusing on `for`, `while`, and `do-while` loops, detailing their syntax, components, and examples. It also discusses inbuilt functions provided by the C++ standard library and the creation of user-defined functions, highlighting their importance for code modularity and readability. Key differences between the loop types and guidance on when to use inbuilt versus user-defined functions are also provided.

Uploaded by

pitahmuriukh29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

COSC 104 LEC IV FURTHER READING

The document explains the use of control structures in C++, specifically focusing on `for`, `while`, and `do-while` loops, detailing their syntax, components, and examples. It also discusses inbuilt functions provided by the C++ standard library and the creation of user-defined functions, highlighting their importance for code modularity and readability. Key differences between the loop types and guidance on when to use inbuilt versus user-defined functions are also provided.

Uploaded by

pitahmuriukh29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LEC IV FURTHER READING

For Constructs

A `for` loop is a powerful control structure that allows you to repeat a block of code a specified number
of times. It's commonly used for iterating over sequences, such as arrays or ranges of numbers.

Here's how the `for` loop works:

1. **Syntax**:

c++

for (initialization; condition; increment/decrement) {

// Code to be executed

2. **Components**:

- **Initialization**: Executed once before the loop starts. Typically used to initialize a loop control
variable.

- **Condition**: Checked before each iteration. If true, the loop continues; otherwise, it terminates.

- **Increment/Decrement**: Executed after each iteration. Used to modify the loop control variable.

3. **Example**:

```cpp

#include <iostream>

using namespace std;

int main() {

// Print numbers from 1 to 5

for (int i = 1; i <= 5; i++) {

cout << i << " ";

return 0;

```

Output:
12345

4. **Characteristics**:

- The loop control variable (`i` in the example) is scoped to the loop.

- You can use any valid C++ expression for initialization, condition, and increment/decrement.

- The loop executes as long as the condition remains true.

- The `for` loop is versatile and widely used for various tasks.

The While and Do... While Constructs:


The **while loop** and the **do-while loop** constructs in **C++**. These loops allow us to
repeat actions based on specific conditions.
1. **While Loop (Condition-Controlled Loop)**:
- The `while` loop continues to execute as long as a given condition remains true.
- It checks the condition **before** entering the loop body.
- If the condition is initially false, the loop won't execute at all.
- Example:
```cpp
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << i << " ";
i++;
}
return 0;
}
Output:
12345
2. **Do-While Loop (Exit-Controlled Loop)**:
- The `do-while` loop executes the loop body **at least once**, regardless of the initial
condition.
- It checks the condition **after** executing the loop body.
- Useful for scenarios where you want to ensure at least one iteration (e.g., checking user
input).
- Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << " ";
i++;
} while (i <= 5);
return 0;
}
Output (same as above):
12345
3. **Key Differences**:
- While Loop: Condition checked **before** entering the loop.
- Do-While Loop: Condition checked **after** executing the loop body.
- Do-while loops are useful when you need to guarantee at least one iteration.
Remember, both loops are essential tools for controlling program flow based on conditions
Inbuilt Functions and User Defined Functions

1. **Inbuilt Functions (Standard Library Functions)**:


- In C++, the standard library provides a rich set of inbuilt functions that you can use directly
in your programs.
- These functions cover a wide range of tasks, from mathematical operations to input/output
handling.
- Examples of inbuilt functions:
- `printf()` and `scanf()` for formatted input/output.
- `sqrt()` for calculating the square root.
- `strlen()` for finding the length of a string.
- `toupper()` and `tolower()` for converting characters to uppercase or lowercase.

2. **User-Defined Functions**:
- User-defined functions are functions that you create yourself within your program.
- These functions serve specific purposes based on your program's requirements.
- Key points:
- You define the function name, parameters, and the code inside the function.
- User-defined functions enhance code modularity and readability.
- Example of a user-defined function:
```cpp
#include <iostream>
using namespace std;

// User-defined function to calculate the factorial of a number


int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}
```
- Output:
```
Factorial of 5 is 120
```

3. **Choosing Between Inbuilt and User-Defined Functions**:


- Use inbuilt functions when they provide the required functionality (e.g., mathematical
operations, string manipulation).
- Create user-defined functions for custom tasks specific to your program.
- Balance between using existing functions and creating new ones.

You might also like