-->





C++ Programming if else statement

If-else statement
  • It is similar to if statement i.e. It is also used to execute or ignore a set of statements after testing a condition.
  • In if-else statement one condition and two blocks of statements are given.
  • First blocks contain if statement with condition and its body part.
  • Second is else and it contain only body part.
Syntax:
Explanation working of if-else statement
  • A condition is a logical or relational expression and it produces either true  or false result.
  • If the condition is true the first block of if-else statement (which is if-statement) is executed and second is ignored and after executing the first block , the control is transferred to next statement after if -else structure.
  • If the condition is false then the first blocks of statement is ignored and the second block of statement is executed. After the executing the second block of statement the control is transferred to next statement after if-else structure.
Possible Errors which can occur using if-else statement 
  • Else statement cannot be use without if statement 
         like that:
                        else
                               {
                                   block of statement
                                }
    compiler will indicate error like misplaced else statement
  • Else statement cannot be use alone within the body of if statement
     like that:
                   if(condition)
                      {
                              else
                                    {
                                      block of statement
                                    }
                    }

Let take an example to implement if-else statement
           Program to find number is Even or Odd using if-else statement

Code





  1. #include<iostream>
  2. using namespace std;

  3. int main()

  4. {
  5.     int Num;

  6.     cout<<"Enter Number to check it is Even or Odd: ";

  7.     cin>>Num;

  8.       if(Num%2==0)

  9.          {

  10.              cout<<"Number is Even";

  11.          }

  12.       else

  13.       {

  14.           cout<<" Number is Odd";

  15.       }
  16. }


3 comments:

  1. very informative post indeed.. being enrolled in http://www.wiziq.com/course/6314-learn-c-programming-language-low-priced-student-edition was looking for such articles online to assist me.. and your post helped me a lot

    ReplyDelete
  2. can somebody help me to compute the simple intrest using c++ program

    ReplyDelete
  3. can somebody help me to compute the simple intrest using c++ program

    ReplyDelete