Join Our 4-Week Free Gen AI Course with select Programs.

Request a callback

or Chat with us on

Branching Statements in C: if, else, and switch with Examples

Basics of Python
Basics of Python
icon
5 Hrs. duration
icon
9 Modules
icon
1800+ Learners
logo
Start Learning

In C language, controlling the execution flow is essential to build interactive and responsive programs. This is achieved using branching statements. All the programs make decisions based on conditions. The primary branching statements in C are if, else and switch. This article will explore these statements in detail and look at examples of how they are used.

Branching Statements in C

 

Branching statements are statements in C language that help a programmer control the flow of program execution according to requirements. They are considered an essential aspect of Programming languages and are used to execute specific source code segments based on certain conditions.

Types of Branching Statements in C

 

The branching statements in C are categorised as follows:

 

  • Conditional Branching Statements in C

 

  • If Statement
  • else Statement
  • else-if Statement
  • switch Statement

 

  • Unconditional branching Statements in C

 

  • goto Statement
  • break Statement
  • continue Statement

Conditional Branching Statements in C

Conditional branching statements in C allow programmers to execute specific blocks of source code based on certain conditions. These statements enable program decision-making, making them dynamic and responsive to user input or other criteria. The following are different types of conditional branching statements in C

 

  • If Statement
  • else statement
  • else-if statement
  • switch statement

If Statement

 

This statement executes the specific block of source code if a certain condition is evaluated to be true.

Syntax of if Statement in C

if(condition){ // code to be executed if the condition specified evaluates is true }

Example of if Statement in C

Here is an example of demonstrating the if statement in action.

 

Program

#include <stdio.h> int main() { int x = 10; if (x > 5) { printf("x is greater than 5"); } return 0; }

Output

x is greater than 5

else Statement

This statement is used just the opposite of the if statement. This statement executes the source code if the condition specified in the if statement is evaluated as false.

 

Syntax of else Statement in C

if (condition) { // statements } else{ // code executed if condition is false }

Example of else Statement in C

#include <stdio.h> int main() { int x = 10; if (x < 5) { printf("x is less than 5n"); } else { printf("x is greater than or equal to 5n"); } return 0; }

Output

x is greater than or equal to 5

else if Statement

In C, else if is a control structure used to execute different blocks of code according to different conditions. It adds some more conditions so that if the initial condition does not hold, it checks again if your conditions are met to perform more complex decision-making paths inside your program.

 

Syntax of else-if Statement in C

if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true and condition1 is false }

Example of else-if Statement in C

#include <stdio.h> int main() { int x = 10; if (x > 15) { printf("x is greater than 15"); } else if (x > 5) { printf("x is greater than 5 but less than or equal to 15"); } else { printf("x is less than or equal to 5"); } return 0; }

Output

x is greater than 5 but less than or equal to 15

switch Statement

The switch statement in C is used when we have multiple conditions to check. It is often used as an alternative to multiple “if” and “else if statements”.

 

Syntax of switch Statement in C

switch (expression) { case value1: // code to be executed if expression equals value1 break; case value2: // code to be executed if expression equals value2 break; ... default: // code to be executed if none of the cases match break; }

Example of Switch Statement in C

#include <stdio.h> int main() { int x = 4; switch (x) { case 1: printf("x is 1"); break; case 2: printf("x is 2"); break; case 3: printf("x is 3"); break; default: printf("x is not 1, 2, or 3"); break; } return 0; }

Output

x is 4
DevOps & Cloud Engineering
Internship Assurance
DevOps & Cloud Engineering

Unconditional Branching Statements in C

In C, unconditional branching statements change the normal flow of program execution. These statements allow programmers to jump to a specific point in their source code regardless of any condition.

 

There are following types of unconditional branching statements in C language.

 

  • goto Statement
  • break Statement
  • continue Statement

 

goto Statement

 

The C goto statement in C is the control flow statement by which you may jump to a predefined label within the same function. It is not that generally discouraged in favor of more structured control flow statements like loops and conditionals.

 

Syntax of goto Statement in C

goto label; ... label: // code to be executed

Example of goto Statement in C

#include <stdio.h> int main() { int x = 0; start: x++; if (x < 20) { goto start; } printf("x is %d", x); return 0; }

Output 

x is 20

break Statement

 

C provides a control flow statement called the break statement that breaks out of a loop or a switch statement from its normal course of execution. At any point when a break statement is encountered, it takes away control to the next statement after the loop or switch.

 

Syntax of break Statement in C

 

break;

 

Example of break Statement in C

#include <stdio.h> int main() { int x = 0; while (x < 10) { x++; if (x == 9) { break; } } printf("x is %d", x); return 0; }

Output

x  is 10

continue Statement

 

The continue statement is used in loops, such as for, while and do-while loops. The continue statements are encountered, the remaining source code in the current iteration is skipped, and the loop proceeds with the next iteration.

 

Syntax of continue Statement in C

 

continue;

 

Example of continue Statement in C

#include <stdio.h> int main() { int i; for (i = 0; i < 10; i++) { if (i % 2 == 0) { continue; } printf("%d ", i); } return 0; }

Output

1 3 5 7 9

Conclusion

Conditional statements in ‘C’ like if, else, and switch are the most important control statements that guide the execution of programs at the time of flow control.m. The developers can incorporate decision-making abilities within the statements, which makes the programme flexible enough to adapt to various conditions and input. Through the proper use of these branching statements, programmers can design more dynamic and robust applications. Knowledge about these control structures can be considered the core knowledge to be acquired while learning a C language and developing the correct algorithms because of various conditions and situations encountered during computation.

FAQs
Yes, we can nest if and switch statements inside each other to handle complex decision-making scenarios.
Yes, But it is not recommended. An empty else statement doesn’t do anything and may confuse others reading the source code, leading to maintenance issues.
Switch statements can only check the equality of expressions against constant values. If you need to check complex conditions or ranges, an if-else statement is required.
The default case is executed when none of the other cases matches the value of the switch expression. It is optional, but highly recommended to handle the unexpected.
Yes, we can have an if statement without accompanying anything else. If the if condition is false, the program simply skips the source code block inside the if.

Deploying Applications Over the Cloud Using Jenkins

Prashant Kumar Dey

Prashant Kumar Dey

Associate Program Director - Hero Vired

Ex BMW | Google

19 October, 12:00 PM (IST)

Limited Seats Left

Book a Free Live Class

left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.

Data Science

Accelerator Program in Business Analytics & Data Science

Integrated Program in Data Science, AI and ML

Accelerator Program in AI and Machine Learning

Advanced Certification Program in Data Science & Analytics

Technology

Certificate Program in Full Stack Development with Specialization for Web and Mobile

Certificate Program in DevOps and Cloud Engineering

Certificate Program in Application Development

Certificate Program in Cybersecurity Essentials & Risk Assessment

Finance

Integrated Program in Finance and Financial Technologies

Certificate Program in Financial Analysis, Valuation and Risk Management

Management

Certificate Program in Strategic Management and Business Essentials

Executive Program in Product Management

Certificate Program in Product Management

Certificate Program in Technology-enabled Sales

Future Tech

Certificate Program in Gaming & Esports

Certificate Program in Extended Reality (VR+AR)

Professional Diploma in UX Design

Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

© 2024 Hero Vired. All rights reserved