If Else Statement

Learn how to use if, else if, and else statements in C programming with clear examples. Build your understanding step by step in Debian 12 using Vim.

If Else Statement in C Programming

If-Else statements are fundamental in C for decision making. They let your program execute different blocks of code based on conditions. This lesson shows how to use if, else if, and else with practical examples.

What is If-Else?

An if statement checks a condition and executes a block if the condition is true. Else if adds more conditions, and else handles all remaining cases.

Example Code

This program checks if a number is positive, negative, or zero:

#include 

int main() 
    int number;
    printf(\\"Enter a number: \\");
    scanf(\\"%d\\", &number);

    if (number > 0) 
        printf(\\"The number is positive.\\\
\\");
     else if (number < 0) 
        printf(\\"The number is negative.\\\
\\");
     else 
        printf(\\"The number is zero.\\\
\\");
    

    return 0;

Compiling and Running

To compile and run on Debian 12:

gcc ifelse.c -o ifelse
./ifelse

Expected Output

Enter a number: 5
The number is positive.

Conclusion

Using if, else if, and else lets you control the program’s flow based on conditions. Practice with different numbers to see how it behaves.

Subscribe to Our YouTube for More

Download as PDF

https://blog.arashtad.com/updates/if-else-statement/?feed_id=12575&_unique_id=688926f072ed8

Comments

Popular posts from this blog

Why Smart Contracts Are Revolutionary? (Smart Contracts Use Cases)

A Quick Guide to Tron Network Interaction using Python.

WordPress: The best CMS platform to create website