Multiple Statements Under if-else

Multiple Statements Under if-else

Its not always important that we need to execute only one statement at a time whenever 'if' statements are called. It may happen that we need to execute multiple statements when some 'if' sorts out to TRUE. In that case what we do is add just a pair of curly brackets i.e. "{}" immediately after 'if' and write all our instructions that are to be executed within these brackets. Same can be done with else as well. Lets take an example and grab up this concept. I've provide a picture of a sample run at the end of the program to clear the concept.

using namespace std;
int main()
    char i;
    cout<<"Give a Grade to C++ : ";
    if(i=='A' || i=='a')
        cout<<"\nI like C++.";
        cout<<"\nI love C++.";
        cout<<"\nC++ is just awesome.";
        cout<<"\nNop! C++ isn't my type.";
        cout<<"\nIts annoying.";
        cout<<"\nC++ doesn't suits me.";

Its not that we only can execute cout statements in it. Any statement that do not flashes error can be executed including arithematic operations as well. Here's the output of the program.

<<if else Statement                                           Home                                      Loops In C++>>

No comments:

Post a Comment