Category Archives: C Decision Making Statements

C If Else If Ladder Statement

The c if else if ladder statement is an extension of the if-else statement.

The if-else if ladder statement allows us to add an alternative set of test conditions in the if-else statement using the else-if statement.

if-else if ladder statement Syntax

Syntax:-

 

C Nested Switch Case Statement

C Nested Switch Case Statement

In C programming, when there is a switch case statement inside another switch case statement then it is known as a nested switch case statement.

It is possible to have a switch case statement as a part of another switch case statement.

Nested Switch Case Statement Syntax

Below is the general syntax of nested switch case statement in c programming:

Syntax:-

Nested Switch Case Statement Example

Below is a simple example to demonstrate the use of nested switch case statement in c programming:

Example:-

When we run the above C program, will see the following output –

Output:-

c-nested-switch-case-statement

C Switch Case Statement

In C programming, It evaluates an expression against multiple cases in order to identify the block of code to be executed. A switch case statement is a simplified form of the Nested if-else statement, it helps to avoid a long chain of if..else if..else statements.

Flow Diagram

c-switch-case-statement-flowchart

C Switch Case Statement Syntax

Below is the general syntax of switch case in c programming:

Syntax:-

Example of Switch Case

Below is a simple example to demonstrate the use of the present statement in c programming:

Example:-

In the above program, we have an integer variable dayOfWeek with an initial value of 5, and we pass this variable as an expression to the following switch statement, value of dayOfWeek is tested with multiple cases to identify the block of code to be executed. When we run the above C program, will see the following output –

Output:-

c-switch-case-statement

C Nested If Else Statement

C Nested If else statement

In C programming, when there is an if statement inside another if statement then it is known as a nested if-else statement. Nested if-else can also be simplified using C Switch Case Statement.

Nested If else statement Syntax

Below is the general syntax of nested if-else statements in c programming:

Syntax:-

Nested If else statement Example

Below is a simple example to demonstrate the use of a nested if-else statement in c programming:

Example:-

When we run the above c program, will see the following output –

Output:-

c-nested-if-else-statement

C if else if Statement

C if else if Statement

In C programming, the if-else if statement allows us to add an alternative set of test conditions in the if..else statement using else-if and single else statements for the if condition. In such way if..else.if statement is used to select one among several blocks of code to be executed.

C if..else..if Statement Flow Diagram

c-if-else-if-statement

c-if-else-if-statement

if else if Statement Syntax

Below is the general syntax of the if-else if statement in c programming:

Syntax:-

if-else if Statement Example

Below is a simple example to demonstrate the use of if-else if statement in c programming:

Example:-

Output:-

c-if-else-if-statement

C If Else Statement

C if-else Statement

The c if-else statement is an extended version of the If statement. When we want to execute a block of code when the if the condition is true and another block of code when the if the condition is false, In such a case we use the if-else statement. The statements inside the “if” body only execute if the given condition returns true. If the condition returns false then the statements inside the “else” body are executed.

But what if we want to do something else if the condition is false. Here comes the C else statement. We can use the else statement with the if statement to execute a block of code when the condition is false.

C If else Statement flowchart

dart-if-else-flowchart

if-else Statement Syntax

The general syntax of if-else statement is as follows:

Syntax:-

Working of if-else Statement

Here, the if statement evaluates the test condition inside the parenthesis (). The Condition is a Boolean expression that results in either True or False. if it results in True then statements inside if the body is executed, if it results in False then statements inside else body is executed.

C if-else Example

Below is a simple example to demonstrate the use of if-else in c programming:

Example:-

Output:-

C If else Statement

C if Statement

C if Statement

The c If a statement is a decision-making statement. It allows a block of code to be executed only when a specified condition is true. If a statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be either true or false. If the statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of If. The statements inside the “if” body only execute if the given condition returns true. If the condition returns false then the statements inside “if” body are skipped

Need of If Statement In C

When we want to execute a block of code only when a given condition is true then we use if statement. It is one of the most simple decision-making statements.

Types of if Statement In C

The if statement can be used in many forms depending on the situation and complexity. There are following four different types of if statements:

If Statement Flowchart

The flowchart image illustrates the working of if statement:

c-if-statement

if Statement Syntax

Below is the general syntax of if statement in c programming:

Syntax:-

Working Of if Statement

Here, the if statement evaluates the test condition inside the parenthesis (). The Condition is a Boolean expression that results in either True or False. If it results in True then statements inside if the body is executed. If it results in False then execution is skipped from if body.

if Statement Example

Below is a simple example to demonstrate the use of if in c programming:

Example:-

The condition (num > 50) specified in the “if” returns true for the value of num, so the statement inside the if the body is executed. Then we would see the following output.

Output:-

c_if_statement

Multiple Conditions In if Statement

Multiple conditions in an if statement can be added using either logical AND (&&) and/or OR(||) operators. If we are using logical AND (&&) operator then the if statement will be executed if both the conditions are true. And if we are using logical OR(||) operator then the if statement will be executed if either of those conditions is true.

Example 1:-

Here, if statement will be executed if both the conditions are true. But, if the first condition is false it will not test the second condition and directly execute the else statement if provided. It will test the second condition only when the first condition is true.

Example 2:-

Here, if statement will be executed if either of those conditions is true. But, if the first condition is true it will not test the second condition and execute the if part. It will test the second condition only when the first condition is false.

Relational Operators In if Statement

Relational operators can be used for making a decision and testing conditions, which returns true or false. C has a set of relational operators that can be used in test expression.

  • < less than
  • <= less than or equal to
  • > greater than
  • >= greater than or equal to
  • == equal to
  • != not equal to

Notice:- The equal to (==) is different from the assignment operator (=). The equal to (==) operator is used to test if two values are equal to each other.

C Decision Making Statements

In C Programming, decision-making statements allow you to make a decision, based upon the result of a test condition. The decision-making statements are also known as Conditional Statements. The if, if-else, If else if Ladder and switch statements are used as decision-making statements in c programming.

What Is Decision Making Statements

So far, we have learned that all statements in a c program are executed sequentially (top to bottom) in the order in which they are written. This occurs until there are no jump statements or loop statements. But sometimes we may need to change the order of program execution depending on some specific conditions. Here comes the need for decision-making structures that enable computers to decide on a set of statements to be executed depending upon some conditional choices. These statements are called the decision-making statements or Conditional Statements.

Decision Making Statements In C

In C programming, we have a rich set of Decision Making statements that enable computers to decide which block of code to be executed based on some conditional choices. Decision-making statement statements are also referred to as conditional statements. A Decision-making statement evaluates single or multiple test expressions which result in “TRUE” or “FALSE”. The outcome of the test expression/condition helps to determine which block of the statement(s) to execute if the condition is “TRUE” or “FALSE” otherwise. It is to be noted that the C language assumes any non-zero or non-null value as true and if zero or null, treated as false.

Types of Decision-Making Statements

In C, we have the following types of decision-making statements –

c-decision-making-statements-types

if Statement

C If statement allows a block of code to be executed only when a specified condition is true. An if statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be either true or false. If the statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of If.

Syntax:-

Example:-

The above example is valid for the single statement in the body of the if statement. When there is a set of multiple statements in the body of the if statement. Then it is written inside a pair of parenthesis.

If Else Statement

The if-else statement is an extended version of the If statement. When we want to execute the same block of code if a condition is true and another block of code if a condition is false, In such a case we use an if-else statement. if the condition is true then the statements inside the body are executed otherwise else part is executed.

Syntax:-

Nested If else Statement

When there is an if statement inside another if statement then it is known as a nested if-else statement. It is used to check multiple conditions.

Syntax:-

if-else if ladder Statement

The if-else if ladder statement is used to select one of among several blocks of code to be executed. It checks the first conditional expression, and if it returns false, then it will check the second conditional expression, and so on. If all conditional expressions return false, it executes the else part.

Syntax:-

Switch Case Statement

A switch case statement is a simplified form of the nested if-else statement, it avoids long blocks of if-else if statements.

Syntax:-