C/C++ if else statement with Examples - GeeksforGeeks

22 Nov, 2019
Decision Making in C/C++ helps to write decision driven statements and execute a particular set of code based on certain conditions.
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the C/C++ else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if (condition)
{
    // Executes this block if
    // condition is true
}
else
{
    // Executes this block if
    // condition is false
}
Working of if-else statements
Control falls into the if block.The flow jumps to Condition.Condition is tested.If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.The if-block or the body inside the if is executed.The else block or the body inside the else is executed.Flow exits the if-else block.
Control falls into the if block.
The flow jumps to Condition.
Condition is tested.If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.
If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.
If Condition yields true, goto Step 4.
If Condition yields false, goto Step 5.
The if-block or the body inside the if is executed.
The else block or the body inside the else is executed.
Flow exits the if-else block.
Flowchart if-else:
Example 1:
C
C++
// C program to illustrate If statement  #include <stdio.h>  int main(){    int i = 20;      // Check if i is 10    if (i == 10)        printf("i is 10");      // Since is not 10    // Then execute the else statement    else        printf("i is 20");      printf("Outside if-else block");      return 0;}
// C++ program to illustrate if-else statement  #include <iostream>using namespace std;  int main(){    int i = 20;      // Check if i is 10    if (i == 10)        cout << "i is 10";      // Since is not 10    // Then execute the else statement    else        cout << "i is 20\n";      cout << "Outside if-else block";      return 0;}
i is 20
Outside if-else block

Dry-Running Example 1:
1. Program starts.
2. i is initialized to 20.
3. if-condition is checked. i == 10, yields false.
4. flow enters the else block.
  4.a) "i is 20" is printed
5. "Outside if-else block" is printed.
Example 2:
C
C++
// C program to illustrate If statement  #include <stdio.h>  int main(){    int i = 25;      if (i > 15)        printf("i is greater than 15");    else        printf("i is smaller than 15");      return 0;}
// C++ program to illustrate if-else statement  #include <iostream>using namespace std;  int main(){    int i = 25;      if (i > 15)        cout << "i is greater than 15";    else        cout << "i is smaller than 15";      return 0;}
i is greater than 15

Related Articles:
Decision Making in C / C++C/C++ if statement with ExamplesC/C++ if else if ladder with ExamplesSwitch Statement in C/C++Break Statement in C/C++Continue Statement in C/C++goto statement in C/C++return statement in C/C++ with ExamplesProgram to Assign grades to a student using Nested If Else
Decision Making in C / C++
C/C++ if statement with Examples
C/C++ if else if ladder with Examples
Switch Statement in C/C++
Break Statement in C/C++
Continue Statement in C/C++
goto statement in C/C++
return statement in C/C++ with Examples
Program to Assign grades to a student using Nested If Else
CPP-Basics
C Language
C++
School Programming
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
Command line arguments in C/C++
Core Dump (Segmentation fault) in C/C++
Left Shift and Right Shift Operators in C/C++
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
Multidimensional Arrays in C / C++