Programmer Coding

If.. else Statements in C Language

Control Flow in C?

The control-flow of a language specify the order in which computations are performed. We have already met the most common control-flow constructions in earlier examples; here we will complete the set, and be more precise about the ones discussed before.

Decision Control Statements

C supports decision control statements that can alter the flow of a sequence of instructions. These statements help to jump from one part of the program to another depending on whether a particular condition is satisfied or not. These decision control statements include

(a) if statement

(b) if–else statement

(c) if–else–if statement

(d) switch–case statement

(a) if statement

The if block may include 1 statement or n statements enclosed within curly brackets. First the test expression is evaluated. If the test expression is true, the statements of the if block are executed, otherwise these statements will be skipped and the execution will jump to statement x.

The statement in an if block is any valid C language statement, and the test expression is any valid C language expression that evaluates to either true or false. In addition to simple relational expressions, we can also use compound expressions formed using logical operators. Note that there is no semi-colon after the test expression. This is because the condition and statement should be put together as a single statement

Example

#include <stdio.h>

int main() {

int num = 10;

// Check if the number is greater than 0

if (num > 0) {

printf(“Number is positive\n”);

}

return 0;

}

Output

Number is positive

(b) if–else statement,

We have studied that the if statement plays a vital role in conditional branching. Its usage is very simple, the test expression is evaluated, if the result is true, the statement(s) followed by the expression is executed else if the expression is false, the statement is skipped by the compiler

But what if you want a separate set of statements to be executed if the expression returns a zero value? In such cases we use an if-else statement rather than using simple if statement. The general form of a simple if-else statement

In the above syntax, we have written statement block. A statement block may include one or more statements. According to the if-else construct, first the test expression

Example

#include <stdio.h>

int main() {

int num = -5;

// Check if the number is greater than 0

if (num > 0) {

printf(“Number is positive\n”);

} else {

printf(“Number is non-positive\n”);

}

return 0;

}

Output

Number is non-positive

(c) if–else–if statement,

C language supports if–else–if statements to test additional conditions apart from the initial test expression.

After the first test expression or the first if branch, the programmer can have as many else–if branches as he wants depending on the expressions that have to be tested. For example, the following code tests whether a number entered by the user is negative, positive, or equal to zero.

Example

#include <stdio.h>

int main() {

int num = 0;

// Check if the number is positive, negative, or zero

if (num > 0) {

printf(“Number is positive\n”);

} else if (num < 0) {

printf(“Number is negative\n”);

} else {

printf(“Number is zero\n”);

}

return 0;

}

Output

Number is zero

(d) switch–case statement.

The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.

Each case is labeled by one or more integer-valued constants or constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied. A default is optional; if it isn’t there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order

Example

#include <stdio.h>

int main() {

int option = 2;

// Check the value of the option and perform corresponding action

switch (option) {

case 1:

printf(“Option 1 selected\n”);

break;

case 2:

printf(“Option 2 selected\n”);

break;

case 3:

printf(“Option 3 selected\n”);

break;

default:

printf(“Invalid option\n”);

}

return 0;

}

Output

Option 2 selected

Advantages of Using a switch–case Statement

Switch–case statement is preferred by programmers due to the following reasons:

  • Easy to debug
  • Easy to read and understand
  • Ease of maintenance as compared to its equivalent if–else statements
  • Like if–else statements, switch statements can also be nested
  • Executes faster than its equivalent if–else construct

Leave a Comment

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

Scroll to Top