C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - If Else decision making statements

C Programming

In this tutorial we will learn about if-else decision making statements in C programming language.

If statement

We use the if statement to execute a block of code if a given condition is satisfied.

Format of an if statement.

if (condition) {
  //code...
}

So, if the condition of the if statement evaluates to non-zero (true) value then the code inside the if block is executed otherwise, it is ignored.

The if block is within the opening curly bracket { and the closing curly bracket }.

In the following example we take an integer value from the user. If the value is greater than 100 then we print "Entered number is greater than 100." otherwise we don't.

#include <stdio.h>
int main(void)
{
  //declare variable
  int x;
  
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  
  //check condition
  if (x > 100) {
    printf("Entered number is greater than 100.\n");
  }

  printf("End of code\n");
  
  return 0;
}

Output

Enter an integer number: 200
Entered number is greater than 100.
End of code

In the above output we entered an integer number greater than 100 so, we got the output from the if-block.

In the following example we entered an integer number less than 100 so, the if-block is not executed.

Enter an integer number: 99
End of code

If Else statement

If we have two options and we want to execute any one of the option depending on a condition then we use the if-else statement.

Following is the format of an if-else statement.

if (condition) {
  //if block code
}
else {
  //else block code
}

So, when the condition evaluates to a non-zero (true) value then the if-block code is executed. If the condition evaluates to zero (false) value then the else-block code is executed.

In the following example we are checking if user entered a value greater than 10.

#include <stdio.h>
int main(void)
{
  //declare variable
  int x;
  
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  
  //check condition
  if (x > 10) {
    printf("Entered number is greater than 10.\n");
  }
  else {
    printf("Entered number is less than or equal to 10.\n");
  }
  
  printf("End of code\n");
  
  return 0;
}

Output

Enter an integer number: 20
Entered number is greater than 10.
End of code

In the above output the if-block code was executed because the entered integer number was greater than 10.

In the following output we will enter a number less than or equal to 10 and that will result in the execution of the else-block code.

Enter an integer number: 10
Entered number is less than or equal to 10.
End of code

Else If statement

If we want to have more than two options then we use the else if statement.

Format of the else if statement.

if (condition1) {
  //if block code...
}
else if (condition2) {
  //else if block code...
}
else {
  //else block code...
}

In the following example we are checking if the entered integer number is greater than, or less than, or equal to 0.

#include <stdio.h>
int main(void)
{
  //declare variable
  int x;
  
  //take user input
  printf("Enter an integer number: ");
  scanf("%d", &x);
  
  //check condition
  if (x > 0) {
    printf("Entered number is greater than 0.\n");
  }
  else if (x < 0) {
    printf("Entered number is less than 0.\n");
  }
  else {
    printf("Entered number is equal to 0.\n");
  }
  
  printf("End of code\n");
  
  return 0;
}

Output: When number is greater than 0.

Enter an integer number: 10
Entered number is greater than 0.
End of code

Output: When number is less than 0.

Enter an integer number: -100
Entered number is less than 0.
End of code

Output: When number is equal to 0.

Enter an integer number: 0
Entered number is equal to 0.
End of code

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT