So far you have learned how to execute a block of code repeatedly based on a particular condition using for loop, while loop, and do while loop statements. These loop statements execute a block until a certain condition is met.
Sometimes, however, you may want to have an additional control over the loop execution. The
break
and continue
statements give you this kind control.C break statement
C
break
statement terminates any type of loop e.g., while loop, do while loop or for loop. The break
statement terminates the loop body immediately and passes control to the next statement after the loop.
The
break
statement is only meaningful when you put it inside a loop body, and also in the switch case statement.
We often use the
break
statement with the if statement, which specifies the condition to terminate the loop.
The following example illustrates how to use the
break
statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <stdio.h>
int main() {
char key;
printf("Press any key or E to exit:\n");
while(1) {
scanf("%c", &key);
// if E or e, exit
if (key == 'E' || key == 'e')
break;
}
printf("Goodbye!\n");
}
|
The program asked users to enter any character. If the user enters
E
or e
, the break
statement terminates the loop and control is passed to the statement after the loop that displays the Goodbye
message.
Besides using the
break
statement to terminate a loop, we also use the break
statement to terminate the processing of a case branch in the switch case statement.C continue statement
C
continue
statement skips the rest of the current iteration in a loop and returns to the top of the loop. The continue
statement works like a shortcut to the end of the loop body.
The following example illustrates the
continue
statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int main()
{
int haystack[SIZE] = {1, 3, 2, 4, 7, 6, 9, 5, 8, 0};
int needle;
printf("Enter a number (0-9) to see its position:");
scanf("%d",&needle);
int i;
for (i = 0; i < SIZE; i++)
{
if (needle != haystack[i])
{
printf("Finding at position %d: %d\n", i, haystack[i]);
continue;
}
printf("Number %d found at position %d\n", needle,i);
break;
}
return 0;
}
|
How it works.
- First we defined an array of 10 integers from 0 to 9.
- Second, we asked users to enter a number to find its position in the array.
- Third, we looped over the array elements to find the input number. If the current element of the array is not equal to the input number, we displayed a message and skip the logic below it using the
continue
statement. If we found the element, we displayed it and exit the loop using thebreak
statement.
The following is the output of the program when we entered number
4
:
1
2
3
4
5
|
Enter a number (0-9) to see its position:4
Finding at position 0: 1
Finding at position 1: 3
Finding at position 2: 2
Number 4 found at position 3
|
In this tutorial, we have shown you how to use the
break
and continue
statement to control the loop iteration.
No comments:
Post a Comment