if Statement
An if/else statement tests a particular condition, if the condition evaluates to true, a course-of-action is followed i.e. a statement or a set of statements is executed. Otherwise another set of instructions are executed (the else block). Such statements are called program control statements that control the flow of execution in a program.
Normally, the statements are executed sequentially as written in the program. This is known as normal flow of control. But sometimes, the flow of control of a program has to be altered depending upon certain conditions. The statements which allow us to do so are known as selection statements. C++ provides two types of control statements: if/else and switch.
Syntax:
Example: The piece of code given below accepts the value of a and b, checks for the condition in if block and then executes the statements in if block. If the condition in if block fails then the else block statements are executed.
Output : 1.When the if block is executed
2. When the else block is executed
What are Nested if/else statements?
A nested if is a statement that has another if in its body or in it else's body. The nested if can have one of the following three forms:
1.
2.
3.
A common programming construct in C++ is the if–elseif ladder, which is often called the if–elseif staircase because of its appearance. It takes the following general form:
Syntax:
Example:
Comments - No Responses to “if Statement”
Sorry but comments are closed at this time.