Skip to main content

Decision Control Structure in C Language

 In the C programming language, decision control structures allow you to control the flow of execution based on specific conditions. These structures help make decisions and choose different paths of execution within your program. There are three main decision control structures in C: if statement, if-else statement, and switch statement. Let's explore each one:

1. if Statement:
   The if statement allows you to execute a block of code if a condition is true. It has the following syntax:
   ```c
   if (condition) {
       // code to execute if the condition is true
   }
   ```
   Example:
   ```c
   int x = 5;
   if (x > 0) {
       printf("x is positive");
   }
   ```

2. if-else Statement:
   The if-else statement provides an alternative block of code to execute if the condition is false. It has the following syntax:
   ```c
   if (condition) {
       // code to execute if the condition is true
   }
   else {
       // code to execute if the condition is false
   }
   ```
   Example:
   ```c
   int x = -5;
   if (x > 0) {
       printf("x is positive");
   }
   else {
       printf("x is non-positive");
   }
   ```

3. switch Statement:
   The switch statement allows you to choose from multiple options based on the value of an expression. It has the following syntax:
   ```c
   switch (expression) {
       case value1:
           // code to execute if expression matches value1
           break;
       case value2:
           // code to execute if expression matches value2
           break;
       // more cases...
       default:
           // code to execute if expression does not match any case
   }
   ```
   Example:
   ```c
   int choice = 2;
   switch (choice) {
       case 1:
           printf("Option 1 selected");
           break;
       case 2:
           printf("Option 2 selected");
           break;
       default:
           printf("Invalid option");
   }
   ```

These decision control structures allow you to write code that can make decisions based on conditions and choose different paths of execution. By utilizing these structures, you can control the flow of your program and make it more dynamic and responsive to different scenarios.

 

Certainly! Here are examples of decision control structures in C, starting from basic to more advanced scenarios:

1. Basic if Statement:
   ```c
   int x = 10;
   if (x > 0) {
       printf("x is positive");
   }
   ```

2. if-else Statement:
   ```c
   int x = -5;
   if (x > 0) {
       printf("x is positive");
   }
   else {
       printf("x is non-positive");
   }
   ```

3. Nested if-else Statement:
   ```c
   int x = 10;
   if (x > 0) {
       if (x % 2 == 0) {
           printf("x is a positive even number");
       }
       else {
           printf("x is a positive odd number");
       }
   }
   else {
       printf("x is non-positive");
   }
   ```

4. if-else if-else Ladder:
   ```c
   int marks = 85;
   if (marks >= 90) {
       printf("Grade: A");
   }
   else if (marks >= 80) {
       printf("Grade: B");
   }
   else if (marks >= 70) {
       printf("Grade: C");
   }
   else if (marks >= 60) {
       printf("Grade: D");
   }
   else {
       printf("Grade: F");
   }
   ```

5. switch Statement:
   ```c
   int choice = 2;
   switch (choice) {
       case 1:
           printf("Option 1 selected");
           break;
       case 2:
           printf("Option 2 selected");
           break;
       default:
           printf("Invalid option");
   }
   ```

6. Advanced switch Statement:
   ```c
   char grade = 'B';
   switch (grade) {
       case 'A':
       case 'a':
           printf("Excellent");
           break;
       case 'B':
       case 'b':
           printf("Good");
           break;
       case 'C':
       case 'c':
           printf("Average");
           break;
       case 'D':
       case 'd':
           printf("Below Average");
           break;
       default:
           printf("Invalid grade");
   }
   ```

These examples demonstrate various scenarios where decision control structures in C can be used to handle different conditions and make appropriate decisions based on those conditions. By utilizing these structures effectively, you can write more flexible and responsive programs.

Comments

Popular posts from this blog

Gujarati Keyboard layout (terafont-varun), Computer Short cut key, Tally short cut key

Word , Excel , Power Point Shortcut Key in Gujarati

Terafont-Varun (Gujarati Typing) Keyboard Layout by "Sama Soyab"

  For Gujarati Typing : Required : Terafont-Varun Font  After Successfully Installed Terafont Varun Open Any Text Editor or any program. Select Font Terafont-Varun -> Ok For more detail please watch below video. Search Topics : Learn terafont varun, Learn terafont chandan, Learn terafont gujarati to english translation, Learn terafont varun keyboard, Learn terafont converter, Learn terafont varun zip, Learn terafont keyboard, Learn terafont kinnari, Learn terafont akash, Learn terafont aakash, Learn terafont akash ttf, Learn terafont aakash gujarati download, Learn terafont akash keyboard, Learn terafont akash download for windows 10, Learn terafont akash font download, Learn terafont arun, Learn terafont border, Learn terafont chandan keyboard, Learn terafont-chandan font, Learn tera font chandana, Learn convert terafont to shruti, Learn convert terafont varun to shruti, Learn terafont varun chart, Learn terafont download, Learn terafont download for windows 10, Learn tera...