Conditional Statement: IF, IF Else and nested in C program DeveloperIndian

3/14/2023

C Conditional Statements #clang #if_else #

Go Back

C Conditional Statements: IF, IF-Else, and Nested Blocks

The 'if' statement will run the block of code , the Boolean expression evaluates to true or false.

Introduction

Conditional statements are crucial in C programming. They control the flow of execution based on specified conditions. This guide explains the if, if-else, and nested if statements with proper syntax and examples.

C Conditional Statements #clang #if_else #

IF Statement in C

The if statement executes a block of code only if a given condition evaluates to true.

Syntax:

if (condition or expression) {
   /* block of code */
}

Example:

#include <stdio.h>

int main() {
   int a = 15;

   if (a < 20) {
      if (a < 10)
         printf("a is less than 10\n");
      else if (a == 10) // Fixed assignment operator error (a=10 to a == 10)
         printf("a is equal to 10\n");
      else
         printf("a is greater than 10\n");
   }

   printf("Value of a is: %d\n", a);
   return 0;
}

Explanation:

  1. Declares an integer a with the value 15.
  2. The outer if statement checks if a < 20, which is true.
  3. The nested if-else block evaluates further:
    • If a < 10, prints "a is less than 10".
    • If a == 10, prints "a is equal to 10" (fixed comparison error).
    • Otherwise, prints "a is greater than 10".

Conclusion

  • The if-else statement allows decision-making in C.
  • Nested if statements help evaluate multiple conditions.
  • C is case-sensitive, so if and else must be in lowercase.

Here we see a exapmle of  If Else in C language .

If...else and if...else  nested is usefull Keyword .
As C is a case sensitive language, If Else is keyword must written in  lowercase.
 
It's occasionally , an if statement inside another if statement. Nested if statements are those that have an if statement inside of another if statement.