Javatpoint Logo
Javatpoint Logo

Branching statements in C

The capacity to decide what to do and how to run distinct pieces of code based on specific circumstances is essential in the realm of programming. This ability to manage the execution flow is provided by branching statements in the C programming language. We'll examine the different branching statements in C in this blog article, going through their syntax, giving examples, and showcasing the results they produce.

Branching statements are mainly categorized as follows.

  1. Conditional Branching Statements
  2. Unconditional Branching Statements

Conditional Branching Statements:

In C, conditional branching statements are used to execute particular code blocks based on a condition (as needed). These branching instructions in C allow programmers to run the code only when specific conditions are met. The various categories of conditional branching statements in C are as follows:

  • if Statement
  • if-else Statement
  • nested if-else Statement
  • switch Statement

The if Statement:

The most fundamental branching construct in C is the "if" statement. If a given condition is met, it permits the execution of a block of code. The "if" statement has the following syntax:

Example:

Output:

The number is positive.

The if-else Statement:

By offering a different block of code to run when the condition is false, the "if-else" statement builds on the "if" statement. The "if-else" expression has the following syntax:

Example:

Output:

The number is non-positive.

The nested if-else Statement:

You can have an "if-else" statement within another "if" or "else" block in C since it supports the nesting of "if-else" statements. It allows for adaptability when managing various environments. The nested "if-else" statement has the following syntax:

Example:

Output:

The number is zero.

The switch Statement:

Multiple actions can be taken using the "switch" statement based on the value of a variable or expression. When managing several instances, it is especially helpful. The "switch" statement has the following syntax:

Example:

Output:

Wednesday

Unconditional Branching Statements:

C uses unconditional branching statements to alter the typical course of program execution. These statements give programmers the freedom to jump to a specific location in their code under any circumstance. The various categories of unconditional branching statements in C are as follows:

  • goto Statement
  • break Statement
  • continue Statement

goto Statement:

The "goto" command enables programmers to leap directly to a designated labelled statement within their code. It is an unconditional branching statement.

Syntax of goto Statement in C:

The "goto" statement executes the code in the block following a jump to the "label"-labeled statement. Any legal identifier can be used as the "label" after a colon (:).

Example:

It is an illustration of a goto statement.

Output:

The value of num is 1.

Explanation:

In this example, the 'goto' statement switches control to the labelled statement 'label', skipping the succeeding printf statement, when the condition num == 1 is true. Only the line "The value of num is 1" is printed consequently

The 'break' Statement:

The "break" statement is frequently employed in switch statements as well as looping constructions like "for", "while", and "do-while". It enables you to skip the statement that follows the loop or switch and end the execution of the closest enclosing loop or switch statement.

The "break" statement in C is written in the following syntax.

Example:

Look at the following illustration of the 'break' statement in use within a loop:

Output:

1 2

Explanation:

In this illustration, the 'for' loop iterates from 1 to 5. However, the 'break' statement is encountered when i equals 3, ending the loop early. Only the numerals 1 and 2 are printed as a result.

continue Statement

In the C programming language, the continue statement is used to go to the next iteration of a loop while skipping the current iteration. Usually, this statement is used in loops like for or while.

Syntax of continue statement in C is as follows:

When a loop encounters the continue statement, the current iteration of the loop is ended, and control is returned to the top of the loop to begin the subsequent iteration.

Example:

This sample demonstrates how the C continues Statement functions.

Output:

1 3 5 7 9

Explanation:

In the above example, the for loop iterates from 0 to 9, and when the loop variable i is even, the continue statement is executed and control is transferred to the following iteration of the loop. As a result, the printf statement only prints odd integers and skips over even values.

Advantages and Disadvantages of Branching Statements

There are various advantages and disadvantages of the branching statements. Some main advantages and disadvantages of the branching statements are as follows:

Advantages of Branching Statements:

Better Decision Making: Branching statements give programmers the ability to decide what to do next in their code depending on certain circumstances. It improves the program's functionality and versatility by allowing it to respond and adapt to various conditions.

Readability of the code: Branching statements make the logic of the code clearer and simpler to understand. For other developers to understand and maintain the codebase, the conditional statements provide distinct signs of the program's decision-making process.

Code effectiveness: Branching statements optimize program execution by only running the necessary code blocks in accordance with the predetermined circumstances. It can make programs run more quickly and effectively, especially when employing the "switch" statement to process big sets of cases or complex decision trees.

Flexibility: Programming flexibility is provided through branching statements, which enables several code paths to be performed in response to diverse circumstances. This adaptability is especially helpful when the program needs to react quickly to altering inputs or outside influences.

Code Reusability: Branching statements make it easier to reuse code by enabling the execution of code blocks in response to various situations. Programmers can create modular code that can be easily reused in various portions of the program by utilizing branching statements efficiently.

Disadvantages of Branching Statements:

Code Complexity: When branching statements are overused or deeply nested, the code may become clearer and easier to comprehend. It may result in maintenance problems, defects, and poorer-quality code. It's crucial to utilize branching statements sparingly and aim for simplicity.

Readability Issues: Although branching statements can sometimes make code easier to comprehend, they can also cause issues if used carelessly. The code may become more difficult to read and understand if there are too many nested levels, complex conditional expressions, or duplicative sections of code.

Potential for Logical Mistakes: If the conditions are not adequately established and validated, branching statements run the danger of logical mistakes.

Code Maintenance: The intricacy of branching statements makes it more difficult to maintain the code. It can be difficult to comprehend the logic of the code and make the necessary adjustments without adding problems when there are numerous branching statements and nested conditions present.

Let's look at a few more instances to see how branching statements are used in various contexts:

Example 1: Finding the Maximum Number

Output:

The maximum number is: 20

Explanation:

In this illustration, the maximum value is established by comparing two numbers (num1 and num2) using the "if-else" expression. The program sets the variable max to its maximum value before displaying it on the terminal.

Example 2: Grade Classification

Output (for input 78):

Grade: C

Explanation:

In this illustration, the program accepts the user's marks as input and determines the grade level using the "if-else if" ladder. The program prints the corresponding grade on the console based on the input marks.

Example 3: Month Name

Output (for input 8):

August

Explanation:

In this illustration, the computer receives the user's input as the month number and then utilizes the "switch" statement to get the name of the matching month. The program prints the corresponding month name on the console based on the input.

Example 4: Leap Year Check

Output (for input 2024):

2024 is a leap year.

Explanation:

In the above example, the for loop iterates from 0 to 9, and when the loop variable i is even, the continue statement is executed and control is transferred to the following iteration of the loop. As a result, the printf statement only prints odd integers and skips over even values.

Conclusion:

Programmers often use branching statements because they give them the power to modify how code is executed based on certain criteria. In this blog post, we focused on the "if", "if-else", nested "if-else", and "switch" expressions to examine the fundamental branching statements in the C programming language.

The "if-else" statement expands on the "if" statement's ability to execute code conditionally by offering an additional code block for when the condition is false. Decision-making is more flexible because numerous criteria may be handled within nested blocks due to the nested "if-else" statement. Finally, the "switch" statement simplifies managing many situations by enabling alternative actions to be carried out depending on the value of a variable or expression.

Learning how to use these branching statements gives programmers more control over how their programs run, allowing them to create applications that are more intelligent and dynamic. It is crucial to experiment with various conditions and scenarios to gain a deeper grasp of and increase one's expertise with branching statements efficiently.

As you advance in your understanding of C programming, keep in mind that branching statements are effective tools that improve the flexibility and logic of the code. With time and practice, you will develop the abilities needed to make the best decisions and build solid, effective programs.


Next TopicBubble Sort in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA