LATEST

How to use if - else statement in C / C++

In this tutorial, we will learn about if - else statement in C / C++ and its working with the help of some examples. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.

In C / C++ programming, if - else statement is used to execute a block of code in a certain condition. There are two cases:

  • If condition is true: Execute block of code.
  • If the condition is false: Skip the code block and execute the statements below.

For example: assigning grades (A, B, C) based on marks obtained by a student.

  • If the percentage is above 90, assign grade A.
  • If the percentage is above 75 and below 90, assign grade B.
  • If the percentage is above 65 and below 75, assign grade C.

There are three forms of if - else statements in C / C++.

  • if statement in C / C++.
  • if - else statement in C / C++.
  • if - else else - if statement in C / C++.

Now, we will learn about each form of if - else statement in C / C++ one by one.

if statement in C / C++

The if statement in C / C++ is used in case we only have a single condition. That means, if the condition is true then do something and if the condition is false then skip.

The syntax of if statement in C / C++:

if (condition) {
  // body of if statement
}

Here,

  • condition: This is the condition of the if statement.
    • If the condition evaluates to true: The block of code inside the if statement body is executed.
    • If the condition evaluates to false: The block of code inside the if statement body is skipped.

*Note: Condition is placed inside ( ) and code block is placed inside { }.

if else 01 PNG

For example: Check positive with if statement in C / C++.

C++ programming:

/* Program to print positive number entered by the user
 If the user enters a negative number, it is skipped */

#include <iostream>
using namespace std;

int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;
  // checks if the number is positive
  if (number > 0) {
    cout << "You entered a positive integer: " << number << endl;
  }
  cout << "This statement is always executed.";

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int number;
  printf("Enter an integer: ");
  scanf("%d", &number);
  // checks if the number is positive
  if (number > 0) {
    printf("You entered a positive integer: %d\n",number);
  }
  printf("This statement is always executed.");

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

if else 02 PNG

if - else statement in C / C++

The if - else statement is used in case of multiple conditions. Each condition will execute a different block of code.

The syntax of if - else statement in C / C++:

if (condition) {
  // block of code if condition is true
}
else {
  // block of code if condition is false
}

Explain:

  • If the condition evaluates true:
    • The code inside the body of the if is executed.
    • The code inside the body of the else is skipped from execution.
  • if the condistion evaluates false:
    • The code inside the body of the else is executed.
    • The code inside the body of the if is skipped from execution.

if else 03 PNG

For example: Check even or odd number with if - else statement in C / C++.

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;
  if (number % 2 == 0) {
    cout << "You entered a even number: " << number << endl;
  }
  else{
    cout << "You entered a odd number: " << number << endl;
  }

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int number;
  printf("Enter an integer: ");
  scanf("%d", &number);
  if (number % 2 == 0) {
    printf("You entered a even number: %d\n",number);
  }
  else{
    printf("You entered a odd number: %d\n",number);
  }

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

if else 04 PNG

if - else else - if statement in C / C++

The if - else else if statement is used in case there are more than two alternatives.

The syntax of if - else else - if statement in C / C++:

if (condition1) {
  // code block 1
}
else if (condition2){
  // code block 2
}
else {
  // code block 3
}

Explain:

  • If the condition 1 is true: The block code 1 is executed.
  • if the condition 1 is false: The block code 1 is skipped and the condition 2 is evaluated.
    • If the condition 2 is true: The block code 2 is excuted.
    • If the condition 2 is false: The block code 3 is executed.

For example: Program to check whether an integer is positive, negative or zero.

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;
  
  if (number > 0) {
    cout << "You entered a positive integer: " << number << endl;
  } 
  else if (number < 0) {
    cout << "You entered a negative integer: " << number << endl;
  } 
  else {
    cout << "You entered 0." << endl;
  }

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int number;
  printf("Enter an integer: ");
  scanf("%d", &number);
  if (number > 0) {
    printf("You entered a positive integer: %d\n",number);
  } 
  else if (number < 0) {
    printf("You entered a negative integer: %d\n",number);
  } 
  else {
    printf("You entered 0.\n");
  }

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output 1:

if else 05 PNG

Output 2:

if else 06 PNG

Output 3:

if else 07 PNG

Nestef if statement in C / C++

The nested if statement occurs where one if statement is inside another if statement.

Think of it as a multi-class if statement. The outer class is an if statement and its inner class adds one more if statement.

The syntax of nested if statement in C / C++:

// outer if statement
if (condition1) {

  // statements

  // inner if statement
  if (condition2) {
    // statements
  }
}

For example: Program to check whether an integer is positive, negative or zero using nested if statement in C / C++.

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;

  if(number != 0){
    if (number > 0) {
      cout << "You entered a positive integer: " << number << endl;
    } 
    else if (number < 0) {
      cout << "You entered a negative integer: " << number << endl;
    } 
  }
  else {
    cout << "The number is 0 and it is neither positive nor negative." << endl;
  }

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int number;
  printf("Enter an integer: ");
  scanf("%d", &number);
  if(number != 0){
    if (number > 0) {
      printf("You entered a positive integer: %d\n",number);
    } 
    else if (number < 0) {
      printf("You entered a negative integer: %d\n",number);
    } 
  }
  else {
    printf("The number is 0 and it is neither positive nor negative.\n");
  }

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

if else 08 PNG

Body of if...else with only one statement

If the body of if - else has only one statement, you can omit { } in the program. This makes your program look cleaner.

if (condition){
    //statement
}

//or

if (condition) //statement

Example 1:

int number = 5;

if (number > 0) {
  cout << "The number is positive." << endl;
}
else {
  cout << "The number is negative." << endl;
}

Or

int number = 5;

if (number > 0)
  cout << "The number is positive." << endl;
else
  cout << "The number is negative." << endl;

if - else statement in C / C++ with examples

Here, we have two examples with if - else statement in C/C++. The program is written in C and C++ languages.

Example 1: Find the largest of the three numbers with if - else statement in C / C+.

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int a, b, c, max;
    cout << "Enter number a = ";
    cin >> a;
    cout << "Enter number b = ";
    cin >> b;
    cout << "Enter number c = ";
    cin >> c;
    max = a;
    if(max < b) {
        max = b;
    }
    if(max < c) {
        max=c; 
    }
    cout << "The largest of the three numbers " << a << ", " << b << ", " << c << " is: " << max;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <iostream>
using namespace std;

int main() {
  int a, b, c, max;
    cout << "Enter number a = ";
    cin >> a;
    cout << "Enter number b = ";
    cin >> b;
    cout << "Enter number c = ";
    cin >> c;
    max = a;
    if(max < b) {
        max = b;
    }
    if(max < c) {
        max=c; 
    }
    cout << "The largest of the three numbers " << a << ", " << b << ", " << c << " is: " << max;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

Output:

if else 10 PNG

Example 2: Check even and odd numbers with if - else statement in C / C++.

C++ programming:

#include <iostream>
using namespace std;

int main() {
  int number;
  cout << "Enter an integer: ";
  cin >> number;

  if(number != 0){
    if (number %2 == 0) {
      cout << "You entered a even number: " << number << endl;
    } 
    else{
      cout << "You entered a odd number: " << number << endl;
    } 
  }
  else {
    cout << "This is zero." << endl;
  }

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int number;
  printf("Enter an integer: ");
  scanf("%d", &number);

  if(number != 0){
    if (number %2 == 0) {
      printf("You entered a even number: %d\n",number);
    } 
    else{
      printf("You entered a odd number: %d\n",number);
    } 
  }
  else {
    printf("This is zero\n");
  }

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

if else 09 PNG

Cùng chuyên mục:

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

Searching and sorting arrays in C / C++

Searching and sorting arrays in C / C++

How to use arrays in C / C++

How to use arrays in C / C++

How to use goto statement in C / C++

How to use goto statement in C / C++

How to use switch...case statement in C / C++

How to use switch...case statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use do while loop in C / C++

How to use do while loop in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

How to use for loop in C / C++

How to use for loop in C / C++

Top