FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
561 views
in Tutorial & Interview questions by Goeduhub's Expert (9.3k points)

1 Answer

0 like 0 dislike
by Goeduhub's Expert (9.3k points)
 
Best answer

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

C programming language provides the following types of decision making statements.

  1. If statement
  2. If-else statement
  3. If else-if ladder
  4. Nested if

If statement

 The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false then the statements inside “if” are skipped.


if(expression){  

//code to be executed  

}  


Example of C language if statement-

#include <stdio.h>
int main()
{
    int x = 40;
    int y = 52;
    if (x<y)
    {
        printf("Variable x is less than y");
    }
    return 0;
}

Output-

Variable x is less than y

The condition (x<y) specified in the “if” returns true for the value of x and y, so the statement inside the body of if is executed.

Multiple if statement

Program to find the largest number of 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");   

    }  

}  


Output-

Enter three numbers

34 65 23

65 is largest

If-else statement

If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.

Syntax-

if(expression){  

//code to be executed if condition is true  

}else{  

//code to be executed if condition is false  

Example-Check whether number is even or odd.


#include<stdio.h>    

int main(){    

int number=0;    

printf("enter a number:");    

scanf("%d",&number);     

if(number%2==0){    

printf("%d is even number",number);    

}    

else{    

printf("%d is odd number",number);    

}     

return 0;  


If else-if ladder Statement

   The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement.

Syntax

if(condition1){  

//code to be executed if condition1 is true  

}else if(condition2){  

//code to be executed if condition2 is true  

}  

else if(condition3){  

//code to be executed if condition3 is true  

}  

...  

else{  

//code to be executed if all the conditions are false  

}  

Program to calculate the grade of the student according to the specified marks.

#include <stdio.h>  

int main()  

{  

    int marks;   

    printf("Enter your marks?");  

    scanf("%d",&marks);   

    if(marks > 85 && marks <= 100)  

    {  

        printf("Congrats ! you scored grade A ...");   

    }  

    else if (marks > 60 && marks <= 85)   

    {  

        printf("You scored grade B + ...");  

    }  

    else if (marks > 40 && marks <= 60)   

    {  

        printf("You scored grade B ...");  

    }  

    else if (marks > 30 && marks <= 40)   

    {  

        printf("You scored grade C ...");   

    }  

    else   

    {  

        printf("Sorry you are fail ...");   

    }  

}  

Nested If..else statement

When an if else statement is present inside the body of another “if” or “else” then this is called nested if else. 

Example-

#include <stdio.h>

int main () {

   /* local variable definition */
   int a = 1000;
   int b = 2000;

   /* check the boolean condition */
   if( a == 1000 ) {

      /* if condition is true then check the following */
      if( b == 2000) {
         /* if condition is true then print the following */
         printf("Value of a is 100 and b is 200\n" );
      }
   }

   printf("Exact value of a is : %d\n", a );
   printf("Exact value of b is : %d\n", b );

   return 0;
}

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

Related questions

0 like 0 dislike
1 answer 617 views
0 like 0 dislike
1 answer 776 views
0 like 0 dislike
1 answer 626 views
0 like 0 dislike
1 answer 498 views
0 like 0 dislike
0 answers 316 views

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...