if statement in C

In the if statement, if is the keyword in C.

Syntax:

if (condition is true){
/*block of statements to be executed*/
}

The keyword if tells the compiler that what follows is decision control statement. The block of statements to be executed must be enclosed in opening and closing braces. If only one statement needs to be executed then braces need not be used. Let us understand if statement using an example.

Example of if statement in C:

int a = 10;
if (a == 10){
    printf(“You are in if block\n”);
    printf(“The value of a is %d\n”, a);
}
printf(“You are out of if block”);

In the above example we have assigned value 10 to a. In the next line, there is an if statement. It is read as “If a is equal to 10 then perform the block of statements enclosed within the braces”. After the execution of this block normal sequence flow of the program is executed.

The output of the above example is:
You are in if block
The value of a is 10
You are out of if block

Lets have a look at different expressions that can be used in if statement.

a == b read as a is equal to b
a!=b read as a is not equal to b
a < b read as a is less than b a > b read as a is greater than b
a<=b read as a is less than or equal to b a>=b read as a is greater than or equal to b

Example:

/*Demonstration of if statement*/
int marks;
printf(“Enter the marks:”);
scanf(“%d”, &marks);
if(marks>=35){
	printf(“Congrats!!!”);
}

In the above program, it will prompt you to enter the marks. If you enter marks greater than or equal to 35, it will display Congrats!!! on the screen else it will do nothing.

You may also like...

23 Responses

  1. Anonymous says:

    awesome tutorial

    • sinya says:

      I don understand the if in block…..all the block thing……pls explain to me clearly…pls pls

  2. Anonymous says:

    That is fantastic!

  3. Anonymous says:

    wat is meant by ‘you are in if block’

  4. LearnCOnline says:

    The statement “you are in if Block” is just an indicator for your reference to indicate that the if condition evaluated to true and the control is inside the IF block.

  5. anuj pal says:

    plz tell me what is the problem in this program.it is not running.
    #include
    #include
    void main()
    {
    char ch;
    clrscr();
    printf(“enter the character”);
    scanf(“%ch”,&ch);
    if (ch==’a’||ch==’e’||ch==’i’||ch||’o’||ch==’u’)
    printf(“character is vowel”);
    else
    printf(“character is consonent”);
    getch();
    }

  6. Anonymous says:

    @anuj: try this code

    #include
    #include
    void main()
    {
    char ch;
    clrscr();
    printf(“enter the character”);
    scanf(“%c”,&ch);
    if (ch==’a’||ch==’e’||ch==’i’||ch==’o’||ch==’u’)
    printf(“character is vowel”);
    else
    printf(“character is consonent”);
    getch();
    }

  7. Anonymous says:

    i am learning C for the very first time and i don’t know where to programme C,I mean can i run the statements in DOS…please reply me as early as possible.plz plz plz

  8. K Mali says:

    No. you have to install turbo c on your computer.

  9. sipan says:

    please tell me this is fi else statement exap.. or if statement

  10. Anonymous says:

    y dint u include iostream.h and conio.h in anuj’s program?? and y just wrote #include??
    plz let me knw d reason

  11. LearnCOnline says:

    Hello… That is due to some error on this website… the page while rendering on the browser took as a tag and dint display it

  12. Anonymous says:

    Excellent place for a person who has no idea what programming is. Well done!

  13. Anonymous says:

    I love dis site

  14. Anonymous says:

    pls tel me wats is the use of scanf

  15. CSE blogger says:

    The use of scanf is to take the input from the programmer

  16. (ch== ‘a’ || ch== ‘e’ || ch== ‘o’|| ch== ‘u’)
    use space
    and#incude

  17. Awesssome work !!! (y)

  18. s00rav says:

    Give me reason, why its printing Equal…

    main()
    {
    int a=10;
    char b=10;
    if(a==b)
    printf(“Equal”);
    else
    printf(“not Equal”);
    }

  19. cb says:

    s00rav because u have given the same value in a and b try b=11 then it will not print equal

  20. sruthi says:

    this pgm is not working
    main()
    {
    char d;
    printf(“Enter the character”);
    scanf(“%c”,&d);
    if(d>=65&&d=97&&d=48&&d<=57)
    {
    printf("Digits");
    }
    else
    {
    printf("Special symbols");
    }
    }

  1. July 3, 2020

    […] if statement in C […]

  2. July 6, 2020

    […] if statement in C […]

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet