Wednesday, August 11, 2010

Selection Statement (if-else if-else) in c++


Selection Statement (if-else if-else) in c++


To understand this chapter you should know about Comparison Operators which is explained in previous chapter. Selection statements are very important in programming because we make decisions using it.

/* A simple c++ program example to demonstrate the if statement. */


// Program 1

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int first, second;

    cout << "Enter two integers." << endl;

    cout << "First " << setw (3) << ": ";
    cin >> first;

    cout << "Second "<< setw (2) << ": ";
    cin >> second;

    if (first > second)
        cout << "first is greater than second." << endl;

    return 0;
}


/* A c++ program example to demonstrate if-else statements */


// Program 2

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int first, second;

    cout << "Enter two integers." << endl;

    cout << "First " << setw (3) << ": ";
    cin >> first;

    cout << "Second "<< setw (2) << ": ";
    cin >> second;

    if (first > second)
        cout << "first is greater than second." << endl;

    else
        cout << "first is less than or equal to second." << endl;

    return 0;
}


/* A c++ program example to demonstrate if-else if-else statements. */


// Program 3

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int first, second;

    cout << "Enter two integers." << endl;

    cout << "First " << setw (3) << ": ";
    cin >> first;

    cout << "Second "<< setw (2) << ": ";
    cin >> second;

    if (first > second)
        cout << "first is greater than second." << endl;

    else if (first < second)
        cout << "first is less than second" << endl;

    else
        cout << "first and second are equal." << endl;

    return 0;
}


Note: All the above three programs satisfies the condition that if first value
is greater then display the message "first is greater than second".

Question: When to use if, if-else and if-else if-else ?
Answer: It depends upon your program. The number of possibilities your program has. For example, in the above program there are three possibilities. The value could either be greater, smaller or equal. All these possibilities are covered using if-else if-else statements. Depending upon the possibilities you can add more "else if" statement before the final else statement. It is good programming to cover all the possibilities to make your program perfect.

In the next chapter we will learn about nested if statements.

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

10 comments:

  1. how about making choices by using letters as you variables?

    ReplyDelete
  2. Selection Statement (Switch-case) in C++ for the above program.

    ReplyDelete
  3. It was good and perfect way of using if-else-if ... But if you put some logical diagrams for more better and conceptual clear programs

    ReplyDelete
  4. thanks for the notes.i'm taking for c++ subject n im new in this program..

    ReplyDelete
  5. Replies
    1. stew mean number of space you want

      Delete
  6. thanks bro... inshaAllah this sample can help me do my project.. :)

    ReplyDelete
  7. in 1st program what is a purpose of setw (3) and setw (2) and what is purpose of header file please explain in detail

    ReplyDelete
  8. Thank You,,Its really useful,,i have got some assignments n now sure this would really help me with solutions.

    ReplyDelete