PHP continue statement

The PHP continue statement is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition.

The continue statement is used within looping and switch control structure when you immediately jump to the next iteration.

The continue statement can be used with all types of loops such as – for, while, do-while, and foreach loop. The continue statement allows the user to skip the execution of the code for the specified condition.

Syntax

The syntax for the continue statement is given below:

  1. jump-statement;  
  2. continue;  

Flowchart:

PHP continue statement

PHP Continue Example with for loop

Example

In the following example, we will print only those values of i and j that are same and skip others.

<?php  
    //outer loop  
    for ($i =1; $i<=3; $i++) {  
        //inner loop  
        for ($j=1; $j<=3; $j++) {  
            if (!($i == $j) ) {  
                continue;       //skip when i and j does not have same values  
            }  
            echo $i.$j;  
            echo "</br>";  
        }  
    }  
?>  

Output:

11
22 
33

PHP continue Example in while loop

Example

In the following example, we will print the even numbers between 1 to 20.

<?php  
    //php program to demonstrate the use of continue statement  
  
    echo "Even numbers between 1 to 20: </br>";  
    $i = 1;  
    while ($i<=20) {  
        if ($i %2 == 1) {  
            $i++;  
            continue;   //here it will skip rest of statements  
        }  
        echo $i;  
        echo "</br>";  
        $i++;  
    }     
?>  

Output:

Even numbers between 1 to 20: 
2
4
6
8
10
12
14
16
18
20

PHP continue Example with array of string

Example

The following example prints the value of array elements except those for which the specified condition is true and continue statement is used.

<?php  
    $number = array ("One", "Two", "Three", "Stop", "Four");  
    foreach ($number as $element) {  
        if ($element == "Stop") {  
            continue;  
        }  
        echo "$element </br>";  
    }  
?>  

Output:

One 
Two 
Three
Four

PHP continue Example with optional argument

The continue statement accepts an optional numeric value, which is used accordingly. The numeric value describes how many nested structures it will exit.

Example

Look at the below example to understand it better:

<?php  
    //outer loop  
    for ($i =1; $i<=3; $i++) {  
        //inner loop  
        for ($j=1; $j<=3; $j++) {  
            if (($i == $j) ) {      //skip when i and j have same values  
                continue 1;     //exit only from inner for loop   
            }  
            echo $i.$j;  
            echo "</br>";  
        }  
    }     
?>  

Output:

12
13
21 
23
31
32 

PHP Break

PHP break statement breaks the execution of the current for, while, do-while, switch, and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only.

The break keyword immediately ends the execution of the loop or switch structure. It breaks the current flow of the program at the specified condition and program control resumes at the next statements outside the loop.

The break statement can be used in all types of loops such as while, do-while, for, foreach loop, and also with switch case.

Syntax

  1. jump statement;  
  2. break;  

PHP Break: inside loop

Let’s see a simple example to break the execution of for loop if value of i is equal to 5.

<?php    
for($i=1;$i<=10;$i++){    
echo "$i <br/>";    
if($i==5){    
break;    
}    
}    
?>  

Output:

1
2
3
4
5

PHP Break: inside inner loop

The PHP break statement breaks the execution of inner loop only.

<?php    
for($i=1;$i<=3;$i++){    
 for($j=1;$j<=3;$j++){    
  echo "$i   $j<br/>";    
  if($i==2 && $j==2){    
   break;    
  }    
 }    
}    
?>  

Output:

1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3

PHP Break: inside switch statement

The PHP break statement breaks the flow of switch case also.

<?php        
$num=200;        
switch($num){        
case 100:        
echo("number is equals to 100");        
break;        
case 200:        
echo("number is equal to 200");        
break;        
case 50:        
echo("number is equal to 300");        
break;        
default:        
echo("number is not equal to 100, 200 or 500");        
}       
?>  

Output:

number is equal to 200

PHP Break: with array of string

<?php  
//declare an array of string  
$number = array ("One", "Two", "Three", "Stop", "Four");  
foreach ($number as $element) {  
if ($element == "Stop") {  
break;  
}  
echo "$element </br>";  
}  
?>  

Output:

One 
Two 
Three

You can see in the above output, after getting the specified condition true, break statement immediately ends the loop and control is came out from the loop.

PHP Break: switch statement without break

It is not essential to break out of all cases of a switch statement. But if you want that only one case to be executed, you have to use break statement.

<?php  
$car = 'Mercedes Benz';  
switch ($car) {    
default:  
echo '$car is not Mercedes Benz<br>';  
case 'Orange':  
echo '$car is Mercedes Benz';  
}  
?>  

Output:

$car is not Mercedes Benz
$car is Mercedes Benz

PHP Break: using optional argument

The break accepts an optional numeric argument, which describes how many nested structures it will exit. The default value is 1, which immediately exits from the enclosing structure.

<?php  
$i = 0;  
while (++$i) {  
    switch ($i) {  
        case 5:  
            echo "At matched condition i = 5<br />\n";  
            break 1;  // Exit only from the switch.   
       case 10:  
            echo "At matched condition i = 10; quitting<br />\n";  
            break 2;  // Exit from the switch and the while.   
       default:  
            break;  
    }  
}?>  

Output:

At matched condition i = 5
At matched condition i = 10; quitting

JavaScript Arrow Function

JavaScript Arrow function or arrow function expression is a shorter alternative to the regular JavaScript function definition syntax. It was introduced in ECMAScript2016 and it makes defining a function just like writing a simple expression.

An arrow function can have optional parameters and a return statement along with the function defintion enclosed within curly braces { }. Arrow function expressions provide a compact syntax but this can make the code less readable. Also as they are less a function and more of an expression, they are not very well suited for using for defining methods. And they cannot be used as constructors.

JavaScript Arrow Function: Syntax

The arrow function expression supports multiple different syntaxes and we will see a few of them.

(param1, param2, …, paramN) => { statements } 

/* or for a single parameter */

(param1) => { statements } 

/* in case of no parameter */

() => { statements }

In case we have a single parameter, using the parethesis () is optional, we can also define the arrow function expression like this:

param1 => { statements }

And if we have a single expression to return some output, we can even skip the curly braces {} like this:

param1 => { return expression; }
/* can be written as*/
param1 => expression

The above syntax also works with multiple parameters.

Line Breaks:

An arrow function cannot have a line break between its parameters and its arrow but we can have a line break after the arrow. For example:

var someFunc = (a, b, c)
  => 1;
// SyntaxError: expected expression, got '=>'

var someFunc = (a, b, c) => 
  1;
// No Sytax Error

var someFunc = (a, b, c) => (
  1
);
// No Sytax Error

Lets have a look at some code examples.

JavaScript Arrow Function Example:

To see the difference between the normal JavaScript function and the arrow function expression. Let’s start by creating a regular function and then we will define it’s arrow function as well.

// Example of a regular function
let add = function(a,b) {
    return a+b;
}

let sum = add(10,20);
console.log(sum);

30

Now let’s change the above function into arrow function expression:

// Arrow function
let add = (a,b) => { return a+b; }

let sum = add(10,20);

console.log(sum);

30

As we discussed in the syntax above, we can further break down the above arrow function expression, removing the curly braces too, as this one has a single statement code.

// Arrow function
let add = (a,b) => a+b;

let sum = add(10,20);

console.log(sum);

Now, let’s take a few more examples covering different scenarios.

JavaScript Arrow Function without Parameter:

Just like a regular function, arrow function can be non-parameterized too. Let’s take an example to see how we can implement it.

// Arrow function - without parameter
show = () => { return "Hello from arrow function"; }

// Calling function
let msg = show();

console.log(msg);

Hello from arrow function

Again, in this example too, we can even remove the curly braces {} and the return keyword, like we did in the example above this one.

JavaScript Arrow Function with Default Parameters:

In JavaScript arrow functions we can also specify the default values for the parameters while defining the arrow function expression. Let’s take an example to understand this concept.

// Arrow function - with default parameters

let sum = (a,b=1) => a+b;

// Calling function
let result = sum(10);
console.log(result);

11

Remember, you can not provide default value for the first parameter only, and not provide default value for the second one. Either provide default value for both or provide default value for just the second one.

JavaScript Arrow Function without Parenthesis:

In case, you have single parameter in your function then you can omit the parenthesis around the parameter. Let’s take an example:

// Arrow function without parenthesis
let show = a => "You entered: " + a

console.log(show(10));

You entered: 10

JavaScript Arrow Function: Live Example

Well, now lets have a live example to understand arrow functions. Do feel free to modify the code and change the arrow functions defined below to practice.

<!doctype html>
	<head>
		<style>
			/* CSS comes here */
			
		</style>
		<title>Arrow Function</title>
	</head>
	<body>
            <h2>Arrow Function Example</h2>
		<script>
		// Arrow function for sum
        let add = (a,b) => {return a+b}
        
        let sum = add(10,20)
        document.write(" Sum of 10 and 20 is: " + sum);
        
        // Arrow function - without return
        let show = () => "Hello from arrow function";
        // Calling function
        document.write("<br>"+show());
        
        // Arrow function with default params
        let newAdd = (a,b=1) => a+b;
        
        let newsum = newAdd(10);
        document.write("<br/>Result is: " + newsum);
		</script>
	</body>
</html>

JavaScript break and continue Statements

JavaScript break and continue statements are known as Loop Control Statements as they are used to control the loops. These statements let you control every loop and switch statements in JavaScript code by enabling you to either break out of the loop completely or directly jump to the next iteration skipping the current ongoing loop iteration.

We can come out of any loop’s execution, whenever required, by using the break statement and we can also skip the current iteration and start next iteration by using the continue statement.

JavaScript break Statement

You may recall that the break statement was introduced in switch case, this statement can also be used inside other loops like for loop and while/do-while loops.

let count=0; while(count<10) { ++count; if((count%5==0)) { break; } else { document.write(“count is “+count+”
“) } } document.write(“while loop terminated”);

Let us recall the break statement once again, the break statement lets us break out of the loop execution or any switch statement and move on to execute the code after the end of the loop or switch.

In the above example, when the value of the variable count becomes divisible by 5, we break out of the loop, and hence the loop execution stops. Try removing the break statement and you will see the loop will get executed 10 times.

JavaScript continue Statement

The continue statement can be used to skip the current loop iteration and jump to the next iteration if the loop condition is satisfied. This is used in case, for some specific condition in the loop execution you want to perform no action and directly jump to next iteration then the continue statement can be used.

var count=0; while(count<10) { ++count; if(count%2==0) { // jump to next iteration continue; } document.write(“count=”+count+”
“); } document.write(“While loop terminated”);

In the code example above, whenever the value of the variable count is divisible by 2, we do nothing and call the continue statement to directly jump to the next iteration of the loop. Hence you can see that the document.write code is executed for only those iterations where the value of the count variable was not divisible by 2, which means for odd values only.

JavaScript Labels

By using the JavaScript labels you can control the flow of your code’s execution more precisely. Where the break and continue statements can only be used to break or skip iteration of a code block in a loop or switch case, using a label with break and continue statement lets you control the execution of any code block.

let count=0; EXECUTE: while(count<10) { if(count==5) { break EXECUTE; } document.write(“Count is “+ count + “
“); ++count; } document.write(“While loop terminated”);

To use JavaScript labels, you must first define a label, which is nothing but a name followed by a colon(:) and then your code.

Then the JavaScript label can be used by writing break or continue followed by the label_name.

As you can see in the example above, we have specified a label with name EXECUTE and then used it along with the break statement to specify which code block to break out of.

In this tutorial, we learned about the break and continue statements which are very important statements when it comes to controlling the flow of execution of the code within a loop or outside a loop.

C++ break and continue

In this tutorial, we will learn about the break statement and its working in loops with the help of examples.

In computer programming, the break statement is used to terminate the loop in which it is used.

The syntax of the break statement is:

break;

Before you learn about the break statement, make sure you know about:


Working of C++ break Statement

Working of C++ break Statement
Working of break statement in C++

Example 1: break with for loop

// program to print the value of i

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        // break condition     
        if (i == 3) {
            break;
        }
        cout << i << endl;
    }

return 0;
}

Run Code

Output

1
2

In the above program, the for loop is used to print the value of i in each iteration. Here, notice the code:

if (i == 3) {
    break;
}

This means, when i is equal to 3, the break statement terminates the loop. Hence, the output doesn’t include values greater than or equal to 3.

Note: The break statement is usually used with decision-making statements.


Example 2: break with while loop

// program to find the sum of positive numbers
// if the user enters a negative numbers, break ends the loop
// the negative number entered is not added to sum

#include <iostream>
using namespace std;

int main() {
    int number;
    int sum = 0;

    while (true) {
        // take input from the user
        cout << "Enter a number: ";
        cin >> number;

        // break condition
        if (number < 0) {
            break;
        }

        // add all positive numbers
        sum += number;
    }

    // display the sum
    cout << "The sum is " << sum << endl;

    return 0;
}

Run Code

Output

Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: -5
The sum is 6. 

In the above program, the user enters a number. The while loop is used to print the total sum of numbers entered by the user. Here, notice the code,

if(number < 0) {
    break;
}

This means, when the user enters a negative number, the break statement terminates the loop and codes outside the loop are executed.

The while loop continues until the user enters a negative number.


break with Nested loop

When break is used with nested loops, break terminates the inner loop. For example,

// using break statement inside
// nested for loop

#include <iostream>
using namespace std;

int main() {
    int number;
    int sum = 0;

    // nested for loops

    // first loop
    for (int i = 1; i <= 3; i++) {
        // second loop
        for (int j = 1; j <= 3; j++) {
            if (i == 2) {
                break;
            }
            cout << "i = " << i << ", j = " << j << endl;
        }
    }

    return 0;
}

Run Code

Output

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

In the above program, the break statement is executed when i == 2. It terminates the inner loop, and the control flow of the program moves to the outer loop.

Hence, the value of i = 2 is never displayed in the output.


The break statement is also used with the switch statement. To learn more, visit C++ switch statement.

In this tutorial, we will learn about the break statement and its working in loops with the help of examples.

In computer programming, the break statement is used to terminate the loop in which it is used.

The syntax of the break statement is:

break;

Before you learn about the break statement, make sure you know about:


Working of C++ break Statement

Working of C++ break Statement
Working of break statement in C++

Example 1: break with for loop

// program to print the value of i

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        // break condition     
        if (i == 3) {
            break;
        }
        cout << i << endl;
    }

return 0;
}

Run Code

Output

1
2

In the above program, the for loop is used to print the value of i in each iteration. Here, notice the code:

if (i == 3) {
    break;
}

This means, when i is equal to 3, the break statement terminates the loop. Hence, the output doesn’t include values greater than or equal to 3.

Note: The break statement is usually used with decision-making statements.


Example 2: break with while loop

// program to find the sum of positive numbers
// if the user enters a negative numbers, break ends the loop
// the negative number entered is not added to sum

#include <iostream>
using namespace std;

int main() {
    int number;
    int sum = 0;

    while (true) {
        // take input from the user
        cout << "Enter a number: ";
        cin >> number;

        // break condition
        if (number < 0) {
            break;
        }

        // add all positive numbers
        sum += number;
    }

    // display the sum
    cout << "The sum is " << sum << endl;

    return 0;
}

Run Code

Output

Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: -5
The sum is 6. 

In the above program, the user enters a number. The while loop is used to print the total sum of numbers entered by the user. Here, notice the code,

if(number < 0) {
    break;
}

This means, when the user enters a negative number, the break statement terminates the loop and codes outside the loop are executed.

The while loop continues until the user enters a negative number.


break with Nested loop

When break is used with nested loops, break terminates the inner loop. For example,

// using break statement inside
// nested for loop

#include <iostream>
using namespace std;

int main() {
    int number;
    int sum = 0;

    // nested for loops

    // first loop
    for (int i = 1; i <= 3; i++) {
        // second loop
        for (int j = 1; j <= 3; j++) {
            if (i == 2) {
                break;
            }
            cout << "i = " << i << ", j = " << j << endl;
        }
    }

    return 0;
}

Run Code

Output

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 3, j = 1
i = 3, j = 2
i = 3, j = 3

In the above program, the break statement is executed when i == 2. It terminates the inner loop, and the control flow of the program moves to the outer loop.

Hence, the value of i = 2 is never displayed in the output.


The break statement is also used with the switch statement. To learn more, visit C++ switch statement.

C break and continue

We learned about loops in previous tutorials. In this tutorial, we will learn to use break and continue statements with the help of examples.

C break

The break statement ends the loop immediately when it is encountered. Its syntax is:

break;

The break statement is almost always used with if...else statement inside the loop.


How break statement works?

Working of break statement

Example 1: break statement

// Program to calculate the sum of a maximum of 10 numbers
// If a negative number is entered, the loop terminates

# include <stdio.h>
int main()
{
    int i;
    double number, sum = 0.0;

    for(i=1; i <= 10; ++i)
    {
        printf("Enter a n%d: ",i);
        scanf("%lf",&number);

        // If the user enters a negative number, the loop ends
        if(number < 0.0)
        {
            break;
        }

        sum += number; // sum = sum + number;
    }

    printf("Sum = %.2lf",sum);
    
    return 0;
}

Output

Enter a n1: 2.4
Enter a n2: 4.5
Enter a n3: 3.4
Enter a n4: -3
Sum = 10.30

This program calculates the sum of a maximum of 10 numbers. Why a maximum of 10 numbers? It’s because if the user enters a negative number, the break statement is executed. This will end the for loop, and the sum is displayed

In C, break is also used with the switch statement. This will be discussed in the next tutorial.


C continue

The continue statement skips the current iteration of the loop and continues with the next iteration. Its syntax is:

continue;

The continue statement is almost always used with the if...else statement.


How continue statement works?

Working of continue statement in C programming

Example 2: continue statement

// Program to calculate the sum of a maximum of 10 numbers
// Negative numbers are skipped from the calculation

# include <stdio.h>
int main()
{
    int i;
    double number, sum = 0.0;

    for(i=1; i <= 10; ++i)
    {
        printf("Enter a n%d: ",i);
        scanf("%lf",&number);

        if(number < 0.0)
        {
            continue;
        }

        sum += number; // sum = sum + number;
    }

    printf("Sum = %.2lf",sum);
    
    return 0;
}

Output

Enter a n1: 1.1
Enter a n2: 2.2
Enter a n3: 5.5
Enter a n4: 4.4
Enter a n5: -3.4
Enter a n6: -45.5
Enter a n7: 34.5
Enter a n8: -4.2
Enter a n9: -1000
Enter a n10: 12
Sum = 59.70

In this program, when the user enters a positive number, the sum is calculated using sum += number; statement.

When the user enters a negative number, the continue statement is executed and it skips the negative number from the calculation.

C# Loop control statements

Break

The C# break is used to break loop or switch statement. It breaks the current flow of the program at the given condition. In case of inner loop, it breaks only inner loop.

C# Break Statement Example

using System;
public class BreakExample
{
public static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}
}
}
Output:
1
2
3
4

Continue

The C# continue statement is used to continue loop. It continues the current flow of the program and skips the remaining code at specified condition. In case of inner loop, it continues only inner loop.

C# Continue Statement Example

using System;
public class ContinueExample
{
public static void Main(string[] args)
{
for(int i=1;i<=10;i++){
if(i==5){
continue;
}
Console.WriteLine(i);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10

Goto

The C# goto statement is also known jump statement. It is used to transfer control to the other part of the program. It unconditionally jumps to the specified label.

It can be used to transfer control from deeply nested loop or switch case label.

Currently, it is avoided to use goto statement in C# because it makes the program complex.

C# Goto Statement Example

using System;
public class GotoExample
{
public static void Main(string[] args)
{
ineligible:
Console.WriteLine(“You are not eligible to vote!”);

Console.WriteLine(“Enter your age:\n”);
int age = Convert.ToInt32(Console.ReadLine());
if (age < 18)
{
goto ineligible;
}
else
{
Console.WriteLine(“You are eligible to vote!”);
}
}
}
output:
You are not eligible to vote!
Enter your age:
11
You are not eligible to vote!
Enter your age:
5
You are not eligible to vote!
Enter your age:
26
You are eligible to vote!