Saturday, June 12, 2021

If-else and switch statement in C

 If-else and switch statement in C



Here we will discuss
  • If-else statement
  • Relational Operator
  • Logical operator
  • Ternary Statement
  • Switch statement
Sometimes we have to program a code with conditions.
Decision-making instruction is as follows:
  1. an if-else statement
  2. Switch statement

If-else Statement

If else statement is used to give conditions for programming.
Syntax of an if-else statement is:
if( /*condition*/)
{
    /*code*/
}
else
{
    /*code*/
}

 For example:-

Write a code to state whether a number is odd or even.

Answer: To write this code we will use an if-else statement and modular operator.

#include<stdio.h>


int main(){

int a;

printf("Enter a number\n");

scanf("%d",&a);


if (a%2==0){

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

}

else

{

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

}

return 0;

}

Its output will be

The output of the program tells us whether a number is odd or even


 Operators used for Decision making instructions are as follows:

  1. Relational Operator
  2. Logical Operator

Relational Operator

The relational operator is used to check the condition whether it is true or false in an if-else statement.

Expression

meaning

==

Equal to

!=

Not equal to

> 

Greater than

< 

Less than

>=

Greater than and equal to

<=

Less than and equal to

 

Note: = is an assignment operator where as == is used to check equality

Example: sum=a+b;

                  If(sum==4){

                                Printf(“Sum of two number is 4”);

                    }

Logical Operator

A logical operator is used to assigning more conditions in an if-else statement.

There are three logical operators

Expression

Name

&&

AND

||

OR

!

NOT

 

&& operator: AND operator states that when both conditions will be true then only statement executes.

|| operator: OR operator states that when anyone condition will be true or both conditions will true then the statement should execute.

! operator: NOT operator states when both conditions will be false then the only statement executes.

 

If we have to execute multiple statements with the condition then we can use if-else if-else statement.

Syntax for using if-else if-else statement is as follows:

If(/*condition*/)

{

               /*statement*/

}

else if(/*condition*/)

{

               /*statement*/

}

else (/*condition*/)

{

               /*statement*/

}

 

For example

Write a program to show whether a number is divisible with 2, 3, and 4.

Answer :   

#include<stdio.h>


int main(){

    int a;

    printf("Enter the value :\n");

    scanf("%d",&a);

    if(a%2==0){

        printf("%d is divisible by 2",a);

    }

    else if (a%3==0)

    {

        printf("%d is divisible by 3",a);

    }

    else

    {

        printf("%d is not divisible by 2 and 3",a);

    }

    

return 0;

}

Its Output is

The output of if-else if statements example


Note: It is not necessary to put else statement. If-else statement works without else statement too.

Now we have studied all types of operators. So, the hierarchy of operator in C is as follows:

Priority

Operator

First

!

Second

*, /, %

Third

+, -

Forth

<, >, <=, >=

Fifth

==, !=

Sixth

&&

Seventh

||

eighth

=

 
Conditional operator or Ternary operator:

? and : are the conditional operator which is also sometimes known as the ternary operator.

Syntax of the ternary operator is as follows:

(expression 1? expression 2: expression 3)

For example:

Write a program to show which one is greater between two number?

#include <stdio.h>

int main()

{

    int a, b, c;

    printf("Enter the value of a : \n");

    scanf("%d", &a);

    printf("Enter the value of b : \n");

    scanf("%d", &b);

    c= (a<b) ? a:b;

    printf("%d is greater",c);

    return 0;

}

Its output is

The output of the Conditional statement

 

 Switch Statement

Switch case-control instruction is used to give the number of alternatives or choice to make a decision. It gives us more than one alternative to choose in which method we have to work.
Syntax of Switch-case statement is as follow:
    switch (expression)
    {
    case /* constant-expression */:
        /* code */
        break;
        case /* constant-expression */:
        /* code */
        break;
    default:
        break;
    }

Note: 

  • /*constant-expression*/: Here we write the constant which we choose in execution. 
  • /*code*/: Here we write valid which execute.
  • break: break statement is used to terminate the process.
For example:
Write a program to make a calculator using a switch-case statement?

#include<stdio.h>

int main(){
char operator;
float a,b;
printf("Operator is \n ");
scanf("%c",&operator);
printf("Enter value of a \n");
scanf("%f",&a);
printf("Enter value of b\n ");
scanf("%f",&b);
switch(operator){
    case '+':
    printf("%f+%f=%f",a,b,a+b);
    break;
    case '-':
    printf("%f-%f=%f",a,b,a-b);
    break;
    case '*':
    printf("%f*%f=%f",a,b,a*b);
    break;
    case '/':
    printf("%f/%f=%f",a,b,a/b);
    break;
}
return 0;
}

Its output is

The output of the switch-calculator

No comments:

Post a Comment

Please do not enter any spam link in the comment box

Best of Our Website

Popular Posts