Else Keyword


In C programming, the 'else' keyword is used in conjunction with the 'if' statement to define an alternative block of code that is executed when the condition of the 'if' statement evaluates to false.

Here's an example of using the 'else' keyword:


int x = 5;
if (x < 0) {
  printf("x is negative");
} else {
  printf("x is non-negative");
}

    

In this example, the 'if' statement checks if the value of 'x' is less than zero.

If the condition is true, the code within the 'if' block is executed. If the condition is false, the code within the 'else' block is executed instead.

In this case, since 'x' is 5, which is non-negative, the statement "x is non-negative" is printed.

The 'else' keyword is optional and provides an alternative course of action when the condition of the preceding 'if' statement is not satisfied.

It allows for branching in the code based on different conditions and provides flexibility in controlling program flow.

Loading...

Search