Public App

a blog about technology

Conditional Controls

In the C programming language, there are two types of conditional control statements:

If Conditional Controls

  1. if statement: This statement is used to execute a block of code if a certain condition is true. It has the following syntax:
if (condition) {
   // block of code to be executed
}

If the condition is true, the block of code inside the if statement is executed. If the condition is false, the block of code is skipped.

  1. if-else statement: This statement is used to execute one block of code if a certain condition is true, and a different block of code if the condition is false. It has the following syntax:
if (condition) {
   // block of code to be executed if the condition is true
} else {
   // block of code to be executed if the condition is false
}

If the condition is true, the first block of code is executed and the second block of code is skipped. If the condition is false, the second block of code is executed and the first block of code is skipped.

Here’s an example of an if-else statement that determines if a number is even or odd:

int num = 4;
if (num % 2 == 0) {
   printf("The number is even.\n");
} else {
   printf("The number is odd.\n");
}

In this example, the condition num % 2 == 0 checks if the remainder of num divided by 2 is 0, which indicates that num is even. If the condition is true, the first block of code is executed and “The number is even.” is printed to the console. If the condition is false, the second block of code is executed and “The number is odd.” is printed to the console.

Switch Conditional Control

The switch statement is a control statement in the C programming language that allows a program to evaluate an expression and execute a specific block of code based on the value of that expression. It has the following syntax:

switch (expression) {
   case value1:
      // block of code to be executed if the expression matches value1
      break;
   case value2:
      // block of code to be executed if the expression matches value2
      break;
   ...
   default:
      // block of code to be executed if the expression does not match any of the values
      break;
}

Here’s how the switch statement works:

  1. The expression is evaluated.
  2. The value of the expression is compared to each of the case values in the switch statement. If a match is found, the block of code following the matching case label is executed. If no match is found, the default block of code is executed (if it is present).
  3. If a matching case block of code is executed, the program continues to execute the remaining statements after the end of the switch statement. If a break statement is encountered, control is transferred to the end of the switch statement, and the program continues to execute the remaining statements.

Here’s an example of a switch statement that determines the day of the week based on a numerical value:

int day = 3;
switch (day) {
   case 1:
      printf("Monday\n");
      break;
   case 2:
      printf("Tuesday\n");
      break;
   case 3:
      printf("Wednesday\n");
      break;
   case 4:
      printf("Thursday\n");
      break;
   case 5:
      printf("Friday\n");
      break;
   case 6:
      printf("Saturday\n");
      break;
   case 7:
      printf("Sunday\n");
      break;
   default:
      printf("Invalid day\n");
      break;
}

In this example, the expression is the variable day. The case labels are the values 1 through 7, representing the days of the week. If day is 3, the block of code following the case 3 label is executed, which prints “Wednesday” to the console. If day is any other value, the default block of code is executed, which prints “Invalid day” to the console.

Amitesh Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top