Unconditional Statements in C
The C programming language is a procedural programming language that successfully executes programs in a top-to-bottom order. Still, sometimes we need to bypass numerous lines of code to meet our requirements.
For this, C provides branching statements. Then, we'll go over the various C branching statement kinds and give examples for each one.
Statements for Branching in C
- Branching statements in C are those statements that enable a programmer to direct the course of a program's execution by specifications.
- These branching statements, a fundamental element of programming languages, are primarily employed to execute particular parts of code segments in response to certain situations.
Types of Branching Statements in C:
- Conditional Branching Statements in C
- Unconditional Branching Statements in C
Here we will be discussing about the Unconditional Branching Statements in C:
Unconditional Branching Statements in C:
- C uses unconditional branching statements to alter the typical course of program execution.
- These statements enable programmers to jump to a specific location in their code.
- Goto Statement.
- Break Statement.
- Continue Statement.
- Programmers can jump to a particular statement of code using a "goto" statement, an unconditional branching statement.
- The "goto" statement, which unconditionally branches from one point in the program to the next, is supported by C.
- An essentially structured language such as C doesn’t necessarily bear the 'goto' statement but may still bear the use of 'goto'.
- A goto needs a label or marker to identify where the branch occurs.
- A label is a variable name that is succeeded by using a colon symbol.
Syntax:
Go to label.
...
Label:statement;
// code to run
In this syntax, labels are identifiers. When the program reaches a goto statement, program control jumps to the label and run the underlying code.
Go to label.
…………
…………
…………
Label:
opinion;
FORWARD JUMP
Label:
opinion;
…………
………… …………
Go to label.
REAR JUMP
- When a statement like "goto begin;" is satisfied during program execution, the control inflow jumps to the statement incontinently after the label "begin:" This occurs without exception.
- The "goto" command breaks the program's usual successive execution.
- A loop is created, and some sentences are repeated if the "label:" comes before the command "goto label." A "backward jump" is the name given to such a leap.
- The jump is referred to as a "forward jump" if the "label:" is added after the "goto label;" some words will be skipped.
Example Program:
#include <stdio.h>
int main()
{
int x = 0;
launch:
x++;
if (x < 20)
{
goto launch;
}
printf("x is %d", x);
return 0;
}
Output:
![Unconditional Statements in C](https://static.tutorialandexample.com/c/unconditional-statements-in-c1.png)
Break Statement:
- One of C's unconditional branching statements, the break statement, is constantly used in loops to quit the loop before it’s finished.
- A break statement stops the loop from running and hands off control to the statement that comes after the loop.
- In other words, the break statement is utilized to exit switches or halt loops. It can be used inside a switch, for, while, or do-while statement.
- It can be employed within a for, while, do-while, or switch statement.
- The break statement is expressed as "break" and ends the control of the inner loop only when used with inner loops.
- Break Statement in C Syntax
- Any time the sentence mentioned above is used inside a loop, it ends regardless of its conclusion.
Syntax:
Jump-statement;
break;
Example Program:
#include <stdio.h>
int main()
{
int x = 0;
while (x < 15)
{
x++;
if (x == 7)
{
break;
}
}
printf("x is %d", x);
return 0;
}
Output:
![Unconditional Statements in C](https://static.tutorialandexample.com/c/unconditional-statements-in-c2.png)
Continue Statement:
When a continuing statement is met, the loop does not end. Instead, the remaining loop statements are skipped, and the computation moves right on to the coming iteration of the loop.
- While do-while and for statements can all contain the continue statement. It says to "continue" in writing.
- It’s written as "continue".
- The compiler is instructed to "Skip the Following Statements and Continue with the Next Iteration" by the continue statement.
- While in "while" and "do," loops keep running, causing the control to jump right to the test condition and carry on with the iteration process.
When using a "for" loop, the update portion of the loop is carried out before evaluating the test condition.
Syntax:
Jump statement;
continue;
Illustration Program:
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
If (i % 2 == 0)
{
continue; // skip indeed numbers
}
printf("%d ", i);
}
return 0;
}
Output:
![Unconditional Statements in C](https://static.tutorialandexample.com/c/unconditional-statements-in-c3.png)