Conditional statements help you to make a decision based on certain conditions. These conditions are specified by a set of conditional statements having boolean expressions which are evaluated to a boolean value of true or false. Show
In our life, we frequently encounter certain situations where we have to make a decision be it your favorite food, movie, hobby, or the color of our shoes. In C programming also, you may encounter similar kinds of situations where you need to make a decision based on the two possibilities that are yes/no or true/false acceptance, we will learn all about the conditional statements in this article. There are the following types of conditional statements in C.
If statementThe single if statement in C language is used to execute the code if a condition is true. It is also called a one-way selection statement. When we use the if condition, we pass the argument and if the argument will be satisfied then the respective code will be executed otherwise nothing can happen. Below is the if statement followed by the conditional expression. Syntaxif(expression) { //code to be executed } How the "if" statement works
Note"Expression must be scalar type" i.e evaluated to a single value. if Statement Example#include<stdio.h> #include<conio.h> void main() { int num=0; printf("enter the number"); scanf("%d",&num); if(n%2==0) { printf("%d number in even",num); } getch(); } If-else statementThe if-else statement in C language is used to execute the code if the condition is true or false. It is also called a two-way selection statement. The single if statement may work pretty well, but if you want to work with multiple variables or the extended conditional parameters, then the if-else statement is the optimum choice. By using the if statement, only one block of the code executes after the condition is true but by using the if-else statement, there are two possible blocks of code where the first block is used for handling the success part and the other one for the failure condition. Syntaxif(expression) { //Statements } else { //Statements } How the "if..else" statement works
if..else Statement Example#include<stdio.h> #include<conio.h> void main() { int num=0; printf("enter the number"); scanf("%d",&num); if(n%2==0) { printf("%d number in even", num); } else { printf("%d number in odd",num); } getch(); } Nested If-else statementThe nested if...else statement is used when a program requires more than one test expression. It is also called a multi-way selection statement. When a series of the decision are involved in a statement, we use the if-else statement in nested form. Nested if-else statements can be useful when we can have multiple sources of expression and the values and based on the specific value, we need to check nested conditions. Its recommended for the best practice to avoid using nested if-else statement as it may turn into a conditional bubbling situation, better to use if-else-if or the switch case for the better conditional handling. Syntaxif( expression ) { if( expression1 ) { statement-block1; } else { statement-block 2; } } else { statement-block 3; } Example#include<stdio.h> #include<conio.h> void main( ) { int a,b,c; clrscr(); printf("Please Enter 3 number"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if(a>c) { printf("a is greatest"); } else { printf("c is greatest"); } } else { if(b>c) { printf("b is greatest"); } else { printf("c is greatest"); } } getch(); } If..else If ladderThe if-else-if statement is used to execute one code from multiple conditions. It is also called a multipath decision statement. It is a chain of if..else statements in which each if statement is associated with an else if statement and the last would be an else statement. Syntaxif(condition1) { //statements } else if(condition2) { //statements } else if(condition3) { //statements } else { //statements } If..else If ladder Example#include<stdio.h> #include<conio.h> void main( ) { int a; printf("enter a number"); scanf("%d",&a); if( a%5==0 && a%8==0) { printf("divisible by both 5 and 8"); } else if( a%8==0 ) { printf("divisible by 8"); } else if(a%5==0) { printf("divisible by 5"); } else { printf("divisible by none"); } getch(); } Switch Statementswitch statement acts as a substitute for a long if-else-if ladder that is used to test a list of cases. A switch statement contains one or more case labels that are tested against the switch expression. When the expression match to a case then the associated statements with that case would be executed. We have seen the way of using conditional statements such as if, if-else. if-else ladder, but the need for an additional way of dealing with conditional statements may seem unnecessary but based on the certain usage, switch case was defined to check for the single condition, and based on the multiple cases, code can be executed. Below is the basic syntax that shows how to use and implement a switch statement. SyntaxSwitch (expression) { case value1: //Statements break; case value 2: //Statements break; case value 3: //Statements case value n: //Statements break; Default: //Statements } switch statement Example#include<stdio.h> #include<conio.h> void main( ) { char grade = 'B'; if (grade == 'A') { printf("Excellent!"); } else if (grade == 'B') { printf("Well done"); } else if (grade == 'D') { printf("You passed"); } else if (grade == 'F') { printf("Better try again"); } else { printf("You Failed!"); } } getch(); } Read More Articles Related to C Programming Language Note
WHEN ONE IF () statement written in another IF () statement it is called as ___ *?Expert-verified answer
Yes, writing the 'if' statement with-in another if is called as nested-if. The nested-if statements are the one that means that the 'if' statement will follow under another of the 'if' condition in the programming language.
What kind of statement is the if statement?An if statement is a programming conditional statement that, if proved true, performs a function or displays information. Below is a general example of an if statement, not specific to any particular programming language.
What do you call an ifIf-else statement. It is also called as branching as a program decides which statement to execute based on the result of the evaluated condition.
What is nested IF statement?Nested IF functions, meaning one IF function inside of another, allow you to test multiple criteria and increases the number of possible outcomes.
|