ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    CSI-311
    Progress0 / 17 topics
    Topics
    1. Overview of Computers and Programming2. Overview of Languages (e.g., C Language)3. Basics of Structured and Modular Programming4. Basic Algorithms and Problem Solving5. Development of Basic Algorithms6. Analyzing Problems7. Designing Solutions8. Testing Designed Solutions9. Fundamental Programming Constructs10. Translation of Algorithms to Programs11. Data Types12. Control Structures13. Functions14. Arrays15. Records16. Files17. Testing Programs
    CSI-311›Control Structures
    Programming FundamentalsTopic 12 of 17

    Control Structures

    7 minread
    1,158words
    Intermediatelevel

    Control Structures in C Language

    Control structures in C are used to control the flow of execution in a program. They help in making decisions, repeating tasks, and branching to different sections of code based on conditions. C provides several types of control structures that allow developers to control the execution path. These are:

    1. Decision-Making Structures
    2. Looping Structures
    3. Jumping Structures

    Let's dive into each of these in detail:


    1. Decision-Making Structures

    These structures allow the program to make decisions and execute different blocks of code based on conditions. The most common decision-making structures in C are:

    a) if Statement

    The if statement is used to test a condition. If the condition evaluates to true, the block of code inside the if statement is executed.

    • Syntax:

      if (condition) {
          // code to be executed if the condition is true
      }
      
    • Example:

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

      Output: x is greater than 5

    b) if-else Statement

    The if-else statement allows you to execute one block of code if the condition is true and another block if it is false.

    • Syntax:

      if (condition) {
          // code to be executed if the condition is true
      } else {
          // code to be executed if the condition is false
      }
      
    • Example:

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

      Output: x is less than or equal to 5

    c) if-else if-else Statement

    The if-else if-else statement is used when there are multiple conditions to check. It allows you to test multiple conditions sequentially.

    • Syntax:

      if (condition1) {
          // code if condition1 is true
      } else if (condition2) {
          // code if condition2 is true
      } else {
          // code if none of the above conditions are true
      }
      
    • Example:

      int x = 8;
      if (x > 10) {
          printf("x is greater than 10\n");
      } else if (x == 8) {
          printf("x is equal to 8\n");
      } else {
          printf("x is less than 8\n");
      }
      

      Output: x is equal to 8

    d) switch Statement

    The switch statement is used when you have multiple possible values for a variable, and you want to choose among them. It is typically more efficient than using multiple if-else conditions for the same variable.

    • Syntax:

      switch (expression) {
          case constant1:
              // code to be executed if expression equals constant1
              break;
          case constant2:
              // code to be executed if expression equals constant2
              break;
          default:
              // code to be executed if no case matches
      }
      
    • Example:

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

      Output: Wednesday


    2. Looping Structures

    Looping structures are used to repeat a block of code multiple times as long as a certain condition is met. C provides several looping structures:

    a) for Loop

    The for loop is typically used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and update.

    • Syntax:

      for (initialization; condition; update) {
          // code to be executed
      }
      
    • Example:

      for (int i = 1; i <= 5; i++) {
          printf("i = %d\n", i);
      }
      

      Output:

      i = 1
      i = 2
      i = 3
      i = 4
      i = 5
      

    b) while Loop

    The while loop is used when the number of iterations is not known in advance, but the loop continues as long as a condition is true.

    • Syntax:

      while (condition) {
          // code to be executed
      }
      
    • Example:

      int i = 1;
      while (i <= 5) {
          printf("i = %d\n", i);
          i++;
      }
      

      Output:

      i = 1
      i = 2
      i = 3
      i = 4
      i = 5
      

    c) do-while Loop

    The do-while loop is similar to the while loop, but it guarantees that the code block will be executed at least once, regardless of the condition.

    • Syntax:

      do {
          // code to be executed
      } while (condition);
      
    • Example:

      int i = 1;
      do {
          printf("i = %d\n", i);
          i++;
      } while (i <= 5);
      

      Output:

      i = 1
      i = 2
      i = 3
      i = 4
      i = 5
      

    3. Jumping Structures

    Jumping structures allow you to control the flow of execution by jumping to different parts of the code. The most commonly used jumping structures in C are break, continue, and goto.

    a) break Statement

    The break statement is used to exit from a loop or switch statement prematurely. It immediately terminates the loop or switch.

    • Example (in a loop):
      for (int i = 1; i <= 10; i++) {
          if (i == 5) {
              break;  // Exit the loop when i is 5
          }
          printf("i = %d\n", i);
      }
      
      Output:
      i = 1
      i = 2
      i = 3
      i = 4
      

    b) continue Statement

    The continue statement skips the current iteration of a loop and moves to the next iteration. It does not terminate the loop, but it prevents the remaining code in the current iteration from executing.

    • Example (in a loop):
      for (int i = 1; i <= 5; i++) {
          if (i == 3) {
              continue;  // Skip the iteration when i is 3
          }
          printf("i = %d\n", i);
      }
      
      Output:
      i = 1
      i = 2
      i = 4
      i = 5
      

    c) goto Statement

    The goto statement is used to transfer control to a specific label in the program. While it allows you to jump to any part of the program, its use is generally discouraged because it can make code harder to read and maintain.

    • Syntax:

      goto label_name;
      
      label_name:
          // code to jump to
      
    • Example:

      int i = 0;
      start:
          printf("i = %d\n", i);
          i++;
          if (i < 5)
              goto start;
      

      Output:

      i = 0
      i = 1
      i = 2
      i = 3
      i = 4
      

    Summary of Control Structures in C

    1. Decision-Making Structures:

      • if, if-else, if-else if-else, switch
    2. Looping Structures:

      • for, while, do-while
    3. Jumping Structures:

      • break, continue, goto

    Control structures are fundamental to programming in C as they determine the flow of control and allow you to make decisions, repeat actions, and jump to different parts of your program. By using these control structures effectively, you can create complex, efficient, and well-organized programs.

    Previous topic 11
    Data Types
    Next topic 13
    Functions

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,158
      Code examples0
      DifficultyIntermediate