Nested-if There are situations where you need to check a condition which is nested in another condition. For example we have sorted list of numbers from 1 to 100. We have divided it into four equal quadrants. i.e. Quadrant No Values From Values To 1 1 25 2 26 50 3 51 75 4 76 100 In above scenario, if the value is greater than 50, then it would be greater than 70. If the value is not greater than 50 then it cannot be greater than 70. The general syntax of nested-if is Syntax if (condition) { if(condition) { ……… } …….. } Let’s implement it in code, so that we would be in a better position to understand the working of nested-if; for better functionality I’ve added else to it. #include <stdio.h> #include <conio.h> void main() { int value; clrscr(); ...
if-else-if Consider a scenario where we have to check multiple conditions which are related to each other. For example we enter marks of a student and then we assign a grade to it. Marks Grade >=80 A >=70 and <80 B >=60 and <70 C >=50 and <60 D <50 Fail In above scenario, if we use simple if then we need to use FIVE if statements. These if’s would be checked every time, even if the first condition is true. Syntax if(condition) { // Statements if true } else if (condition) { //Statements if true } else if (condition) { //Statements if true } else if (condition) { //Statements if true } else if (condition) { //Statements if true } . . else { } With à if Example 19 - Grading system using if With à if-else-if Exam...
Comments
Post a Comment