Saturday, April 6, 2024

Loops in c++



LOOPS IN
C++:

In Programming, some of the time there is a need to play out some activity at least a couple of times or (say) n number of times. Circles come into utilization when we want to execute a block of explanations more than once.

For instance: Assume we need to print "Hi World" multiple times. This should be possible in two ways as displayed underneath:

Manual Technique (Iterative Strategy):

Physically we need to compose cout for the C++ explanation multiple times. Suppose you need to compose it multiple times (it would unquestionably require greater investment to compose 20 articulations) presently envision you need to compose it multiple times, it would be truly chaotic to re-compose a similar assertion over and over. Along these lines, here circles play their part

// C++ program to Demonstrate the need of loops
#include <iostream>
using namespace std;
 
int main()
{
    cout << "Hello World\n";
    cout << "Hello World\n";
    cout << "Hello World\n";
    cout << "Hello World\n";
    cout << "Hello World\n";
    return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World


Time complexity: O(1)

Space complexity: O(1)

Using Loops:

In Circle, the assertion should be composed just a single time and the circle will be executed multiple times as displayed underneath. In PC programming, a circle is a grouping of directions that is rehashed until a specific condition is reached.
There are essentially two kinds of circles:
Section Controlled circles: In this kind of circle, the test condition is tried prior to entering the circle body. For Circle and keeping in mind that Circle is section controlled circles.
Exit Controlled Circles: In this sort of circle the test condition is tried or assessed toward the finish of the circle body. In this manner, the circle body will execute something like once, regardless of whether the test condition is valid or misleading. the do-while circle is exit controlled circle.







S.No.Loop Type and Description

1.

while loop

– First checks the condition, then executes the body.

2.

for loop

– firstly initializes, then, condition check, execute body, update.

3.

do-while

loop

– firstly, execute the body then condition check

nested loop is also very instresting in c++

No comments:

Post a Comment

last post

C language Vs C++ language in 2024

Popular Posts