Home     Blog

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:

if structure if Statement

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.

if examples if Statement

Output : 1.When the if block is executed

if output if Statement

2. When the else block is executed

else output if Statement

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:

if else block if Statement

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Will.Spencer on

Leave a Reply

Related Posts

  • Conditional Operators and Switch Statements

    C++ has an operator that can be used as an alternative to an if-statement. This operator is called the conditional operator (?:). This operator can be used to replace an if-else statement of the following general form: if(expression) {      statements ;      -      - } else {      statements ;      -      - } The above form of [...]...


  • break and continue Statements

    break Statement The break statement is used to alter the flow of control. When a break statement is executed in a while loop, for loop, do-while loop or switch statement, it causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from [...]...


  • goto Statement

    In programming, significance has always been given to use of structured programming technique to build reliable software that are easy to debug, maintain and modify. In some cases, performance is more important than strict obedience to structured programming technique. In these cases, some unstructured programming technique may be used. For example, we can use break [...]...


  • Throw and Catch Mechanism

    Throw Mechanism When an exception, which is desired to be handled, is detected, it is thrown using the throw statement. It can be used in any one of the following form: throw(exception) ; throw exception ; throw ; // used for re-throwing an exception The object passed to the throw statement may be of any [...]...


  • Iterations

    An Iteration involves repeating some portion of a program, a specified number of time or until a particular condition is satisfied. This repetitive operation in C and C++ is achieved through for, while and do-while loop. How does a for loop work? A for loop is the easiest iteration loop to understand the C and [...]...