Control Statements in C

Control Statements in C Language

In this article, I will discuss the Control Statements in C Language. Please read our previous articles, where we discussed the C Fundamentals. As part of this article, you will learn what C Control Statements are, their type, and when and how to use Control Statements in C Programs with examples.

Control Statements in C:

Control Statements are the statements that alter the flow of execution and provide better control to the programmer on the flow of execution. They are useful to write better and more complex programs. A program executes from top to bottom, except when we use control statements, we can control the order of execution of the program based on logic and values.

In C, control statements can be divided into the following three categories:

  • Selection Statements or Branching statements (ex: if-else, switch case, nested if-else, if-else ladder)
  • Iteration Statements or looping statements (ex: while loop, do-while loop, for loop)
  • Jump Statements (ex: break, continue, return, goto)

Control Statements in C Language

Types of Control Statements in C

Control statements in C programming are used to control the flow of execution of the program. They determine the direction the program takes based on certain conditions.

Selection Statements:

These include if, if…else, and switch. They are used to execute certain parts of code based on the evaluation of a condition.

If Statement: It is used to execute a block of code if a specified condition is true.
Syntax:

if (condition) {
    // code to be executed if condition is true
}

If-Else Statement: It extends the if statement to execute a different code block if the condition is false.
Syntax:

if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

Nested If-Else: This involves using if-else statements within another if-else block.

Syntax:

if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition2 is true
} else {
    // code to be executed if both conditions are false
}

Switch Statement: This is used to execute one code block among many alternatives based on the value of a variable.
Syntax:

switch(variable) {
    case value1:
        // code to be executed if variable == value1
        break;
    case value2:
        // code to be executed if variable == value2
        break;
    // ... other cases ...
    default:
        // code to be executed if variable doesn't match any case
}
Iteration Statements (Loops):

These include for, while, and do…while. They are used to repeat a block of code multiple times. Loops are used for executing a block of code repeatedly as long as a certain condition is met. The main loop constructs in C are for, while, and do-while.

for loop Syntax:

for (initialization; condition; increment/decrement) {
    // code to be executed
}

while loop Syntax:

while (condition) {
    // code to be executed
}

do-while loop Syntax:

do {
    // code to be executed
} while (condition);
Jump Statements:

These include break, continue, goto, and return. They are used to alter the normal sequence of program execution. For example:

Break Statement: Used within loops to terminate the loop and transfer control to the statement immediately following the loop.
Syntax: break;

Continue Statement: Skips the current iteration of the loop and continues with the next iteration.
Syntax: continue;

goto Statement: It is used to transfer control to another part of the program. It jumps to a specified label. 
Syntax:

goto label;
...
label: 
// code to execute after the goto statement

return Statement: Used to exit a function and return control to the calling function. If the function is supposed to return a value (non-void), return is used to specify the value to be returned. The return statement is crucial in functions for exiting the function and, when necessary, returning a value to the caller.
Syntax:
Void Function: return;
Non-Void Function: return expression;

These control statements allow complex decision-making and repetitive tasks to be handled efficiently in C programming.

In the next article, I will discuss If else Selection Statements in C with examples. In this article, I try to explain Control Statements in C and their type. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, questions, or comments about this Control Statements in the C Language article.

Leave a Reply

Your email address will not be published. Required fields are marked *