Pernyataan Nested IF (IF Bersarang) C, Lengkap Contoh Program Ditulis MateriDosen Selasa, 16 Mei 2017 Tulis Komentar Edit Salah satu permasalahan yang pasti akan dijumpai dalam pembuatan program adalah suatu percabangan. There can be many types of nested loops in C but the mostly used nested loops are. Nested while loop. Nested do-while loop. Nested for loop. Note: There can also be very variation of nested loops where a while loop can be inside a for loop, a for loop can be inside a do-while loop and many more.
- C++ Basics
- C++ Object Oriented
- C++ Advanced
- C++ Useful Resources
- Selected Reading
An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Syntax
The syntax of an if..else statement in C++ is −
If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
Flow Diagram
Example
When the above code is compiled and executed, it produces the following result −
if..else if..else Statement
An if statement can be followed by an optional else if..else statement, which is very usefull to test various conditions using single if..else if statement.
When using if , else if , else statements there are few points to keep in mind.
- An if can have zero or one else's and it must come after any else if's.
- An if can have zero to many else if's and they must come before the else.
- Once an else if succeeds, none of he remaining else if's or else's will be tested.
Syntax
![Dev Dev](/uploads/1/2/6/1/126147007/785009390.png)
The syntax of an if..else if..else statement in C++ is −
Example
When the above code is compiled and executed, it produces the following result −
In the last tutorial we learned how to use if statement in C. In this guide, we will learn how to use if else, nested if else and else if statements in a C Program.
C If else statement
Syntax of if else statement:
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.
Flow diagram of if else statement
Example of if else statement
In this program user is asked to enter the age and based on the input, the if.else statement checks whether the entered age is greater than or equal to 18. If this condition meet then display message “You are eligible for voting”, however if the condition doesn’t meet then display a different message “You are not eligible for voting”.
Nested If Dev C++
Note: If there is only one statement is present in the “if” or “else” body then you do not need to use the braces (parenthesis). For example the above program can be rewritten like this:
C Nested If.else statement
When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
Syntax of Nested if else statement:
Syntax of Nested if else statement:
Example of nested if.else
Output:
C – else.if statement
The else.if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else.if statement. Traktor pro for mac.
Syntax of else.if statement:
Example of else.if statement
Lets take the same example that we have seen above while discussing nested if.else. We will rewrite the same program using else.if statements.
Output:
As you can see that only the statements inside the body of “if” are executed. This is because in this statement as soon as a condition is satisfied, the statements inside that block are executed and rest of the blocks are ignored.
Nested If Statement In C
Important Points:
1. else and else.if are optional statements, a program having only “if” statement would run fine.
2. else and else.if cannot be used without the “if”.
3. There can be any number of else.if statement in a if else.if block.
4. If none of the conditions are met then the statements in else block gets executed.
5. Just like relational operators, we can also use logical operators such as AND (
1. else and else.if are optional statements, a program having only “if” statement would run fine.
2. else and else.if cannot be used without the “if”.
3. There can be any number of else.if statement in a if else.if block.
4. If none of the conditions are met then the statements in else block gets executed.
5. Just like relational operators, we can also use logical operators such as AND (
&&
), OR(||
) and NOT(!
).