Branching statements, also known as jump statements, unconditionally transfer the flow of a program's control from one point to another.
C Programming: Branching (Jump) Statements ⤵️
While conditional
statements create potential paths, branching statements (or jump
statements) force an immediate transfer of control. They are used to exit
loops, skip iterations, and leave functions. The four branching statements in C
are break
, continue
, return
, and goto
.
1. The break
Statement
The break
statement immediately terminates the execution
of the innermost enclosing loop (for
, while
, do-while
) or switch
statement. Control is transferred to the statement
immediately following the terminated block.
·
Purpose: To exit a loop early or to prevent
"fall-through" in a switch
case.
Example
This loop searches
for the number 5 in an array. Once it's found, there's no need to keep
searching, so break
is used to exit the loop.
C
#include <stdio.h>
int main() {
int numbers[
10] = {
1,
2,
3,
4,
5,
6,
7,
8,
9,
10};
int i;
for (i =
0; i <
10; i++) {
printf(
"Checking index %d\n", i);
if (numbers[i] ==
5) {
printf(
"Found 5! Exiting loop.\n");
break;
// Terminate the for loop
}
}
// Execution resumes here after the break
printf(
"Loop finished. The value of i is: %d\n", i);
return
0;
}
size=2 width="100%" align=center>
2. The continue
Statement
The continue
statement skips the remaining code inside the
current iteration of a loop and proceeds directly to the next iteration.
It doesn't terminate the loop itself.
· Purpose: To bypass a part of the loop's body for certain iterations.
Example
This loop prints
all odd numbers from 1 to 10 by using continue
to skip the printf
statement for all even numbers.
C
#include <stdio.h>
int main() {
int i;
for (i =
1; i <=
10; i++) {
// If i is even, skip to the next iteration
if (i %
2 ==
0) {
continue;
// Skip the printf statement below
}
printf(
"%d ", i);
}
printf(
"\n");
return
0;
}
// Output: 1 3 5 7 9
size=2 width="100%" align=center>
3. The return
Statement
The return
statement terminates the execution of the current
function and transfers control back to the calling function. It can also
optionally pass a value back.
· Purpose: To exit a function and/or provide a result.
Example
The find_sum
function calculates the sum of two integers and uses return
to send the result back to main
.
C
#include <stdio.h>
// This function returns an integer value
int find_sum(int a, int b) {
int sum = a + b;
return sum;
// Return the value of 'sum' and exit the function
}
int main() {
int result;
result = find_sum(
10,
20);
// Call the function and store the returned value
printf(
"The result is: %d\n", result);
return
0;
// Return 0 from main to indicate success
}
size=2 width="100%" align=center>
4. The goto
Statement
The goto
statement provides an unconditional jump to a labeled
statement somewhere within the same function. Its use is heavily
discouraged in modern programming because it breaks the structured flow of code
and can make programs extremely difficult to read and debug (often called
"spaghetti code").
· Purpose: To jump to a specific location within a function.
Example
This example uses goto
to jump out of a loop to a cleanup section if a condition
is met.
C
#include <stdio.h>
int main() {
int i =
0;
start_loop:
// This is a label
if (i >=
5) {
goto end_loop;
// Jump to the 'end_loop' label
}
printf(
"%d ", i);
i++;
goto start_loop;
// Jump back to the start of the "loop"
end_loop:
// Another label
printf(
"\nLoop finished.\n");
return
0;
}
Note: The loop in this example can and should be written
more clearly using a standard for
or while
loop. The example only serves to demonstrate the
mechanics of goto
.