Min menu

Pages

VB

The control break statement and the continue statement in C++

break control  clause C++

Definition of the sentence  break in C Plus Plus C++

The sentence  break is used in loops and in the sentence switch.
Once the sentence break is executed  , it stops the . scope It is completely removed from it and erased from memory and then moved to the next code in the program.

The break clause in C ++ Defining break clause in C ++ The break statement is used in loops and in the switch statement. Once you execute the break statement, it stops the entire scope, gets out of it, wipes it out of memory, and then moves to the next code in the program.

How to define a sentence   break in C++

This sentence consists of one command and is written on a single line.

                  break;
  

Example of a control clause  break in C++

In the following example, we have defined a loop that would have printed all numbers from  1  to  10 had  we not used the syntax break  to make the loop stop when the counter becomes  i 6  .

An example of the use of the break control clause in C++

Main.cpp
                    #include <iostream> 
	  
		  using namespace std; 
	  
		  int main() 
		  { 
		  // composed of 10 cycles. In each cycle, the value of the counter used for for is printed. Here we create a 
		  for loop ( int i=1; i<=10; i++ ) 
		  { 
		  // In each cycle the value of the counter will be checked and as soon as it becomes 6 the loop will be stopped permanently 
		  if ( i == 6 ) { 
		  break; 
		  } 
	  
		  cout << i << endl; 
		  } 
	  
		  return 0; 
		  }
	

We will get the following result when running.

                    1 
		  2 
		  3 
		  4 
		  5
	

So the sentence break  made the loop stop when the counter value became  i 6  .


Note when using the break control clause in C++

Here we tested the sentence break  with the loop for  only, but the principle is exactly the same with the loop while  and the loop  do while .

________

C++ Control Sentence  continue Plus C++

Definition of the sentence  continue in C Plus Plus C++

The sentence  continue is used with the . loops.
We use  continue the statement to override the execution of a certain code in the loop, so we use it to override part of the . code scope.

And we use it specifically to stop the current cycle and move on to the next cycle in the loop, don't worry you will understand the meaning of the example.

The continue control statement in C ++ Define a continue clause in C ++ The continue clause is used with loops. We use the continue clause to override the implementation of a specific code in the loop, so we use it to override part of the scope code. And we use it specifically to stop the current cycle and move to the next cycle in the loop. Don't worry, you will understand what is meant by the example.

How to define the sentence  continue in C++

This sentence consists of one command and is written on a single line.

                  continue;
  

Examples of control clause continue في C++

In the following example, we have defined a loop that prints all numbers from  1  to  10  except for the number 3 .
We used the sentence  continue to make the loop bypass the third period in the loop. That is, the print command will not be executed when the counter value  i becomes 3 .

The first example of the continue control statement in C++

Main.cpp
                    #include <iostream> 
	  
		  using namespace std; 
	  
		  int main() 
		  { 
		  // composed of 10 cycles. In each cycle the value of the counter used in for is printed. Here we create a 
		  for loop (int i=1; i<=10; i++) 
		  { 
		  // in each cycle the value of the counter will be checked, when it becomes 3 it will move to the next cycle in the loop without Execution of the next print command 
		  if (i == 3) { 
		  continue; 
		  } 
	  
		  cout << i << endl; 
		  } 
	  
		  return 0; 
		  }

	

We will get the following result when running.

                    1 
		  2 
		  4 
		  5 
		  6 
		  7 
		  8 
		  9 
		  10
	

So the sentence  continue made the loop skip the third cycle, so it did not print the number  3  because it did not execute the printing command in the third cycle.


In the following example we have defined a loop that prints all single numbers from  1  to 10 .
We use the syntax  continue to make the loop skip every cycle in which the counter  i is a single number.

The second example of the continue control statement in C++

Main.cpp
                    #include <iostream> 
	  
		  using namespace std; 
	  
		  int main() 
		  { 
		  // composed of 10 cycles. In each cycle the value of the counter used in for is printed. Here we create a 
		  for loop (int i=1; i<=10; i++) 
		  { 
		  // In each cycle the counter value will be checked, if it is a double, it will move to the next cycle in the loop without Execution of the next print command 
		  if (i%2 == 0) { 
		  continue; 
		  } 
	  
		  cout << i << endl; 
		  } 
	  
		  return 0; 
		  }
	

We will get the following result when running.

                    1 
		  3 
		  5 
		  7 
		  9
	

So the sentence continue  made the loop skip every cycle in which the counter value was a single number.


note

Here we tested the syntax  with continue  loops only, but the principle is exactly the same with loops   and  .for whiledo while

____ __