For Decision making, C uses the keyword if. The general form of the if statement is
The test expression should always be enclosed in parentheses. If the test_expression is nonzero(true) then the statement statement1 is executed; Otherwise, statement1 is skipped and control passes to the next statement. Statement1 can be a single statement or compound statement.
The statement1 part of the if statement is the body of the if statement. This construct is shown in the following flowchart.
printf("Enter Marks :");
Enter Marks: 50
If -Else Statement
The if – else statement is also used for decision making purpose, C uses the keyword if-else. The general form of the if-else statement is,
The if statement executes a single statement or a compound statement, when the test expression is true or nonzero. It does nothing when the test expression is not true.
When the test expression is true or nonzero, then the body of if is executed and control passes to the next statement immediately following the if-else construct. But if the expression is not true or zero then the body of else is executed.
printf("Enter Your Marksn");
C – Output
Enter Your Marks
you failed Condition 2;
Enter Your Marks
Nested If-Else Statement
The meaning of nesting is to have same construct within itself. Nesting means statement inside statement. Previously we learn about if-else statement, we identified a set of statements as the if block ans another set as the else block.
The statement within the if-else statements could be another if or if-else statement. So this type of statement is called a nested conditional statement. This inner conditional expression is said to be nested within the outer one.
The statements can be surrounded by compound statement, if there exist more than one statement or other statement.
printf("Enter 2 Numbers:");
printf("first is larger");
printf("second is largern");
printf("both numbers are equaln");