Tuesday, August 31, 2010

Nested if-else statements in c++


Nested if-else statements in c++



/*A c++ program example that takes input from user for username and password and then makes decision.*/


// Program 1

#include <iostream>
#include <string>

using namespace std;

const string userName = "computergeek";
const string passWord = "break_codes";


int main ()
{
    string name, pass;

    cout << "Username: ";
    cin >> name;

    cout << "Password: ";
    cin >> pass;

    if (name == userName)
    {
        if (pass == passWord)
            cout << "You are logged in!" << endl;
        else
            cout << "Incorrect username or password." << endl;
    }
    else
        cout << "Incorrect username or password." << endl;

    return 0;
}


/*A c++ program example that takes input from user for username and if username is correct then only asks for password and makes decision.*/


// Program 2

#include <iostream>
#include <string>

using namespace std;

const string userName = "computergeek";
const string passWord = "break_codes";

int main ()
{
    string name, pass;

    cout << "Username: ";
    cin >> name;

    if (name == userName)
    {
        cout << "Password: ";
        cin >> pass;

        if (pass == passWord)
            cout << "You are logged in!" << endl;
        else
            cout << "Incorrect password." << endl;
    }
    else
        cout << "Incorrect username." << endl;

    return 0;
}


Programming Advice & Explanation:


Whenever possible avoid nesting or deep nesting as it makes program difficult to read. We should program in a manner that it can be read and understood easily by us and other programmers. For example, in the "Program 1" the nesting can be avoided by using logical operator. However in "Program 2" the nesting is required as we want to take input for password only if username is correct.

In the next chapter we will learn about logical operators and will rewrite the "Program 1" using it.

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

15 comments:

  1. hi,,,,,,,,,,,,,,,can i ask something?

    ReplyDelete
  2. what if if-else statement nested in an else.Any example ?

    ReplyDelete
  3. what should i do if dnt want to display the password ....that is i want to display ******* in the password

    ReplyDelete
  4. could you pls tell me whats wrong here?
    im a beginner.


    #include
    using namespace std;
    int main()
    {
    int sc1, sc2, sc3, sc4, as, ave;
    cout << "enter first score:\n" <> sc1;
    cout << "enter second score:\n" <> sc2;
    cout << "enter third score:\n" <> sc3;
    cout << "enter fourth score:\n" <> sc4;
    as = sc1 + sc2 + sc3 + sc4;
    ave = as / 4;
    cout << "average score is:\t" <=95)
    cout << "grade is 1.50" <=90)
    cout << "grade is 1.75" <=85)
    cout << "grade is 2.0" <=80)
    cout << "grade is 2.5" <=75)
    cout << "grade is 3.0" <<endl;
    else (ave<=74);
    cout << "grade is 5.0" <<endl;
    system ("pause");
    return 0;
    }

    ReplyDelete
  5. #include
    using namespace std;
    int main()
    {
    int sc1, sc2, sc3, sc4, as, ave;
    cout << "enter first score:\n" <> sc1;
    cout << "enter second score:\n" <> sc2;
    cout << "enter third score:\n" <> sc3;
    cout << "enter fourth score:\n" <> sc4;
    as = sc1 + sc2 + sc3 + sc4;
    ave = as / 4;
    cout << "average score is:\t" <=95)
    cout << "grade is 1.50" <=90)
    cout << "grade is 1.75" <=85)
    cout << "grade is 2.0" <=80)
    cout << "grade is 2.5" <=75)
    cout << "grade is 3.0" <<endl;
    else (ave<=74);
    cout << "grade is 5.0" <<endl;
    system ("pause");
    return 0;
    }

    ReplyDelete
  6. hai!!i'am first time visit u blogger and i interesting to ask something about c++?
    can u explain me..about the program at above..
    why in #include not using iostream..i mean like this #include..??

    ReplyDelete
  7. hi! guys this written program doesnt work dont know why

    ReplyDelete
  8. #include
    using namespace std;
    int main()
    {
    char fname[10],lname[10],subj[10];
    float a,b,c,d,e,itema,itemb,ave;
    cout<<"Enter Name:";
    cin>>fname>>lname;
    cout<<"Subject:";
    cin>>subj;
    cout<<"Enter a Grade:";
    cin>>a;
    cout<<"Enter b grade:";
    cin>>b;
    cout<<"Enter c grade:";
    cin>>c;
    cout<<"Enter d grade:";
    cin>>d;
    itema=((a+b+c)/3)*0.5;
    itemb=d*0.5;
    ave=itema+itemb;
    e=ave;
    cout<<"**********************"<<"\n";
    cout<<"Name:"<= 95) && (e <= 99))
    {
    cout << "1.25" << endl;
    cout<< "Almost there, Strive more"<< endl;
    }
    else if ((e >= 90) && (e <= 94))
    {
    cout << "1.50" << endl;
    cout<< "Damn, a little bit more"<< endl;
    }
    else if ((e >= 85) && (e <= 89))
    {
    cout << "2.0" << endl;
    cout<< "You can do it next time"<< endl;
    }
    else if ((e >= 80) && (e <= 84))
    {
    cout << "2.5" << endl;
    cout<< "Try again so it will be more easier"<< endl;
    }
    else if ((e >= 75) && (e <= 79))
    {
    cout << "3.0" << endl;
    cout<< "OMG!you almost failed!"<< endl;
    }
    else if (e <= 74)
    {
    cout << "5.0" << endl;
    cout<< "You SSSSStupid!Noob!!!"<< endl;
    }
    cout<<"**********************"<<"\n";
    system("pause");
    return 0;
    }


    just added some stuff

    ReplyDelete
  9. can i ask something??
    how can i use nested if else using venn diagram?

    ReplyDelete
  10. How can I go about writing a nested if statement to calculate the weight of a person on a planet?

    ReplyDelete
  11. this just helped me to complete my assignment.... thanks a million

    ReplyDelete
  12. can you explain and give example nested if(if_else_if)

    ReplyDelete