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 –
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:-
|
if(condition) statement; Or if(condition) { // execute statement 1; // execute statement 2; ... } |
Example:-
|
if(a>b) printf("a is greater than b"); |
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(a>b) { printf("body of if statement"); printf("a greater than b"); } |
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:-
|
if(condition) { //Block of code to be executed comes here if condition is true; } else { //Block of code to be executed comes here if condition is false; } |
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(condition) { Statement; } else { if(condition) { Statement; } else { Statement; } } |
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:-
|
if(condition1) { Statement 1; } else if(condition2) { Statement 2; } . . else { Statement n; } |
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:-
|
switch(condition) { case val1: //Block of code to be executed if expression = val1; break; case val2: //Block of code to be executed if expression = val2; break; default: //Block of code to be executed //if expression is different //from both val1 and val2; } |