Control Statements in C

Updated

Control statements in C are used to control the flow of program execution based on certain conditions. There are three main types of control statements in C: conditional statements, loop statements, and jump statements.

1. Conditional Statements:
Conditional statements allow a program to make decisions based on whether a certain condition is true or false. The most common conditional statement in C is the “if” statement, which is used to execute a block of code if a certain condition is true. The basic syntax of the if statement is:

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

The “if” statement can also be combined with an “else” statement to execute a different block of code if the condition is false:

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

The “else if” statement can also be used to check multiple conditions:

if (condition1) {
  // Code to execute if condition1 is true
} else if (condition2) {
  // Code to execute if condition2 is true
} else {
  // Code to execute if both condition1 and condition2 are false
}

2. Loop Statements:
Loop statements are used to execute a block of code repeatedly based on a certain condition. The two most common loop statements in C are the “while” loop and the “for” loop.
The “while” loop executes a block of code as long as a certain condition is true:

while (condition) {
  // Code to execute while condition is true
}

The “for” loop is used to execute a block of code a certain number of times:

for (initialization; condition; increment) {
  // Code to execute while condition is true
}

The “initialization” statement is executed only once at the beginning of the loop, the “condition” is checked before each iteration of the loop, and the “increment” statement is executed at the end of each iteration.

3. Jump Statements:
Jump statements are used to transfer control to a different part of the program. The three main jump statements in C are “break”, “continue”, and “goto”.

The “break” statement is used to exit a loop:

while (condition) {
  // Code to execute while condition is true
  if (break_condition) {
    break; // Exit the loop
  }
}

The “continue” statement is used to skip to the next iteration of a loop:

while (condition) {
  if (skip_condition) {
    continue; // Skip to the next iteration of the loop
  }
  // Code to execute if skip_condition is false
}

The “goto” statement is used to transfer control to a different part of the program. It is generally considered bad practice to use “goto”, as it can make code difficult to understand and maintain:

if (condition) {
  goto label;
}
// Code to execute if condition is false
label:
// Code to execute after the goto statement

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Comment
Next tutorial
Mithlesh Upadhyay Published 20 Feb 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials

  • Call by value, call by address and call by reference in C++

    The parameters of the function call are known as actual parameters and the parameters of the function definition are known as formal parameters. There are 3 ways of…

    3 min read
  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. GATE CS 2027 Online Test Series72 tests in the real GATE…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read