Matlab

What are Loop Control Statements and how to use them in MATLAB?

Sometimes, we are required to execute a code block multiple times. Most of the time, this process is executed sequentially, which means the first statement in the given block of code is executed first, and so on for all other statements. These executions are normal executions.

The loop control statements are the statements used for changing the execution path. All programming languages including MATLAB support these statements.

This guide will teach us:

  • What are Loop Control Statements?
  • What is the continue Statement?
  • What is the break Statement?
  • Example 4: How to Use break Statement in Nested Loops?
  • How do the continue and break Statements Differ from Eachother?
  • Conclusion
  • What are Loop Control Statements?

    As we discussed before, the loop control statements are used to deal with complicated execution paths. These statements alter the normal sequence of executions. Whenever the executions leave the scope, all automatically created objects in the scope are destroyed. There are two types of loop control statements:

    • continue statement
    • continue statement

    What is the continue Statement?

    The continue statement is the control statement used for continuing the block of code by skipping some unwanted or unnecessary statements. In MATLAB, this statement forces the next iteration to take place by avoiding some statements to execute in the current iteration in the for or while loop. In the scope of nested loops, the continue statement is only implemented in the loop where it occurs.

    The working of the continue statement is described in the given flow-chart:

    Syntax
    The continue statement follows the given syntax in MATLAB:

    continue

    Example 1: How to Use continue Statement to Display Selective Elements?

    In this example, we find and display the values from 5 to 10. To do this, we initialize a while loop consisting of 16 iterations from 0 to 15. After that, we use the continue statement to find the values that do not lie between 5 and 10. The continue statement terminates the current statement if it finds any value i that is less than 5 or greater than 10 without displaying that value. As a result, we obtain the values from 5 to 10 on the screen.

    i=0;
    while i<=15
    i=i+1;
    if i10
    continue;
    end
    disp(i)
    end

    Example 2: How to Use continue Statement to Sum up the Selective Values?

    The given below MATLAB code sums up the even values lying between 0 to 20. To perform this task, we initialize a for loop containing 21 iterations from 0 to 20 and use the continue statement to terminate the iterations at which the value of i is odd. As a result, we obtain and print the partial sums of all even numbers lying between 0 and 20.

    s=0;
    for i=0:20
    if rem(i,2) ~= 0
    continue;
    end
    s=s+i;
    fprintf('Sum is %d\n', s);
    end

    Example 3: How to Use continue Statement to Find the Index and Occurrence of an Element in a Vector of Randomly Distributed Integers?

    This example creates a vector A of randomly distributed integers between 1 and 10 and starts the for loop to find the index of value 2 in the created vector A. If value 2 is found at the index i then the continue statement terminates the current iteration without executing the statements after it and the for loop starts the next iteration. Here, the variable count counts the occurrences of all other elements except 2 and finds occurrences of element 2 by subtracting the count of all other occurrences from the total length of the vector A. At last, it uses the fprintf() function to print the total occurrences of value 2.

    A = randi(10,1,10)
    count=0;
    for i=1:10
    if A(i)==2
    fprintf("2 occur at index %d\n",i)
    continue;
    end
    count=count+1;
    end
    n=length(A)-count;
    fprintf("\nOccurance of 2 in A is %d times",n)

    Example 4: How to Use continue Statement in Nested Loops?

    In this MATLAB code, we implement nested loops and continue statementto print a pattern of numbers. Here, we use two for loops, one for variable i and the second for variable j. After that, we specify a condition to execute the continue statement in the j-loop. When the condition is satisfied, the continue statement terminates the j-loop without affecting the i-loop.

    for i=1:10
    for j=1:10
    if i>j
    continue;
    end
    fprintf("%d ",j)
    end
    fprintf("\n")
    end

    What is the break Statement?

    The break statement is another type of loop control statements used for terminating the whole loop if the given condition is satisfied. This statement can be used in the for or while loop with the if statement. When the if statement having the break statement is satisfied, the break statement not only terminates the current iteration but also does not execute the all next iterations and shifts the control to the out-of-loop to execute the outer loop statements. If the break statement is used with the nested loops, it only works in the loop in which it occurs.

    The break statement works on the basis of the given flow-chart:

    Syntax
    The break statement follows the given syntax:

    break

    Example 1: How to Use break Statement to Display Selective Elements?

    The given example uses the while-loop and break statement to display the values from 1 to 9 on the screen. To do this, we define a while loop having 16 iterations from 0 to 15 and use the break statement by implementing the condition i==10. As i equals 10, the if condition becomes true and executes the break statement that terminates the whole loop. As a result, we obtain values from 1 to 9 on the screen.

    i=0;
    while i<=15
    i=i+1;
    if i==10
    break;
    end
    disp(i)
    end

    Example 2: How to Use break Statement to Sum up the Selective Values?

    In this MATLAB code, we use the for-loop and the break statement to find the sum of the values by mentioning the condition s<110. This code starts the loop from 1 and finds the partial sum of each iteration with the previous calculated sum s. When the value of s exceeds 110, the if condition is satisfied and it executes the break statement that terminates the whole loop and stops the remaining iterations.

    s=0;
    for i=0:20
    s=s+i;
    if s > 110
    break;
    end
    fprintf('Sum is %d\n', s);
    end

    Example 3: How to Use break Statement to Find an Element Having Index Equal to its Value in a Vector of Randomly Distributed Integers?

    This example creates a vector A having randomly distributed integers ranging from 1 to 10. After that, it initializes a for-loop consisting of 10 iterations i and sets an if condition A(i)==i means, the element of A is equal to its index. When the condition is satisfied, the break statement is executed and terminates the loop. As a result, we print the found value and its index on the screen.

    A = randi(10,1,10)
    for i=1:10
    if A(i)==i
    break;
    end
    end
    fprintf("%d occur at index %d\n",A(i),i)

    Example 4: How to Use break Statement in Nested Loops?

    The given below MATLAB code uses nested for loop to implement the break statement in the respective loop. To perform this task, we initialize two loops one for variable i and the other for variable j. After that, we mention a condition to execute the break statement in the j-loop. When the given condition is satisfied, it terminates the j-loop by executing the break statement without affecting the i-loop. As a result, we display a pattern of numbers on the screen

    for i=1:10
    for j=1:10
    if i==j
    break;
    end
    fprintf("%d ",j)
    end
    fprintf("%d\n",i)
    end

    How do the continue and break Statements Differ from Eachother?

    The comparison between the continue and the break statements is described in the following table.

    continue statement break statement
    This is a loop control statement used for terminating the iteration in which it is executed. This is a loop control statement used for terminating the whole loop when it is executed.
    This statement allows the loop to complete all iterations even if it has been executed at a specific iteration. This statement does not allow the loop to complete all its iterations when it has been executed.

    Example: How to Use continue and break Statements in a Single MATLAB Code to Observe Their Difference?

    The given example describes the working of both continue and break statement to clarify their difference. In the below-given example, we initialize the nested loop for i and j. The i-loop contains 10 iterations while the j-loop contains 5 iterations. After that, we use the continue statement with a condition i>j. in the j-loop and the break statement with a condition i==j. Here, observe that the continue statement completes the loop iterations and executes the block of code for each iteration except the iterations for which the condition i>j is satisfied. On the other hand, the break statement does not complete the 10 iterations of i and terminates the loop for i=5 because it satisfies the condition i==j.

    for i=1:10
    for j=1:5
    if i>j
    continue;
    end
    fprintf("* ")
    end
    if i==j
    break;
    end
    fprintf("\n")
    end

    Conclusion

    When we need to execute a block of code multiple times, usually we use for and while loops. These loops sequentially execute all iterations and statements. But if our code does not want to execute some unwanted statements, we use the loop control statements i.e the continue statement and the break statement. The continue statement completes all specified loop iterations by avoiding to execute the unwanted statements. However, the break statement does not execute the iterations preceding the iteration at which it is executed. This guide has discussed the loop control statements and their comparison with the help of some examples.

    About the author

    Komal Batool Batool

    I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.