C++ While loop –

|
In C++ programming language, loops are an important concept to iterate over a group of statements until a specific condition is met. While loop is one of the loops that is known for its simplicity and versatility. In C++, a while loop is used to iterate a part of the program several times. If the number of iterations is not fixed, it is recommended to use a while loop than for loop. Syntax:It has the following syntax: Where,
Flow Diagram of While Loop:
C++ While Loop Examples:Print integer value from 1 to 10 using While LoopLet us take a simple example of a while loop to print number 1 to 10. ExampleCompile and Run Output: 1 2 3 4 5 6 7 8 9 10 Explanation: In this example, we print the integer value from 1 to 10 using a while loop. Here, we initialize an integer value (i=1). After that, it checks the condition (1<=10) in the loop. If the condition is true, it prints the value of i. If the condition is false, it exits the loop. Factorial Number Program using While Loop:ExampleCompile and Run Output: C++ Nested While Loop:In C++, we can use a while loop inside another while loop, which is known as a nested while loop. The nested while loop is executed fully when the outer loop is executed once. Simple Nested while Loop ExampleLet us take a simple example of a nested while 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 Print Multiplication Table using While Loop in C++ExampleCompile and Run Output: Enter the starting number: 2 Enter the ending number: 3 Multiplication Table of 2: 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 Multiplication Table of 3: 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 C++ Infinitive While Loop:We can also create an infinite while loop by passing true as the test condition. It means that if we pass the condition to true (while(true)) in the while loop, it would be an infinitive while loop. Syntax: It has the following syntax: In this syntax, this loop will continue because the condition is always true. Simple Example for Infinite While LoopLet us take a simple example to illustrate the C++ Infinitive While Loop: ExampleCompile and Run Output: Infinitive While Loop Infinitive While Loop Infinitive While Loop Infinitive While Loop Infinitive While Loop Print pyramids using Infinite while loop in C++ExampleCompile and Run Output: * * * * * * * * * * * * * * * Controlling While Loop Execution:
The break statement immediately exits the loop. ExampleCompile and Run Output: 1 2 3 4
The continue statement skips the current iteration and moves to the next.
ExampleCompile and Run Output: 1 2 3 4 5 6 7 8 9 10 Conclusion:In C++, the while loop is an essential tool for repeating actions in C++ when the number of iterations is unknown. Mastering while do-while and nested loops enables developers to write efficient and flexible programs. Proper understanding of loop termination, optimization techniques, and best practices can lead to better performance and maintainability in programming. Frequently Asked Questions:1. In C++ programming, which methods are used to terminate a while loop? The termination of a while loop occurs through these statements:
2. List out the major components of a while loop. A while loop consists of:
3. What specific situations lead us to adopt while loops over for loops? A while loop should be used whenever any of the following circumstances apply:
4. What are the pros and cons of using a while loop in C++? Advantages:
Disadvantages:
5. Does while loop accept multiple conditional checks? Multiple conditions can be implemented in loops through logical operators (&&, ||). Example: The loop continues execution until both values meet the conditions x ≤ 5 AND y ≥ 6. Next TopicC++ Do-While Loop
|




