Conditional Statements in C

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:05:00

What are Conditional Statements in C?

Conditional statements are often called decision-making statements. Conditional statements in C are used to execute different sections of code based on particular conditions. These conditions are provided by a series of decision-making statements with Boolean expressions. Boolean expressions are evaluated as true or false.

Why use conditional statements?

  • The method by which a program is executed is dynamically controlled.
  • Efficient in error and exception handling.
  • Check the flow of your program.
  • Helps you manage the status of your program.
  • Helpful for conducting difficult calculations.

Types of Conditional Statements in C

  • If Statement
  • If-Else Statement
  • If-Else-If or Ladder Statement
  • Switch Statement

If Statement

An if statement contains a Boolean expression that is evaluated to a Boolean value.​ If the value is True, the block executes; otherwise, the next statement is executed.

How does the 'if' statement work in C?

  • Use 'if' followed by a condition in parentheses.
  • Condition is similar to a query the program asks itself.
  • If the condition is true, the code inside {} executes.
  • If false, the code inside {} will be skipped.
  • The program continues following the 'if' block.

Advantages of an If Statement

  • Execute code based on the true/false condition.
  • Allows for flexible program flow.
  • Critical for error handling and validation.
  • Scalable and flexible to program complexity.

Disadvantages of If Statement

  • Nested 'if' statements can limit readability.
  • There is a risk of code duplication when using many comparable 'if' expressions.

If-Else Statement

An if-else statement consists of two statements: the if statement and the else statement. When an expression in an if-statement is evaluated as true, the if block is executed; otherwise, the else block is run.

Working of the if-else statement

  • When the program control arrives at the if-else block for the first time, it checks the test condition.
  • The block is executed if the test condition in the provided code is true.
  • If the test condition in the provided code is false, then the else block is run.
  • After that, the code control moves to the statements below the if-else line.

Advantages of if-else statement

  • The if-else statement allows the developer to make code decisions and run the correct code.
  • Developers can also use it to debug code.

Disadvantages of if-else statement

  • It multiplies the amount of code paths to be tested.
  • If there are too many if statements in the code, it can become unreadable.

If-Else-If statement or ladder 

It makes use of several if-else if-else statements to test a set of conditions. The statement that meets the outcome will be performed. If no condition matches the result, the default else statement will be executed.

How to use If-Else If Statements in C?

  • The If Statement: This stage contains its first condition. If the condition in the first step is true, the code block will be executed.
  • The Else If Statement: If the condition is false in the second stage, the program flow will go to the Else if statement, where the condition will be checked again. If the condition in the else if statement is true, the block of code inside it will be executed straight.
  • The Else Statement: If both of the conditions stated in the if and else if statements are true, the code block inside the Else statement is executed directly.

Advantages If-Else-If Statements in C

  • Multiple Decision-Making: It enables us to manage several conditions in an organized manner.
  • Efficient: Using if else if allows the program to bypass the operation of certain conditions whenever a true condition is found, which increases code performance, especially when there are multiple conditions.
  • Readable: if-else statements simplify and make the code easier to understand.

Disadvantages of If-Else-If Statements in C

  • Complex in nature: As the number of conditions increases, the if-else-if ladder becomes more complex and difficult to understand.
  • High Maintenance: Adding or updating many conditions necessitates careful study to ensure that the logic stays right and that all conceivable scenarios are taken into account, which may raise maintenance.
  • More Execution: Each condition in an if-else statement must be processed sequentially until a true condition is discovered. In short, more conditions equals greater execution.

Switch Statement

A switch statement tests a set of conditions using one or more cases that are compared to the switch expression. The case that meets the requirements would be executed. When no cases match the result, the default case is executed.

Rules for the switch statement in C

  • The switch expression's data type must be a character or integer.
  • Each case label must be followed by a colon ":".
  • The case value should be an integer or a character constant.
  • The break keyword is optional, although it can help your program run faster. After a match is found, it stops executing other cases.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this