0% found this document useful (0 votes)
4 views11 pages

Unit 1 Introduction to C++

The document discusses control structures in C++, specifically focusing on looping and conditional statements. It explains the differences between while, for, and do...while loops, along with programming tips for using conditional statements effectively. Key recommendations include using else-if for mutually exclusive conditions, avoiding deep nesting, and ensuring clarity in loops.

Uploaded by

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

Unit 1 Introduction to C++

The document discusses control structures in C++, specifically focusing on looping and conditional statements. It explains the differences between while, for, and do...while loops, along with programming tips for using conditional statements effectively. Key recommendations include using else-if for mutually exclusive conditions, avoiding deep nesting, and ensuring clarity in loops.

Uploaded by

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

Nested Control Structures (continued)

• What is the result if we replace the first for statement with the
following?
for (i = 5; i >= 1; i--)

• Answer:
*****
****
***
**
*
Summary

• C++ has three looping (repetition) structures:


• while, for, and do…while
• while, for, and do are reserved words
• while and for loops are called pretest loops
• do...while loop is called a posttest loop
• while and for may not execute at all, but do...while
always executes at least once
Summary (continued)
• while: expression is the decision maker, and the
statement is the body of the loop
• A while loop can be:
• Counter-controlled
• Sentinel-controlled
• EOF-controlled
• In the Windows console environment, the end-of-
file marker is entered using Ctrl+z
Summary (continued)
• for loop: simplifies the writing of a counter-controlled while loop
• Putting a semicolon at the end of the for loop is a semantic error
• Executing a break statement in the body of a loop immediately
terminates the loop
• Executing a continue statement in the body of a loop skips to the next
iteration
Programming Tips for Conditional Statements
Prefer else-if : Use else if for mutually exclusive conditions to avoid unnecessary
checks.

if (score >= 90) {


cout << "Grade A";
} else if (score >= 80) {
cout << "Grade B";
} else {
cout << "Grade C";
}

Use switch for Fixed Values : Switch is ideal for comparing a variable against fixed
values.

switch (choice) {
case 1: cout << "Option 1"; break;
default: cout << "Invalid choice"; break;
}
Programming Tips for Conditional Statements
Avoid Deep Nesting : Refactor nested conditions to improve readability.

if (a <= 0 || b <= 0) return;


cout << "Valid values";

Use Ternary Operator : For simple conditions, use the ternary operator.

int result = (a > b) ? a : b;

Handle Floating-Point Comparisons : Avoid direct equality checks.

if (fabs(a - b) < 1e-6) {


cout << "Nearly equal";
}
Programming Tips for Conditional Statements
Always Use Braces : Even single-line conditions should use braces.

if (x > 0) {
cout << "Positive";
}

Use Logical Operators Wisely : Leverage short-circuiting behavior.

if (ptr != nullptr && *ptr == 5) {


cout << "Valid pointer";
}

Avoid Duplicate Conditions : Avoid repeating conditions unnecessarily.

if (x > 0) {
cout << "Positive";
}
Programming Tips for Conditional Statements
Use Constants for Magic Numbers : Use named constants instead of hard-coded values.

const int MAX = 100;


if (score == MAX) {
cout << "Perfect!";
}

Test Edge Cases : Ensure conditions handle all edge scenarios.

if (number < 0) {
cout << "Negative";
}
Programming Tips for Looping Statements
Choose the Right Loop : Use `for`, `while`, and `do-while` loops appropriately based
on the scenario.

for (int i = 0; i < 10; i++) {


cout << i << " ";
}

Use `while` for Conditional Loops : `While` is ideal when the number of iterations
isn't known beforehand.

while (condition) {
cout << "Running loop";
condition = false;
}

Use `do-while` for At Least One Iteration : `Do-while` ensures the block executes at
least once.

do {
cout << "Executed once";
} while (condition);
Programming Tips for Looping Statements
Use Break and Continue Wisely : Use `break` to exit a loop early and `continue` to
skip the current iteration.

for (int i = 0; i < 10; i++) {


if (i == 5) break;
if (i % 2 == 0) continue;
cout << i << " ";
}

Avoid Off-By-One Errors : Be cautious with loop boundaries to prevent errors.

for (int i = 0; i <= 10; i++) {


cout << i << " ";
}

Use Constants for Loop Limits : Avoid hard-coded values in loop conditions.

const int LIMIT = 10;


for (int i = 0; i < LIMIT; i++) {
cout << i << " ";
}
Programming Tips for Looping Statements
Keep Loops Simple and Clear : Avoid overly complex nested loops and keep your logic
straightforward.

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


for (int j = 0; j < 5; j++) {
cout << i * j << " ";
}
}

You might also like