Online Courses
Free Tutorials  Go to Your University  Placement Preparation 
Goeduhub's Online Courses @ Udemy in Just INR 570/-
Online Training - Youtube Live Class Link
0 like 0 dislike
187 views
in Tutorial & Interview questions by Goeduhub's Expert (8.3k points)

Goeduhub's Top Online Courses @Udemy

For Indian Students- INR 360/- || For International Students- $9.99/-

S.No.

Course Name

 Coupon

1.

Tensorflow 2 & Keras:Deep Learning & Artificial Intelligence

Apply Coupon

2.

Natural Language Processing-NLP with Deep Learning in Python Apply Coupon

3.

Computer Vision OpenCV Python | YOLO| Deep Learning in Colab Apply Coupon
    More Courses

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.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;
}

3.3k questions

7.1k answers

394 comments

4.6k users

Related questions

0 like 0 dislike
1 answer 197 views
0 like 0 dislike
1 answer 156 views
0 like 0 dislike
1 answer 210 views
0 like 0 dislike
1 answer 167 views
0 like 0 dislike
0 answers 134 views

 Goeduhub:

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