C Decision Making: If, If-Else, Switch-Case Statements – MPS


In this C programming class, we’ll cowl the C resolution making constructs such as C if, if-else, and the switch-case assertion. It is additionally identified as the conditional programming in C.

C Decision Making Statements

In your life, you encounter conditions the place you need to decide be it your favourite meals dish or the colour of your new automotive. In C programming additionally, chances are you’ll encounter such conditions the place it’s essential decide.

The situations in C language will aid you.

C primarily gives the next three forms of conditional or resolution-making assemble.

  1. if statement
  2. if-else statement
  3. switch case

1. if assertion in C Programs

The if assertion facilitates to test a selected situation. If that situation is true, then a selected block (enclosed beneath the if) of code will get executed.

This flowchart will aid you.

C Decision Making - If Statement
C Decision Making – If Statement

Now we’ll see a easy program utilizing if assertion.

Program 1: Program to seek out the higher quantity between two numbers.

Flowchart:

C Decision Making - If Example
C Decision Making – If Example

Algorithm:

Step 1: Start.

Step 2: Take two inputs (a and b) from the person.

Step 3: If a is higher than b then go to step 4 in any other case go to step 5

Step 4: Print a higher than b

Step 5: Print b higher than a

Step 6: Stop.

Code:

#embrace<stdio.h>
#embrace<conio.h>

void important()

    int a,b;

    printf("Enter two numbers :");
    scanf("%d %d",&a,&b);

    if (a>b)
        printf("%d is greater",a);

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

    getch();

The output is as follows:

C Decision Making - If Output

2. if-else Statement

The if assertion works fairly good, however if you need to work with extra variables and extra knowledge, the if-else assertion comes into play.

In the if assertion, just one block of code executes after the situation is true.

But in the if-else assertion, there are two blocks of code – one for dealing with the success and different for the failure situation.

This flowchart will aid you get it.

C Decision Making - If...Else Statement
C Decision Making – If…Else Statement

Syntax:

if(situation)

    //Statement block

else

    //Statement block

Program 2: Program to seek out whether or not a quantity is odd or even.

Flowchart:

C Decision Making - If Else Example
C Decision Making – If Else Example

Algorithm:

Step 1: Start.

Step 2: Take enter from the person.

Step 3: Check situation. If the rest is zero go to step 4 else go to step 5

Step 4: Print a is even and go to step 6

Step 5: Print a is odd

Step 6: Stop

Code:

#embrace<stdio.h>
#embrace<conio.h>

void important()

    int a;

    printf("Enter a number :");
    scanf("%d",&a);

    if (apercent2==0)
    
        printf("%d is even",a);
    
    else
        printf("%d is odd",a);

    getch();

The output ought to look one thing like this-

C Decision Making - If...Else Output

You can use a number of if-else statements which get known as “nested if-else” statements. It is no completely different than the one above you should use varied if-else statements in this order. Just preserve in thoughts that the sequence ought to finish with a final else assertion, not if assertion.

3. Switch-Case Statement

When you need to execute a number of statements beneath one operation, Switch-case comes into play.

There are a number of instances beneath one switch assertion.

Syntax:

switch(variable)

    case n1:
        //Statement block;
        break;
    case n2:
        //Statement block;
        break;
    .
    .
    .
    case n:
        //Statement block;
        break;

Here the variable is taken from the person as enter.

Program 3: Program to calculate the realm of a rectangle or circle or triangle by taking the person’s selection.

Flowchart:

C Decision Making - Switch...Case Example
C Decision Making – Switch…Case Example

Algorithm:

Step 1: Start

Step 2: Initialize variables

Step 3: Take enter for selection and then for space variables from the person

Step 4: Case 1: Circle: 3.14*3.14*r

Case 2: Rectangle: ar=a*b

Case 3: Triangle: at=0.5*a*b

Step 5: Display output in accordance with case

Step 6: Stop

Code:

#embrace<stdio.h>
#embrace<conio.h>

void important()

    int ac,ar,at,r,a,b,selection;

    printf("Enter your choicen”);
    prinft(“A for area of circlen”);
    printf(“B for area of rectanglen”);
    printf(“C for space of trianglen");
    scanf("%c",&selection);

    switch(selection)
    
    case A:
        printf("Enter radius: ");
        scanf("%d",&r);
        ac=3.14*3.14*r;
        printf("Area of circle is: %d",ac);
        break;
    case B:
        printf("Enter length and breadth:");
        scanf("%d %d",&a,&b);
        ar=a*b;
        printf("Area of rectangle is: %d",ar);
        break;
    case C:
        printf("Enter base and height: ");
        scanf("%d %d",&a,&b);
        at=0.5*a*b;
        printf("Area of triangle is: %d",at);
        break;
    

    getch();

Output:

C Decision Making - Switch...Case Output

Python  Recommended Post

Source hyperlink

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.