c++ If...else statement with program example.

 

what is if...else statement and how we used if...else statement in c++?

If...else statement in c++

If...else statement is a type of the decision statement. it is used to make two-way decision . Some time a programmer need to execute another statement to take a decision then the programmer used if else statement in a program. if else statement execute the one statement or set of statement when the condition is true and when the condition is false then another statement is execute. The if else statement is most important feature of c++ program to take double way decision. As in only if statement or program the only one statement is execute when the condition is true and the other statement will be skipped when the condition is false. Some time we need to execute double statement or grouped of statement when the condition is true and false then we use if else statement in our program.

If...else statement evaluate whether a statement is true or false and only execute a set of statement if the statement is true and the statement of else will execute when the condition is false.

The else statement is never appear in a program without if statement. When we need to take two way decision then we must add else statement with if statement.

Main point in if else statement.

  • Both statements can never be executed.
  • Both statements can never be skipped.


syntax of if....else statement .

         For single statement 

                          if (condition )

                               statement ;

                           else

                                statement ;


            syntax of multiple statement 

                                if (condition)

                                    {   statement 1;

                                         statement 2;

                                           .

                                             .

                                              statement n;    

                                                    }

                                            else

                                          {     statement 1;

                                                  statement 2;

                                                    .

                                                        .

                                                        statement n;

                                                        }

In above statement there is two different between single statement and the multiple statement , In single statement have no curly brackets {    } , Multiple statement have curly bracket. Because in c++ program we must have add curly brackets in multiple statement rather than single statement....


flow chart 

flow chart example of if....else statement ,


If...else statement in c++ with program example.
flow chart 


c++ if else program

Now we write a program and learn how we used if else statement in c++ program, As we know that we have two way decision the one is if statement decision and the other is else decision ,

Let we know that how we used if else statement in c++

If...else statement in c++ with program example.
if else statement in c++


Now the output of the  executed statement  c++ program.

 for even number program. 



If...else statement in c++ with program example.
 for even number program.



for odd number program.


If...else statement in c++ with program example.
 for odd number program.



if you want to know about if statement and how we used if statement in a program 

then click here