Break c что это

от admin

Break c что это

The break in C is a loop control statement that terminates a loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time.

Syntax:

The following three types of loops will be used with break statements:

  • Simple Loops
  • Nested Loops
  • Infinite Loops

Break Statement with Simple Loops

Break statements in C++ can be used with simple loops i.e, for loops, while loops, and do-while loops.

Example 1: Break statement with for loop

Working of break statement in for loop

Example 2: Break statement with the while loop

Working of Break in while Loop

Example 3: Break statement with a do-while loop

working of break in do-while loop

Working of break in do-while loop

Break Statement with Nested Loops

Break statements can also be used when working with nested loops. The control will come out of only that loop in which the break statement is used.

Example:

Note: Break statement only breaks out of one loop at a time. So if in nested loop, we have used break in inner loop, the control will come to outer loop instead of breaking out of all the loops at once. We will have to use multiple break statements if we want to break out of all the loops.

Break Statement with Infinite Loops

An Infinite loop can be terminated with a break statement as a part of the condition.

Output:

Note: Please do not run the above program in your compiler as it is an infinite loop so you may have to forcefully exit the compiler to terminate the program.

In the above program, the loop condition is always true, which will cause the loop to execute indefinitely. This can be corrected by using the break statement as shown below:

Example:

Iteration of the loop is restricted to 8 in the above code with the help of a break statement.

Break statement with Switch case

In general, the Switch case statement evaluates an expression, and depending on the value of the expression, it executes the statement associated with the value. Not only that, all the cases after the matching case after the matching case will also be executed. To prevent that, we can use the break statement in the switch case as shown:

break statement

Causes the enclosing for, range-for, while or do-while loop or switch statement to terminate.

Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.

Contents

[edit] Syntax

attr   (optional) break ;

[edit] Explanation

After this statement the control is transferred to the statement immediately following the enclosing loop or switch. As with any block exit, all automatic storage objects declared in enclosing compound statement or in the condition of a loop/switch are destroyed, in reverse order of construction, before the execution of the first line following the enclosing loop.

[edit] Keywords

[edit] Notes

A break statement cannot be used to break out of multiple nested loops. The goto statement may be used for this purpose.

C++. Операторы break и continue. Особенности применения. Примеры использования

Операторы break и continue . Особенности применения. Примеры использования

Содержание

Похожие статьи