Control structures in C programming language Class 10 and 11




Control structures are statements that control the flow of execution in a program. There are three types of control structures. They are:-


1. Selection statements: Selection statements are an important aspect of programming in the C language, as they allow you to control the flow of your program based on certain conditions. With selection statements, you can specify different actions to be taken based on a specific condition.

There are two main selection statements in C: the if statement and the switch statement.

Types of If Statements in C Programming:

  • Simple If Statement: This is the most basic form of if statement, where a single condition is checked and if the condition is true, the corresponding code block is executed.
if (condition) 
{
  /* code to be executed if condition is true */
}

  • If-Else Statement: In this type of if statement, two different code blocks are executed based on the result of the condition. If the condition is true, the first code block is executed, and if the condition is false, the second code block is executed.
if (condition) 
{
  /* code to be executed if condition is true */
else 
{
  /* code to be executed if condition is false */
}

  • Nested If Statement: In this type of if statement, multiple conditions are checked using nested if-else statements. This means that one if statement is placed inside another if statement.
if (condition1) 
{
  /* code to be executed if condition1 is true */
  if (condition2) 
  {
    /* code to be executed if condition2 is true */
  }
  else 
  {
    /* code to be executed if condition2 is false */
  }
else 
{
  /* code to be executed if condition1 is false */
}

  • If-Else-If Ladder: In this type of if statement, multiple conditions are checked using if-else-if statements. If the first condition is true, the corresponding code block is executed and the rest of the conditions are skipped. If the first condition is false, the second condition is checked, and so on.
if (condition1) 
{
  /* code to be executed if condition1 is true */
else if (condition2) 
{
  /* code to be executed if condition2 is true */
else if (condition3) 
{
  /* code to be executed if condition3 is true */
...
else 
{
  /* code to be executed if all conditions are false */
}
  • The switch statement, on the other hand, is used to perform different actions based on multiple conditions. The switch statement allows you to test the value of a variable against a number of cases and execute the corresponding code block. The syntax for a switch statement is as follows:
switch (variable)
{
case value1:
// code to be executed if variable == value1
break;
case value2:
// code to be executed if variable == value2
break;
// ...
default:
// code to be executed if none of the cases match the value of the variable
break;
}

2. Iteration statements: These allow you to execute a block of code multiple times. The three iteration statements in C are the for loop, the while loop, and the do-while loop.


 For Loop:

The syntax of a for loop in C is:

for (initialization; condition; increment/decrement) {

   statement(s);

}

 

Example program for a for loop in C:

#include <stdio.h>

int main() {

   for (int i = 1; i <= 10; i++) {

      printf("%d ", i);

   }

   return 0;

}

 

While Loop:

The syntax of a while loop in C is:

while (condition) {

   statement(s);

}

 

Example program for a while loop in C:

#include <stdio.h>

int main() {

   int i = 1;

   while (i <= 10) {

      printf("%d ", i);

      i++;

   }

   return 0;

}]

 

Do-While Loop:

The syntax of a do-while loop in C is:

do {

   statement(s);

} while (condition);

 

Example program for a do-while loop in C:

#include <stdio.h>

int main() {

   int i = 1;

   do {

      printf("%d ", i);

      i++;

   } while (i <= 10);

   return 0;

}

3. Jump statements: These allow you to transfer control to a different point in the program. The three jump statements in C are break, continue, and goto.

break: The break statement is used to exit a loop prematurely. It terminates the nearest enclosing loop and transfers control to the statement immediately following the loop.

Syntax:

    for (init; condition; increment) {

      // loop body

      if (condition) {

        break;

      }

    }

continue: The continue statement skips the current iteration of a loop and continues with the next iteration.

Syntax:

    for (init; condition; increment) {

      // loop body

      if (condition) {

        continue;

      }

    }

goto: The goto statement transfers control to a label.

Syntax:

    label:

      // statements

    // ...

    goto label;

 

Here is an example of each control structure in C:

 

#include <stdio.h>

 

int main() {

    // Selection statement (if statement)

    int x = 10;

    if (x > 5) {

        printf("x is greater than 5\n");

    }

 

    // Iteration statement (for loop)

    for (int i = 0; i < 10; i++) {

        printf("i is %d\n", i);

    }

 

    // Jump statement (break)

    for (int i = 0; i < 10; i++) {

        if (i == 5) {

            break;

        }

        printf("i is %d\n", i);

    }

 

    return 0;

}

This code first uses an if statement to check if x is greater than 5, and if it is, it prints a message. Then it uses a for loop to print the values of ‘i’ from 0 to 9. Finally, it uses a break statement to exit the loop when ‘i’ reaches 5.

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers