TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: C Decision Making: If, If-Else, Switch-Case
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
C Tutorials

C Decision Making: If, If-Else, Switch-Case

Last updated: Sep 12, 2023 2:25 am
By Meenakshi Agarwal
Share
5 Min Read
C Decision Making_ If, If-Else, Switch-Case
C Decision Making_ If, If-Else, Switch-Case
SHARE

In this C programming class, we’ll cover the C decision-making constructs such as C if, if-else, and the switch-case statement. It is also known as conditional programming in C.

Contents
C If Syntax:Program using C If Statement to Find the Greatest of Two Numbers.Flowchart:Algorithm:Code:C If…Else Syntax:Program using C If…Else to Find Whether a Number is Odd or Even.Flowchart:Algorithm:Code:C Switch…Case Syntax:Program using C Switch…Case to Calculate the Area of a Rectangle/Circle/Triangle.Flowchart:Algorithm:Code:

In your life, you encounter situations where you have to make a decision be it your favorite food dish or the color of your new car. In C programming also, you may encounter such situations where you need to make a decision.

The conditions in C language will help you. C primarily provides the following three types of conditional or decision-making construct.

Don’t miss out on how to write your first C program in just 2 minutes.

If Statement in C

The if statement helps you check a particular condition. If that condition is true, then a specific block (enclosed under the if) of code gets executed.

This flowchart will help you.

C If Statement Flowchart
C Decision Making – If Statement

C If Syntax:

if(condition either true or false)
{
    //Statement block
}

Now we will see a simple program using an if statement.

Program using C If Statement to Find the Greatest of Two Numbers.

Flowchart:

C Program to Find the greatest of two numbers
Find the greatest of two numbers

Algorithm:

Step 1: Start.

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

Step 3: If a is greater than b then go to step 4 otherwise go to step 5

Step 4: Print a greater than b

Step 5: Print b greater than a

Step 6: Stop.

Code:

#include<stdio.h>
#include<conio.h>

void main()
{
    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 Program Output

If…Else Statement in C

The if statement works pretty well, but if you want to work with more variables and more data, the if-else statement comes into play.

In the if statement, only one block of code executes after the condition is true.

But in the if-else statement, there are two blocks of code – one for handling the success and the other for the failure condition.

This flowchart will help you get it.

C If...Else Statement
C If…Else Decision Making Flowchart

C If…Else Syntax:

if(condition)
{
    //Statement block
}
else
{
    //Statement block
}

Program using C If…Else to Find Whether a Number is Odd or Even.

Flowchart:

C Program to Find whether a number is odd or even
Find whether a number is odd or even

Algorithm:

Step 1: Start.

Step 2: Take input from the user.

Step 3: Check condition. If remainder 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:

#include<stdio.h>
#include<conio.h>

void main()
{
    int a;

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

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

    getch();
}

The output should look something like this-

C If...Else Program Output

You can use multiple if-else statements which get called “nested if-else” statements. It is no different than the one above you can use various if-else statements in this order. Just keep in mind that the sequence should end with a final else statement, not an if statement.

Switch-Case Statement in C

When you have to execute multiple statements under one operation, Switch-case comes into play.

There are several cases under one switch statement.

C Switch…Case 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 user as input.

Program using C Switch…Case to Calculate the Area of a Rectangle/Circle/Triangle.

Flowchart:

C Program to Calculate the Area of a Rectangle/Circle/Triangle.
Calculate the area of a rectangle/circle/triangle.

Algorithm:

Step 1: Start

Step 2: Initialize variables

Step 3: Take input for choice and then for area variables from the user

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 according to case

Step 6: Stop

Code:

#include<stdio.h>
#include<conio.h>

void main()
{
    int ac,ar,at,r,a,b,choice;

    printf("Enter your choice\n”);
    prinft(“A for area of circle\n”);
    printf(“B for area of rectangle\n”);
    printf(“C for area of triangle\n");
    scanf("%c",&choice);

    switch(choice)
    {
    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 Switch...Case Program Output

You Might Also Like

C Programming Language “Struct”

20 C Programs for Beginners to Practice

Learn C Programming – How To Guide for Beginners

Python vs C++ – Is Python or C++ Better?

The Best Top-Down Approach Guide for You in C

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article What are different types of Operators in C Language? C Operators – Learn Step by Step
Next Article C While and Do-While Loops C While and Do-While Loops

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x