Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, March 17, 2020

If Statement in C

Simple If statement: When we need to execute a group of statements only when a given condition is true then we use if statement.

Simple if statement | welcome2protec.com


When we need to execute a group of statements only when a given condition is true then we use if statement.

If statement allows us to select an action based on some condition. It gives programmer to take control over a piece of code. Programmer can control the execution of code based on some condition or user input. For example: if a user inputs valid user name and password to login to his laptop, then will be able to login else not.



Boolean Expression:

If statement perform action based on Boolean expression either true or false.

A C expression that evaluates either true or false is known as Boolean expression. However, in C programming there is no concept of true or false value.

In C we represent true with a non-zero integer and false with zero. Hence, if an expression evaluates to integer is considered as Boolean expression.

Syntax of  simple if statement:

if (condition)
{
     //Group of C statements.
     //These statements will only execute if the condition is true.
}

How if statement works?

  • The if statement check the given condition inside the parenthesis().
  • If the condition returns true, statements inside the body of ''if'' are executed. otherwise, the condition returns false then the statements inside “if” are skipped.

For Example:

Simple if | welcome2protec.com
Simple if | welcome2protec.com

Note: Relational and logical operators are used to compare things and take action based on the comparison.


Flow chart of If statement:

Flow chart of simple if | welcome2protec.com
Flow chart of simple if | welcome2protec.com

Example 1: Simple if statement

// Program to display a number if it is negative
#include <stdio.h>
int main() {
    
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // if number is less than 0 then number is negative
    if (number < 0) {
        printf("You entered %d, i.e. a negative number.\n", number);
    }

    printf("You entered %d, i.e. a positive number.\n", number);
    return 0;
}

Output 1:

Enter an integer: -2
You entered -2, i.e. a negative number.

In output-1, when the user enter -2, then the condition if (number < 0) evaluated to true. Hence, you entered -2 is displayed on the screen.

Output 2:

Enter an integer: 5
You entered 5, i.e. a positive number.

In output-2, when the user enter 5, then the condition if (number < 0) evaluated to false. Then the statement inside if is skipped, and will display whatever statement written outside of if.


Try some more exercises on if statement.

Multiple if statement:

We can use multiple if statements to check more than one conditions. Let's have a look at the below example.

Example 2: Multiple if statement

/* Program to find the largest number among the three. */ 
#include <stdio.h>   
int main()  
{  
    int a, b, c;   
    printf("Enter three numbers?");  
    scanf("%d %d %d",&a,&b,&c);  
    if(a>b && a>c)  
    {  
        printf("%d is largest", a);  
    }  
    if(b>a  && b > c)  
    {  
        printf("%d is largest", b);  
    }  
    if(c>a && c>b)  
    {  
        printf("%d is largest", c);  
    }  
    if(a == b && a == c)   
    {  
        printf("All are equal");   
    }
    printf("End of program");
    return 0;
}  

Output

/*The output depends on the user input.*/
  Enter three numbers: 10
  20 
  30
  30 is largest.











Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us