When an if statement is placed inside another if statement it is known as this type of statement?
Conditional Statements in C programming are used to make decisions based on the conditions. Conditional statements execute sequentially when there is no condition around the statements. If you put some condition for a block of statements, the execution flow may change based on the result evaluated by the condition. This process is called decision making in ‘C.’ Show In ‘C’ programming conditional statements are possible with the help of the following two constructs: 1. If statement 2. If-else statement It is also called as branching as a program decides which statement to execute based on the result of the evaluated condition. In this tutorial, you will learn- If statementIt is one of the powerful conditional statement. If statement is responsible for modifying the flow of execution of a program. If statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of If. The syntax for if statement is as follows: The condition evaluates to either true or false. True is always a non-zero value, and false is a value that contains zero. Instructions can be a single instruction or a code block enclosed by curly braces { }. Following program illustrates the use of if construct in ‘C’ programming: #includeThe above program illustrates the use of if construct to check equality of two numbers.
Relational OperatorsC has six relational operators that can be used to formulate a Boolean expression for making a decision and testing conditions, which returns true or false : < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to Notice that the equal test (==) is different from the assignment operator (=) because it is one of the most common problems that a programmer faces by mixing them up. For example: int x = 41; x =x+ 1; if (x == 42) { printf("You succeed!");}Output : You succeedKeep in mind that a condition that evaluates to a non-zero value is considered as true. For example: int present = 1; if (present) printf("There is someone present in the classroom \n");Output : The If-Else statementThe if-else is statement is an extended version of If. The general form of if-else is as follows: if (test-expression) { True block of statements } Else { False block of statements } Statements;n this type of a construct, if the value of test-expression is true, then the true block of statements will be executed. If the value of test-expression if false, then the false block of statements will be executed. In any case, after the execution, the control will be automatically transferred to the statements appearing outside the block of If. Following programs illustrate the use of the if-else construct: Let’s start. #includeOutput: The value is greater than 10
In ‘C’ programming we can use multiple if-else constructs within each other which are referred to as nesting of if-else statements. Conditional ExpressionsThere is another way to express an if-else statement is by introducing the ?: operator. In a conditional expression the ?: operator has only one statement associated with the if and the else. For example: #includeOutput : y =2Nested If-else StatementsWhen a series of decision is required, nested if-else is used. Nesting means using one if-else construct within another one. Let’s write a program to illustrate the use of nested if-else. Output: The value is:1The above program checks if a number is less or greater than 10 and prints the result using nested if-else construct.
Try changing the value of variable see how the program behaves. NOTE: In nested if-else, we have to be careful with the indentation because multiple if-else constructs are involved in this process, so it becomes difficult to figure out individual constructs. Proper indentation makes it easy to read the program. Nested Else-if statementsNested else-if is used when multipath decisions are required. The general syntax of how else-if ladders are constructed in ‘C’ programming is as follows: if (test - expression 1) { statement1; } else if (test - expression 2) { Statement2; } else if (test - expression 3) { Statement3; } else if (test - expression n) { Statement n; } else { default; } Statement x;This type of structure is known as the else-if ladder. This chain generally looks like a ladder hence it is also called as an else-if ladder. The test-expressions are evaluated from top to bottom. Whenever a true test-expression if found, statement associated with it is executed. When all the n test-expressions becomes false, then the default else statement is executed. Let us see the actual working with the help of a program. #includeOutput: First classThe above program prints the grade as per the marks scored in a test. We have used the else-if ladder construct in the above program.
Try modifying the value and notice the change in the output. Summary
|