C++ Programming: Loops and Control Flow, Study notes of Computer science

A comprehensive introduction to loops in c++, covering the 'for', 'while', and 'do-while' loop constructs. It explains the syntax, functionality, and common use cases of each loop type, illustrating them with clear examples. The document also highlights potential pitfalls like infinite loops and off-by-one errors, emphasizing the importance of careful loop condition management. Practical exercises are included to reinforce understanding and encourage hands-on learning.

Typology: Study notes

2023/2024

Uploaded on 10/29/2024

carl-francisco
carl-francisco 🇭🇰

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Module 1-2: Computer Programming 2 C++
C: A procedural programming language focused on efficiency and low-level
programming.
C+: Not a recognized language; likely a typo or informal reference to C++.
C++: An extension of C that includes object-oriented features and other
enhancements, making it suitable for larger and more complex software projects.
What is C++?
C++ is a general-purpose programming language developed by Bjarne Stroustrup as an
extension of C, incorporating object-oriented features like classes and inheritance.
Known for its high performance, it is widely used in system programming, game
development, and real-time simulations. C++ supports generic programming through
templates and includes the Standard Template Library (STL) for common data
structures and algorithms. Its compatibility with C allows for code reuse, making it a
versatile language used in various industries, from software development to embedded
systems.
For Loop?
The ‘for’ loop in C++ is used to iterate over a range of values. It is particularly useful
when you know in advance how many times you want to execute a statement or a block
of statements.
for (initialization; condition; increment/decrement)
Initialization: This step is executed first and only once. It allows you to initialize any
loop control variables.
Condition: This is evaluated before each iteration. If the condition is true, the loop
continues; if false, the loop ends.
Increment/Decrement: This updates the loop control variable after each iteration.
Example: Let's look at an example where we print numbers from 1 to 10 using a ‘for’
loop.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C++ Programming: Loops and Control Flow and more Study notes Computer science in PDF only on Docsity!

Module 1-2: Computer Programming 2 C++ C : A procedural programming language focused on efficiency and low-level programming. C+ : Not a recognized language; likely a typo or informal reference to C++. C++ : An extension of C that includes object-oriented features and other enhancements, making it suitable for larger and more complex software projects. What is C++? C++ is a general-purpose programming language developed by Bjarne Stroustrup as an extension of C, incorporating object-oriented features like classes and inheritance. Known for its high performance, it is widely used in system programming, game development, and real-time simulations. C++ supports generic programming through templates and includes the Standard Template Library (STL) for common data structures and algorithms. Its compatibility with C allows for code reuse, making it a versatile language used in various industries, from software development to embedded systems. For Loop? The ‘for’ loop in C++ is used to iterate over a range of values. It is particularly useful when you know in advance how many times you want to execute a statement or a block of statements. for (initialization; condition; increment/decrement) Initialization : This step is executed first and only once. It allows you to initialize any loop control variables. Condition : This is evaluated before each iteration. If the condition is true, the loop continues; if false, the loop ends. Increment/Decrement : This updates the loop control variable after each iteration. Example: Let's look at an example where we print numbers from 1 to 10 using a ‘for’ loop.

Initialization : ‘int i = 1;’ initializes the loop control variable ‘i' to 1. Condition : ‘i <= 10;’ checks if i is less than or equal to 10. The loop will continue as long as this condition is true. Increment : ‘++i’ increments the value of ‘i’ by 1 after each iteration. Nested For Loops A ‘for’ loop can be nested inside another ‘for’ loop. This is useful for working with multi- dimensional data structures, such as matrices. Example: Here’s an example of a nested ‘for’ loop that prints a 5x5 grid of asterisks. Common Mistakes Infinite Loops : Forgetting to update the loop control variable or having a condition that never becomes false can lead to infinite loops.

Activity Write a nested ‘for’ loop to print the multiplication table for numbers 1 through 10.

While and Do-While Loops in C++ Loops are fundamental in programming for performing repetitive tasks. In C++, ‘while’ and ‘do-while’ loops are two types of loops used when the number of iterations is not known before the loop starts. While Loop A ‘while’ loop repeatedly executes a block of code as long as a specified condition remains true. It is useful when the number of iterations is not predetermined. Syntax Condition : Evaluated before each iteration. If true, the loop continues; if false, the loop terminates. Body : The block of code to be executed in each iteration. Example Print numbers from 1 to 5 using a ‘while loop’.

Explanation:Initialization : ‘int i = 1;’ sets the starting value of ‘i'.  Body : ‘cout << i << " ";’ prints the current value of ‘i'.  Increment : ‘++i;’ increases ‘i' by 1 in each iteration.  Condition : ‘while (i <= 5);’ ensures the loop continues while ‘i' is less than or equal to 5. Comparison While Loop : Checks the condition before executing the loop body. If the condition is false initially, the body may not execute at all. Do-While Loop : Checks the condition after executing the loop body, ensuring that the body executes at least once regardless of the condition. Common Use Cases While Loop : Used when the number of iterations is not known in advance and might not execute if the condition is initially false. Do-While Loop : Used when you need to ensure that the loop body executes at least once before checking the condition. Practice Exercise Solutions

  1. Finding First 10 Even Numbers with While Loop
  1. Reading Integers Until Negative Number with Do-While Loop Body of the Loop :  cout << "Enter an integer (negative number to quit): "; prompts the user to enter an integer.  cin >> number; reads the integer value from the user and stores it in number.  if (number >= 0) { ... } checks if the entered number is non-negative. If true, it prints the number. This check ensures that negative numbers are not printed but just terminate the loop. Condition : while (number >= 0);  After executing the loop body, the condition number >= 0 is checked. If number is non-negative, the loop continues. If number is negative, the loop terminates. Exit Message : cout << "Negative number entered. Exiting..." << endl;  Once the loop terminates (when a negative number is entered), this message is printed to indicate that the program is exiting.