C++ for Loop –

|
In C++, the for loop is an entry-controlled loop that is mainly utilized to iterate a part of the program multiple times. If the number of iterations is fixed, it is recommended to use for loop than while or do-while loops. Syntax:The C++ for loop is the same as other languages, such as C and C#. We can initialize variables, check conditions, and increment/decrement values.
Flowchart of for Loop:
As we can see in the flowchart, use the following steps to implement the for loop in C++: Step 1: First, go to the for loop and initialize the variables and their values. Step 2: Next, the control flow jumps to the condition statement. Step 3: Here, the condition is tested:
Step 4: After that, it goes to the update expression, where it increases or decreases the value by 1, and then again, it goes to step 3 to check the condition. Step 5: At the end, it exits from the loop and prints the output. Simple C++ for Loop Example:Let us take a simple example to print the numbers 1 to 10 using the C++ for loop. ExampleCompile and Run Output: 1 2 3 4 5 6 7 8 9 10 Factorial Program using C++ for Loop:Let us take another example to find the factorial of a given number using a for loop in C++. ExampleCompile and Run Output: Multiplication table program using for loop in C++Let us take an example to print a multiplication table using for loop in C++. ExampleCompile and Run Output: Enter a Number: 4 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40 C++ Nested for LoopIn C++, if we have a for loop inside another for loop, it is known as a nested for loop. The inner loop is completely executed when the outer loop executes. It allows us to perform complex iterations by executing a for loop for every iteration of the outer for loop. Nested for loops are beneficial when working with multi-dimensional arrays or patterns. Syntax:It has the following syntax: Flowchart of Nested for loop:
C++ Nested for Loop Example:Let us take a simple example of a nested for loop in C++. ExampleCompile and Run Output: 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 C++ Pyramid Program using Nested for Loop:Let us take an example to print a pyramid pattern using nested for loop in C++. ExampleCompile and Run Output: Enter the Number of rows: 6 * * * * * * * * * * * * * * * * * * * * * Explanation: In this example, we have taken two for loops. Here, the first loop handles the number of rows, and the second loop handles the number of columns and prints the stars. C++ Inverted Pyramid Program using Nested for Loop:Let us take an example to print an inverted pyramid pattern using nested for loop in C++. ExampleCompile and Run Output: Enter the Number of rows: 5 * * * * * * * * * * * * * * * Explanation: In this example, we have taken two for loops. Here, the outer loop handles the number of rows, and the inner loop handles the number of stars per row. C++ foreach LoopThe foreach loop is also known as the range-based for loop. It is mainly utilized to iterate over the elements of a container (such as list, array, vector, etc) in a simple way without performing initialization, increment/decrement, and testing. The ‘for’ keyword is utilized for the foreach loop in C++. Syntax:It has the following syntax: In this syntax,
C++ Foreach Loop Example:Let us take an example to illustrate the foreach loop in C++. ExampleCompile and Run Output: 20 25 30 35 40 Explanation: In this example, we have taken a vector library for using dynamic arrays. Next, we declare a vector of integers. After that, we use two for loops in this program. The first loop uses a reference to modify the vector elements, and the second loop prints the modified values. C++ Infinite for LoopIn C++, if we remove the initialization, condition, and update expression, it will result in an infinite loop. In other words, if we use a double semicolon in the for loop, it will be executed infinite times. Syntax:It has the following syntax: C++ Infinite For Loop Example:Let us take an example to illustrate the infinite for loop in C++. ExampleCompile and Run Output: Hello! Tpointtech Hello! Tpointtech Hello! Tpointtech Hello! Tpointtech Here, we need to press Ctrl+C to exit from the infinitive program. C++ for Loop MCQs1. In for loop, which of the following operators is utilized to separate expressions in C++?
Answer: (b) Semicolon (;) 2. Which of the following options is the first expression of for loop in C++?
Answer: (c) Initialization 3. What will be the output of the following code?
Answer: (a) 1 2 3 4 5 4. What type of loop is the for loop in C++ programming?
Answer: (d) Entry-controlled loop 5. Can we utilize the multiple initialization and update expression in a for loop in C++?
Answer: (a) Yes Next TopicC++ While loop
|





