If block

If statement in C programming language

If statements in C programming language

In this tutorial, we will discuss If statement in C programming language

if …else…statement

Three type of if statements are mainly available in Java:

  • if statements
  • if else statements
  • if else if else statements
If statements in C language

if statements

Declaration

Syntax

if(Test_condition){
statement(s)
}  //this statements are false control flow jump out of if part

}

Flow diagram – if in C language

Flowchart of if statement in C language

In the C programming language, first test the Boolean expression inside the parenthesis of if part, when the Boolean expression is true, flow of control enters the codes inside the  body of if part and executes body part statement, when the Boolean expression is false, flow loop control skips the body of if  part and comes out loop control.

Example

 

How  if statement works in C

the flow of control of if statement in C

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int marks=66;
    if(marks>=65)
    {
        printf("Happy you pass exam\n");
    }
    printf("End of loop\n");
    getch();
    return 0;
}

In the above program, initially, marks are declared 66.

the test expression is marks>=65

When the test expression is evaluated, test expression becomes true. So the body of if part statement is executed and display “Happy you pass exam” .

Then the loop control exit from if part and executes out of if statements

 

When the above code is compiled and executed, it produces the following results

Happy you pass exam
End of loop

In the above program, initially marks is declared  66. when the mark is 66, the test expression (marks>=65) is evaluated and it returns true, the code inside the body of if statements executed.

Now, when we change the value of marks to below 65, the output of in this case will be executed

End of loop

In the example, when the mark is 60, the test expression (marks>=65) is evaluated  and returns false  hence the flow of control skips the execution of the body of if statements and flow of control comes out of if body for execution

In the above program statements of the end of the loop is always executed.

 

if else in C language

 

Declaration

Syntax

if(Test_condition){
statement(s)
}
else{
statement(s)

}

Flow diagram – if else in C language

 

Flow diagram if -else

In the C programming language, first evaluates the test expression inside the parenthesis of if part, when the test expression is true, the flow of control enters the codes inside the body of if part and executes body part statement. When the test expression is false, loop control skips the body of if part and goes to else part and execute its statements.

How  if -else statements works in C

flow of control of if else

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Enter your age...");
    scanf("%d",&age);
    if(age<=18)
        {
            printf("your age is %d you are a younger\n",age);
        }
        else{
                printf("your age is overage",age);
                }

   getch();
    return 0;
}

 

When the above code is compiled and executed, it produces the following results.

Enter your age .... 13
your age is 14 you area younger

 

if …else if.. else…statement

Declaration

Syntax

if(Test_condition){ 
statement(s) 
}
else if{ 
statement(s) 

}

 else{ 
statement(s) 

}

In the C programming language, first evaluates the test expression inside the parenthesis of if part, when the test expression is true, the flow of control enters the body of if part and executes body part statement. When the test expression is false, loop control skips the codes inside the body of if part and goes to else if the part to execute its statements.

When the test expression is evaluated(else if) to be false, else part executes and exits from the loop for rest.

Flow chart of if else if else in C language

flow diagram if-else-if else i n C

How  if -else statements works in C

the flow of control of if- else if -else in C

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    printf("Enter your age here");
    scanf("%d",&age);
    if(age<=13)
        {
            printf("your age a chuld\n");
        }
        else if(age<35){
                printf("your age is younger");
                }
        else{
                printf("your age is older");
                }

   getch();
    return 0;
}

 

When the above code compiled and executed, it produced the following results

Enter your age Here

45

you are an older

 

Suggested for you

 Nested if statements in C language

 Nested if statements in Java language

 Nested if statements in C++ language

 Nested if statements in Python language

if statement in Python programming language
if statement in CPP programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago