





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






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