C if-else Statement Language Skill UP
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Understanding the C if-else Statement: A Complete Guide

📑 On this page:
  • Introduction to if-else
  • Syntax of if-else
  • Example: Even or Odd
  • Flowchart Explanation
📚 In this tutorial, you will learn:
  • What the if-else statement is and why it matters in C
  • How to write if-else syntax correctly
  • How to check even/odd numbers using if-else
  • How to use flowcharts to understand program flow

Understanding the C if-else Statement

In C programming, the if-else statement is a decision-making statement. This statement decides whether the statements or block of code should be executed or not based on conditions. It is one of the simplest and most important conditional statements in C language.

Using the if-else statement, the flow of execution of a program can be changed or modified. Basically, the if-else statement controls the flow of a program and is also termed a control flow statement. It decides whether a certain statement or block of statements will be executed or not based on a given condition.

For example, if a given condition is true, then the statements or block of statements inside the if body are executed; otherwise, the code in the else block will be executed. if-else allows code to be executed conditionally based on a boolean condition. if-else statements make the block of code more readable.

How the if-else Statement Works

If the "condition" is true:
• The statements inside the body of the if control statement are executed.
• Statements inside the body of the else statement are ignored.

If the "condition" is false:
• The statements inside the body of the if are ignored.
• Statements inside the else part are executed.


Syntax: if-else Statement

if (condition)
{
    // if body
    // Statements to execute if condition is true
}
else
{
    // Statements to execute if condition is false
}

Step-by-step execution:
1. The program control first reaches the if statement and tests the condition.
2. If the test condition is true: The if block is executed.
3. If the test condition evaluates to false: The else block is executed.
4. Afterward, the program control continues to the remaining statements below inside the program.
5. The condition in the syntax evaluates to true (i.e., 1) or false (i.e., 0).

The C language instructions or statements can be a single line or multiple lines of code enclosed within curly braces { }.

Flowchart: if-else Statement

Diagram illustrating the flowchart and syntax of the C if-else statement for decision making.

The diagram illustrates the syntax and flowchart of the if-else statement.

How it works:
1. If the tested condition is true, then the statements inside the if block are executed.
2. If the tested condition is false, then the code or statements inside the else block are executed.
Note that "statements outside if" always execute regardless of whether the condition is true or false.


Example 1: C Program to Check Even or Odd

/* C program to check if a given number is even or odd using if-else statement */
#include <stdio.h>

int main()
{
    int x;
    printf("\n Enter any number: ");
    scanf("%d", &x);
    
    if(x % 2 == 0)  /* test-condition */
    {
        printf("\n %d is an even number", x);
    }
    else            /* else executes when condition is false */
    {
        printf("\n %d is an odd number", x);
    }
    
    return 0;
}

/* Output:
   Enter any number: 7
   7 is an odd number */

Program Explanation

The above program demonstrates the use of an if-else statement to determine whether a given number is even or odd.

1. First, we declare an integer variable int x; to store the number entered by the user.

2. Next, we use printf("\n Enter any number"); to display a prompt on the screen, and scanf("%d", &x); to read the user's input. In this example, the user enters 7, which is stored in the variable x.

3. The program then evaluates the if condition x % 2 == 0 to check whether the number is even or odd. Since 7 % 2 gives a remainder of 1, the condition x % 2 == 0 evaluates to false.

4. Because the condition is false, the else block is executed. The statement printf("\n %d is an odd number", x); runs, and the output displayed is: "7 is an odd number".

Note: The %d is a format specifier that acts as a placeholder for the integer value of x. It is replaced with the actual value 7 when the output is displayed, resulting in the message: "7 is an odd number".

💻 Practice Exercise

Challenge: Write a program that checks if a number is positive, negative, or zero using if-else statement.

🔍 Click to Show Solution
#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    
    if(num > 0) {
        printf("%d is a positive number\n", num);
    }
    else if(num < 0) {
        printf("%d is a negative number\n", num);
    }
    else {
        printf("The number is zero\n");
    }
    
    return 0;
}
                                        

Expected Output:

Enter a number: -5
-5 is a negative number
                                        

Frequently Asked Questions About if-else in C

1. What is the difference between if and if-else in C?

The if statement executes code only when the condition is true. The if-else statement executes one block when the condition is true and another when it is false.

2. Can I use multiple conditions in if-else?

Yes, you can combine multiple conditions using logical operators like && (AND) and || (OR). You can also use nested if-else or if-else ladder for multiple conditions.

3. Is else mandatory with if in C?

No, the else block is optional. You can use if alone when you only need to execute code when a condition is true.

📖 Related Tutorials

  • if statement in C
  • if else if ladder in C
  • Nested if-else in C
  • Switch Case in C

Previous Topic: -->> if statement in C   ||   Next topic: -->> if else if ladder in C


📚 Explore More Programming Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.