In this tutorial, we are going to learn about unconditional constructs in C.
C break statement:
In C language there is a special statement called break;, which is used to unconditionally transfer the execution control out of its immediately enclosing loop or switch-case block.
In C language the term break is a keyword. However when we place a semicolon after it i.e, break;, it becomes an independent statement.
When a break; statement appears in a loop or in a switch-case control structure, it terminates the execution at that point and transfers execution control to the statement that immediately follows the loop or the switch-case statement.
A break; statement is used to transfer control out of the enclosing switch, for, while and do-while statements.
#include<stdio.h>
void main() {
int i;
for (i = 1; i < 50; i++) {
if (i % 2 == 0) {
break; //breaks from for-loop, if i is divisible by 2
}
printf("i : %d\n", i);
}
}
Output:
i : 1
Here it can be observed that once the condition i % 2 == 0 evaluates to true the break; the statement is executed and it transfers the control out of the loop and further numbers are not printed.
Whenever a break; statement appears in the inner loop of a nested loop structure, the control comes out only from the inner loop, but not from the outer loops.
For example in the below code the control is terminated only from the inner loop:
#include<stdio.h>
void main () {
int i, j;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == j) {
break; // breaks from inner for-loop, if i is equal to j.
}
printf("i : %d, j : %d\n", i, j);
}
}
}
Output:
i : 2, j : 1
i : 3, j : 1
i : 3, j : 2
C continue statement:
C language provides a statement called continue; which can be applied in loops.
When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for the next iteration, skipping the execution of statements inside the body of the loop for the current iteration. Instead of terminating the loop, it forces the next iteration of the loop to take place.
While a break; transfers control out (meaning terminates) the loop, a continue; statement transfers control to the innermost enclosing for, while, and do statements and continues with the next iteration.
Similar to a break statement, a continue statement can be written simply as continue;.
#include<stdio.h>
void main() {
int i;
for (i = 1; i < 10; i++) {
if (i % 5 == 0) {
continue; // transfers control to for-loop, if i is divisible by 5.
}
printf("i : %d\n", i);
}
}
Output:
i : 1
i : 2
i : 3
i : 4
i : 6
i : 7
i : 8
i : 9
The above code skips printing all odd numbers.
Whenever the control encounters continue in a for loop then control will go to the update section which usually contains the code to increment/decrement.
Here it can be observed that once the condition i % 2 == 1 evaluates to true the continue; statement is executed and it skips printing the current number and transfers control to the for-loop.
Whenever a continue statement appears in the inner loop of a nested loop structure, the control is transferred to the next iteration of the inner loop.
For example, the below code skips printing the values of i and j using the continue; statement whenever they are the same:
#include<stdio.h>
void main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
if (i == j) {
continue; //continues next iteration of the inner for-loop if i is equal to j
}
printf("i : %d, j : %d\n", i, j);
}
}
}
Output:
i : 1, j : 2
i : 1, j : 3
i : 1, j : 4
i : 1, j : 5
i : 2, j : 1
i : 2, j : 3
i : 2, j : 4
i : 2, j : 5
i : 3, j : 1
i : 3, j : 2
i : 3, j : 4
i : 3, j : 5
i : 4, j : 1
i : 4, j : 2
i : 4, j : 3
i : 4, j : 5
i : 5, j : 1
i : 5, j : 2
i : 5, j : 3
i : 5, j : 4
C goto statement:
The goto construct causes an unconditional transfer of execution control from the current location to any other labeled location.
The syntax of the forwarding goto statement is
goto Label;
....
....
....
Label:
....
....
....
The syntax of the backward goto statement is:
Label:
....
....
....
goto Label;
....
....
....
In the above syntax Label is an identifier (meaning a name) and not a number. The same name used for a label should not be used for declaring an identifier of any other type between the current label and its goto statement.
The compiler identifies this name as a label if it is followed by a colon (:).
#include <stdio.h>
void main() {
char ch;
start:
printf("Enter a character : " );
scanf(" %c", &ch);
printf("The given character is : %c\n", ch);
if (ch != '#') {
goto start;
}
}
Output:
Enter a character : a
The given character is : a
Enter a character : c
The given character is : c
Enter a character : #
The given character is : #
The above code reads a character and prints the character until the given character is #.
Note: It is generally suggested to avoid using the goto construct so that the program is more structured and readable.
REFERENCES:
Happy Learning 🙂