If the condition is false the program, execution is terminated. It consists of two blocks of statements each enclosed inside if block and else block respectively. If the condition inside if statement is true, statements inside if block are executed, otherwise statements inside else block are executed.
Else block is optional and it may be absent in a program. Here, a number is entered by user which is stored in n. The if statement checks if the remainder of that number when divided by 2 is zero or not.
If the remainder is zero, the number is even which is printed on the screen. If the remainder is 1, the number is odd. It is used when more than one condition is to be checked. A block of statement is enclosed inside if, else if and else part. Conditions are checked in each if and else if part. If the condition is true, the statements inside that block are executed. If none of the conditions are true, the statements inside else block are executed. Explore C Examples. Related Topics Add Two Integers.
Video: C if else Statement. If the test expression is evaluated to true, statements inside the body of if are executed. If the test expression is evaluated to false, statements inside the body of if are not executed. Working of if Statement To learn more about when test expression is evaluated to true non-zero value and false 0 , check relational and logical operators.
The if statement is easy. Output 2 Enter an integer: 5 The if statement is easy. If the test expression is evaluated to false, statements inside the body of else are executed statements inside the body of if are skipped from execution. Working of if Table of Contents if Statement if Previous Tutorial:.
Next Tutorial:. Share on:. Did you find this article helpful? The flow of execution goes in a way that condition 1 will get tested if it becomes false then, statement 3 will get executed. If the condition 1 gets satisfied i. In case the statement with condition 2 gets false or unsatisfied then it will execute else with statement 2 in consideration. An Example will be good to illustrate the working concept of Nested if statement. Every person is eligible for working once he or she is above 18 years otherwise not eligible.
Moreover, any organization will offer a job if he or she is above 18 years otherwise no job is guaranteed it means the condition then and there becomes false. Therefore, we will make use of another nested if statement to check the required qualification or any other special skill or requirement gets satisfied with this.
This working of nested if the statement is done in a way that when an if the condition gets true and other statements can go for a false condition but then it presumes that it has to become true and satisfactory for the other statement with the second condition then there will be need of Nested if statement. One very special characteristic to describe such type of uncertain logic behind this is helpful with Nested If statement. Control statement like if can be easily nested within another nested if statement besides the fact that if outer statement gets failed then the compiler will skip the entire block irrespective of any other inner statement condition.
Program for analysis of people of certain age groups who are eligible for getting a suitable job if their condition and norms get satisfied using nested if statement.
Program to find which number is greater among the considered number and then how the execution happens with the help of nested if statement if the flow gets successful then it is counted as normal flow.
0コメント