Table of contents
1.
Introduction
2.
What Are Control Statements in C?
3.
Types of Control Statements in C
4.
Conditional statements
4.1.
a. If statement
4.2.
b. If-else statement
4.3.
c. Switch statement
5.
Loop statements
5.1.
a. For loop
5.2.
b. While loop
5.3.
c. Do-while loop
6.
Jump statements
7.
Decision-Making Control Statements Are
7.1.
If statements
7.2.
Switch statements
8.
Conditional Control Statements in C
8.1.
If statements
8.2.
Switch statements
8.3.
Goto Statements in C
8.4.
C
9.
Frequently Asked Questions
9.1.
What is the primary use of decision-making statements in C?
9.2.
When should the ternary operator be used instead of traditional if-else statements?
9.3.
Why is the use of goto generally discouraged in modern programming?
10.
Conclusion
Last Updated: Jun 11, 2024
Easy

Control Structures in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Control structures are an important part of programming in C. They allow you to control the flow of your program & make decisions based on certain conditions. Without control structures, your program would just be a series of statements that execute one after the other. Control structures give you the power to create more complex & useful programs. 

Control Structures in C

In this article, we will discuss the different types of control structures available in C & how to use them effectively. 

What Are Control Statements in C?

Control statements in C are used to control the flow of execution in a program. They allow you to make decisions, repeat code, & jump to different parts of your program based on certain conditions. There are three main types of control statements in C:

  1. Conditional statements: These statements allow you to execute different code based on whether a certain condition is true or false. Examples include if statements, if-else statements, & switch statements.
     
  2. Loop statements: These statements allow you to repeat a block of code multiple times until a certain condition is met. Examples include for loops, while loops, & do-while loops.
     
  3. Jump statements: These statements allow you to jump to different parts of your program or exit the program altogether. Examples include break statements, continue statements, & goto statements.
     

Control statements are an essential part of programming in C because they allow you to create more complex & useful programs. Without them, your program would just be a series of statements that execute one after the other without any decision-making or repetition.

Types of Control Statements in C

There are several types of control statements in C that allow you to control the flow of your program in different ways. Let's discuss each of the type:

Conditional statements

a. If statement

The if statement allows you to execute a block of code if a certain condition is true. The syntax is:

if (condition) {
   // code to execute if condition is true
}

b. If-else statement

The if-else statement allows you to execute one block of code if a condition is true & another block of code if the condition is false. The syntax is:

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

c. Switch statement

 The switch statement allows you to select one of many code blocks to execute based on the value of a variable. The syntax is:

switch (variable) {
   case value1:
      // code to execute if variable equals value1
      break;
   case value2:
      // code to execute if variable equals value2
      break;
   default:
      // code to execute if variable doesn't match any case
}

Loop statements

a. For loop

The for loop allows you to repeat a block of code a specific number of times. The syntax is:

for (initialization; condition; increment/decrement) {
   // code to execute repeatedly
}

b. While loop

The while loop allows you to repeat a block of code as long as a certain condition is true. The syntax is:

while (condition) {
   // code to execute repeatedly
}

c. Do-while loop

The do-while loop is similar to the while loop, but it executes the code block at least once before checking the condition. The syntax is:

do {
   // code to execute repeatedly
} while (condition);

Jump statements

a. Break statement: The break statement allows you to exit a loop or switch statement prematurely.
 

b. Continue statement: The continue statement allows you to skip the rest of the current loop iteration & move on to the next one.
 

c. Goto statement: The goto statement allows you to jump to a labeled statement elsewhere in your program. However, the use of goto is generally discouraged as it can make code harder to read & maintain.

Decision-Making Control Statements Are

Decision-making control statements in C allow you to execute different blocks of code based on whether certain conditions are true or false. The two main types of decision-making control statements are:

If statements

The if statement is the most basic decision-making statement in C. It allows you to execute a block of code if a certain condition is true. Here's an example:

int x = 10;
if (x > 5) {
   printf("x is greater than 5");
}


In this example, the code inside the if block will only execute if the condition x > 5 is true. Since x is equal to 10, the condition is true & the message "x is greater than 5" will be printed.

You can also use an if-else statement to execute one block of code if a condition is true & another block of code if the condition is false. Here's an example:

int y = 3;
if (y > 5) {
   printf("y is greater than 5");
} else {
   printf("y is less than or equal to 5");
}


In this case, since y is equal to 3, the condition y > 5 is false & the code inside the else block will execute, printing "y is less than or equal to 5".

Switch statements

The switch statement allows you to select one of many code blocks to execute based on the value of a variable. Here's an example:

int day = 3;
switch (day) {
   case 1:
      printf("Monday");
      break;
   case 2:
      printf("Tuesday");
      break;
   case 3:
      printf("Wednesday");
      break;
   default:
      printf("Invalid day");
}


In this example, the switch statement checks the value of the day variable. If day is equal to 1, it will print "Monday". If day is equal to 2, it will print "Tuesday". If day is equal to 3, it will print "Wednesday". If day doesn't match any of the cases, it will execute the code in the default block & print "Invalid day".

Conditional Control Statements in C

Conditional control statements in C allow you to execute different blocks of code based on whether certain conditions are true or false. The two main types of conditional control statements are if statements & switch statements.

If statements

If statements are used to execute a block of code if a certain condition is true. The basic syntax of an if statement is:

if (condition) {
   // code to execute if condition is true
}


Here's an example that checks if a number is positive:

int num = 10;
if (num > 0) {
   printf("%d is a positive number", num);
}


In this example, the condition num > 0 is true, so the code inside the if block will execute & the message "10 is a positive number" will be printed.

You can also use an if-else statement to execute one block of code if a condition is true & another block of code if the condition is false. The syntax is:

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


Here's an example that checks if a number is even or odd:

int num = 7;
if (num % 2 == 0) {
   printf("%d is even", num);
} else {
   printf("%d is odd", num);
}


In this case, since num is equal to 7, the condition num % 2 == 0 is false, so the code inside the else block will execute & the message "7 is odd" will be printed.

You can also combine multiple if-else statements together to check for multiple conditions. The syntax is:

if (condition1) {
   // code to execute if condition1 is true
} else if (condition2) {
   // code to execute if condition2 is true
} else {
   // code to execute if all conditions are false
}


Here's an example that checks if a number is positive, negative, or zero:

int num = -5;
if (num > 0) {
   printf("%d is positive", num);
} else if (num < 0) {
   printf("%d is negative", num);
} else {
   printf("%d is zero", num);
}


In this case, since num is equal to -5, the condition num > 0 is false, but the condition num < 0 is true, so the code inside the second if block will execute & the message "-5 is negative" will be printed.

Switch statements

Switch statements are used to select one of many code blocks to execute based on the value of a variable. The basic syntax of a switch statement is:

switch (variable) {
   case value1:
      // code to execute if variable equals value1
      break;
   case value2:
      // code to execute if variable equals value2
      break;
   default:
      // code to execute if variable doesn't match any case
}


Here's an example that prints the name of a month based on its number:

int month = 3;
switch (month) {
   case 1:
      printf("January");
      break;
   case 2:
      printf("February");
      break;
   case 3:
      printf("March");
      break;
   default:
      printf("Invalid month");
}


In this example, since month is equal to 3, the code inside the third case block will execute & the message "March" will be printed.

Goto Statements in C

The goto statement in C allows you to transfer control to a labeled statement elsewhere in your program. It is a type of unconditional jump statement that can be used to create complex program flows. However, the use of goto is generally discouraged because it can make code harder to read, understand & maintain.

The basic syntax of a goto statement is:

goto label;
// some code
label:
// code to execute after the goto


Here's an example that demonstrates the use of goto:

  • C

C

#include <stdio.h>

int main() {

  int i = 1;

   loop:

  printf("%d ", i);

  i++;  

  if (i <= 5) {

     goto loop;

  } 

  return 0;

}
You can also try this code with Online C Compiler
Run Code


In this example, the goto statement is used to create a loop that prints the numbers from 1 to 5. The label loop is defined before the printf statement. The goto loop statement transfers control to the labeled statement, creating a loop that continues until the condition i <= 5 is false.

Output : 

1 2 3 4 5


While goto statements can be used to create complex program flows, they are often considered a "code smell" because they can make code difficult to follow & maintain. In most cases, it is better to use structured programming constructs like loops & conditional statements to create clear & readable code.

Here are a few reasons why we should avoid goto statements:

  1. They can make code harder to read & understand because the flow of control can jump around in unpredictable ways.
     
  2. They can make code harder to maintain because changes to the code can have unintended consequences.
     
  3. They can lead to spaghetti code, which is code that is tangled & difficult to follow.
     
  4. They can make it harder to reason about the correctness of code because the flow of control is not always clear.
     

In general, it is best to avoid using goto statements in your C programs unless there is a compelling reason to do so. If you find yourself using goto statements frequently, it may be a sign that your code could be structured in a better way.

Frequently Asked Questions

What is the primary use of decision-making statements in C?

Decision-making statements are used to execute specific parts of code based on conditions, enabling the program to choose different execution paths and respond dynamically to different scenarios.

When should the ternary operator be used instead of traditional if-else statements?

The ternary operator is best used for simple conditional assignments and decisions where the outcome is a straightforward choice between two expressions, making the code more concise and readable.

Why is the use of goto generally discouraged in modern programming?

goto is discouraged because it can lead to complex and hard-to-follow code structures, making maintenance and debugging more challenging. It's typically replaced with structured loops and conditionals that promote clearer and more maintainable code.

Conclusion

In this article, we learned about control structures in C, which are essential for controlling the flow of a program. We discussed different types of control statements, which includes conditional statements (if, if-else, switch), loop statements (for, while, do-while), & jump statements (break, continue, goto). We also explained the importance of using these control structures effectively to create clear, readable & maintainable code. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass