CodersEditor Tutorials

CodersEditor Tutorials

  • Online IDE
  • Q&A
  • C Programming
  • Java
  • GraphQL.NET
  • System Software

›Control Flow/Decision Making

C Programming

    Introduction

    • Overview
    • Installation & Setup
    • C Program Structure
    • Keywords & Identifiers
    • Data Types
    • Variables & Constants
    • Scope Rules
    • Storage Class
    • Standard Input and Output
    • Operators

    Control Flow/Decision Making

    • Overview
    • if Statement
    • if else Statement
    • Nested If statement
    • Switch Statement
    • Nested Switch Statement
    • Conditional Operators

    Loop

    • Loops Overview
    • While loop
    • For Loop
    • Do while
    • Nested Loops
    • Break
    • Continue Statement
    • Goto statement

    Arrays

    • Arrays
    • Multi Dimensional Arrays
    • Arrays & Functions

    Pointers

    • Pointers Basics
    • Pointers and Arrays
    • Pointers and Functions
    • Memory allocation

    Strings

    • String Basics
    • String Functions

    C Programming Examples

    • Basic Example 1

if statement in C Programming

This article is about the if statement in C programming and how and where it can be used in the program.

If statement in C programming

The If statement is used when you have to execute a block of statements only when the condition is true. There are various If statements like if else, else if, else and nested if.

Syntax if(condition) { … //set of statements … }

Here the foremost thing is that the compiler will check the condition given. If the condition is true then the set of statements inside ‘if’ will be executed but if the condition returns false then the statements inside 'if' will be skipped.

Example

#include<stdio.h>
int main()
{
    int age = 18;
    if (age>=18)
    {
        printf("eligible to vote");
    }
    return 0;
}

Output

eligible to vote

In this example, the condition ‘ age>=18 ’ is checked first, since it is 18 the statement inside ‘if’ is executed.

Multiple If statements

Multiple If statements can also be used to check more than one condition.

Example

#include<stdio.h>
int main()
{
    int a, b;
    printf("Enter value a:\n");
    scanf("%d", &a);
    printf("Enter value b:\n");
    scanf("%d", &b);
    if (a>b)
    {
        printf("a is greater than b");
    }
    if (a<b )
    {
        printf("a is lesser than b");
    }
    if (a==b)
    {
        printf("a equals b");
    }
    return 0;
}

Output

Enter value a:10
Enter value b:20
a is lesser than b
← Overviewif else Statement →
  • If statement in C programming
    • Multiple If statements
CodersEditor Tutorials
CoderdEditor Developer Tools
CodersEditor Online EditorGit Flavoured Markup EditorFree Online JSON FormatterFind My IP Address Details
Tutorials
C ProgrammingJava
Copyright © 2021 CodersEditor.com