Min menu

Pages

VB

Loops in C++

 Episodes in C Plus Plus C++

We use loops Loops ) In order to repeat the same code multiple times.
So any code we want to be executed several times, we write it inside a loop, and it re-executes the code as much as we want under certain conditions that we specify.


 Execution of commands and loops in memory

Commands are usually executed in a sequence after each other, but the loops make the order execution arrow stop at it and it executes the orders inside it several times, and after it exits the loop it returns and completes the implementation of the rest of the commands after it as follows.

Loops in C++ | C++ loop

When the loop is executed, the commands placed in it are executed separately from the rest of the commands in the program, that is, you can consider that all the loop commands are placed in a special place in the memory, this place is called scope.

After you have executed all the loop commands in this the scope, . is erased the scope entirely from memory and this process is called Destroy.

 Types of loops in C++

Episode nameIndications
For LoopThe loop is used  for if the number of times the code will be re-executed is known.
Continue reading for episode in C Plus Plus
While LoopIt is preferable to use a loop  while if the number of times the code will be re-executed is unknown.
Continue reading the while episode in C Plus Plus »
Do While LoopIt is preferable to use the loop  do while if the number of times the code will be re-executed is unknown and at the same time it must be executed at least once.
Continue reading the do while episode in C Plus Plus »

Control sentences with loops in C++

We use control clauses Control Statements ) To control the flow of loops and with the condition clause switch.


control clauseits definition
Break StatementIt is used in loops and in a sentence switch.
Once the sentence is executed  break , it stops the scope It is completely removed from it and erased from memory and then moved to the next code in the program.
Continue reading break statement in C++ »
Continue StatementUse with rings only.
We use it to override a specific code in the loop, so we use it to override a piece of code the scope.
Continue reading the continue statement in C++ »
_______

Episode  for in C Plus Plus C++

Episode definition for

We use the loop  for if we want to execute the code several specific times. For example, if we want to execute a specific code  10  times, we put it in a loop that repeats itself  10  cycles.


 Using the for loop in C++

  for( initialisation; condition; increment or decrement )
		{
		// statements
		}
  

  • initialisation : is the first step in the loop that is executed only once, unlike all the elements in the loop.
    In this step, we define a variable (called a counter) and put . after it ;.


  • condition : It is the second step that is executed in the loop and it is executed in each cycle.
    In this step, we set a condition that determines when the loop stops. In each cycle, it is first checked if this condition is met or not, and we put after it  ;.
    Here as long as the result of the condition is equal  true the code will re-iterate.


  • statements : is the third step, it means to execute all the commands in the loop and it is executed in each cycle.
    After all commands are executed, it will climb to the last step that occurs at the end of each cycle, which is either increasing or decreasing the value of the counter.


  • decrement  or  increment : It is the fourth and final step, and it is implemented in each cycle.
    Here we specify how the value of the counter increases or decreases, and we do not put after it ;.


Just remember that all these steps are repeated in each cycle except for the first step, and the reason is that we do not need to define a new counter in each cycle, but rather use the old counter through which we know in which cycle we have become.

Example of the episode  for in C Plus Plus

In the following example we have defined a loop that prints all numbers from  1  to 10 .

Example

Main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Consists of 10 cycles. In each cycle it prints the value of the counter used in for here we have created a loop
		  for( int i=1; i<=10; i++ )
		  {
		  cout << i << endl;
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    1
		  2
		  3
		  4
		  5
		  6
		  7
		  8
		  9
		  10
	

Episode  while in C Plus Plus C++

Definition of the episode  while in C Plus Plus C++

We use the loop  while if we want the code to be executed multiple times, but we don't know exactly how many because we want to stop the execution if a certain condition is met.
This loop stops repeating itself if the condition we set for it is met.

Here it is as if we say: "As long as the condition is not met, it continues to repeat the code."


 Use the loop  while in C++

                
		initialisation;
	
		while( condition )
		{
		    // statements
	
		    increment or decrement;
		}
	
	
	
  

  • initialisation : is the first step in the loop that is executed only once, unlike all the elements in the loop.
    In this step we define a variable (called a counter).


  • condition : It is the second step that is executed in the loop and it is executed in each cycle.
    In this step, we set a condition that determines when the loop will stop. In each cycle, it is first checked if this condition is met or not.
    Here as long as the result of the condition is equal  true the code will re-iterate.


  • statements : is the third step, it means to execute all the commands in the loop and it is executed in each cycle.


  • decrement  or  increment : It is the fourth and final step, and it is implemented in each cycle.
    Here we define how the counter's value increases or decreases.


Just remember that all these steps are repeated in each cycle except for the first step, and the reason is that we do not need to define a new counter in each cycle, but rather use the old counter through which we know in which cycle we have become.

Example of a loop  while in C-Plus

In the following example we have defined a loop that prints all numbers from  1  to 10 .

Example

Main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we have defined the variable we used as counter in the loop
		  int i=1;
	  
		  // it keeps executing the commands placed in it as long as the value of the number is still less than or equal to 10 while here we created a loop
		  while( i<=10 )
		  {
		  // In each cycle the counter value will be printed and then 1 will be added to it
		  cout << i << endl;
		  i++;
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    1
		  2
		  3
		  4
		  5
		  6
		  7
		  8
		  9
		  10
	

Episode  do while in C Plus Plus C++

Definition of the episode  do while in C Plus Plus C++

We use the loop  do while if we want the code to be executed multiple times, but we don't know exactly how many because we want to stop the execution if a certain condition is met.
This loop stops repeating itself if the condition we set for it is met.

The only difference between it and the loop  while is that it is executed at least once because it checks the condition after the commands are executed and not before them.

Here it is as if we say: "As long as the condition is not met, it continues to repeat the code."


 Use the loop  do while in C++

                  
		initialisation;
	
		do{
		    // statements
		    increment or decrement;
		}
		While( condition );
	
  

  • initialisation : is the first step in the loop that is executed only once, unlike all the elements in the loop.
    In this step we define a variable (called a counter).


  • statements : is the second step, it means to execute all the commands in the loop and it is executed in each cycle.


  • decrement  or  increment : It is the third step, and it is implemented in every cycle.
    Here we define how the counter's value increases or decreases.


  • Condition : It is the fourth and final step and it is implemented in each cycle.
    In this step, we set a condition that determines when the loop will stop. At the end of each cycle, it is checked if the condition is met or not.
    Here as long as the result of the condition is equal  true the code will re-iterate.


Just remember that all these steps are repeated in each cycle except for the first step, and the reason is that we do not need to define a new counter in each cycle, but rather use the old counter through which we know in which cycle we have become.

Example of a loop  do while in C-Plus

In the following example we have defined a loop that prints all numbers from  1  to 10 .

Example

Main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  int main()
		  {
		  // Here we have defined the variable we used as counter in the loop
		  int i=1;
	  
		  // it keeps executing the commands placed in it as long as the value of the number is still less than or equal to 10 while here we created a loop
		  do
		  {
		  // In each cycle the counter value will be printed and then 1 will be added to it
		  cout << i << endl;
		  i++;
		  }
		  while( i<=10 );
	  
		  return 0;
		  }
	

We will get the following result when running.

                    1
		  2
		  3
		  4
		  5
		  6
		  7
		  8
		  9
		  10
	
_______