Javatpoint Logo
Javatpoint Logo

MATLAB break

The break statement terminate the execution of a for loop or while loop. When a break statement is encountered, execution proceeds with the next statement outside of the loop. In nested loops, break exists from the innermost loop only.

Syntax:

Following are the points while using a break statement in MATLAB:

  • The break keyword is used to define a break statement.
  • The break statement terminates or stops the execution of the for or while loop and statements those coming after the break statement do not execute.
  • After the execution of the break statement, then control passes to the statement that follows the end of the loop.
  • If the break statement occurs in a nested loop, then it terminates only that particular loop, not the outer loop, and control passes to the statement that follows the end of that loop.
  • The break statement only affects the execution of the for or while loop; hence, it doesn't define outside the for or while

Flowdiagram of Break Statement

MATLAB Break Statement

Example1:

Output:

a = 66
    82    17    70    54    54    10
    27    18    70    66    33    27
    60    43    64    41    11    16
     3    10     4    82    62    29
    43    60     7    72    78    45
    32    48    32    97    43    53
program running smoothly
program running smoothly
program encounters the number 27, which is not useful for the current program;
at index no.:2
so loop terminates now.....bye bye

Example2:

Output:

a = 44
    0.2398   -1.6118    0.8617    0.5812
   -0.6904   -0.0245    0.0012   -2.1924
   -0.6516   -1.9488   -0.0708   -2.3193
    1.1921    1.0205   -2.4863    0.0799
negative number :-0.69036,found at index: 2,hence the program terminated

Program to terminate the flow of execution using the break statement.

Example:

Suppose we have a system that running on temperature variance. The temperature of the environment defines the working of the system. If the temperature of the environment passes beyond the hazardous limit, the program must stop the execution of the application that running the system.

The working temperature range varies according to some predefined conditions. So during summertime, when heat waves can damage the sophisticated system, or there is a drop in temperature below the specified limit during winter, we need to protect the system from getting it down.

The temperature range varies from -100° c to + 600°c.

The system is installed at different locations all over the world. Somewhere the temperature is measured in Celsius and somewhere it is measured in Fahrenheit. So we need to take care of these temperature units also.


Next Topic#




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA