Dear members please help me in understanding the control flow in the following code, thanks

include <iostream>

class constructs

#include <iostream>
class constructs
  {
       char inp;
       public:

       void acceptCharacter()
       {
       cout<<"Enter a character: ";
       cin>>inp;
       if(inp>='A')                           //ASCII value of A = 65
             if(intp<='Z')                    //ASCII value of Z = 90
                  cout<<endl<<"Uppercase";
             else if (inp>='a')               //ASCII value of a = 97

                    if(inp<='z')              //ASCII value of z = 122
                      cout<<endl<<"Lowercase";
                    else
                      cout<<endl<<"Input character>z";
             else
                    cout<<endl<<"Input character>Z but lessthan a";
        else
             cout<<endl<<"Input character less than A";
        };

  int main()
  {
      constructs c1;
      c1.acceptCharacter();
      return 0;
  }

Suppose if I key in + whose ASCII value is 43 the last statement "Input character less than A" is displayed, my question is about the understanding of control flow, does it skips all the other if else??? please guide me... many thanks.

Recommended Answers

All 6 Replies

Perhaps this rewrite makes it clearer. Braces make code easier to read.

void acceptCharacter()
{
       cout<<"Enter a character: ";
       cin>>inp;
       if(inp>='A')                           //ASCII value of A = 65
       {
             if(intp<='Z')                    //ASCII value of Z = 90
             {
                  cout<<endl<<"Uppercase";
             }
             else if (inp>='a')               //ASCII value of a = 97
             {
                    if(inp<='z')              //ASCII value of z = 122
                    {
                        cout<<endl<<"Lowercase";
                    }
                    else
                    {
                         cout<<endl<<"Input character>z";
                    }
             }
             else
             {
                    cout<<endl<<"Input character>Z but lessthan a";
             }
        }     
        else
        {
             cout<<endl<<"Input character less than A";
        }
};

This makes things clear, ok, so if I key in + (ASCII-43) the first condition fails and the entire if(inp>='A') block is totally skipped and the control falls down to the last else statement on no.27???

And if I key in \ (ASCII 92) the first if statement on no.5 is true so the control enters 1st if block to check for second if statment which fails, control skips the 2nd if block (8-10) and enters else if block (11) it too comes out to be false so it skips from (12-21) and falls to the second last else statement on 22??? is my understanding of control flow right??? is this what actually happens, many thanks for your response.

is this what actually happens

Can you think of a way to find out for yourself?

using CodeBlocks, I know the program works, since am learning by myself I need to confirm if am on the right track in deciphering things correctly, for eg. in electronics we get to check the signals where ever using oscilloscope thereby helping self to understand what exactly is happening at what point. Wish I could just tap inbetween chunks of codes to understand the control flow better using printf(?) or something maybe my approach of learning programming language could be wrong, I just dont know, just seeking guidance and reassurance. Thanks Moschops.

Wish I could just tap inbetween chunks of codes to understand the control flow better using printf(?) or something

Sounds like a good idea to me. We have cout in C++.

You could also look into using the debugger and putting breakpoints at places of interest.

void acceptCharacter()
{
       cout<<"Enter a character: ";
       cin>>inp;
       if(inp>='A')                           //ASCII value of A = 65
       {
             cout << "Greater or equal to A" << endl;
             if(intp<='Z')                    //ASCII value of Z = 90
             {
                  cout << "Less or equal to Z" << endl;
                  cout<<endl<<"Uppercase";
             }
             else if (inp>='a')               //ASCII value of a = 97
             {
                    cout << "Greater or equal to a" << endl;
                    if(inp<='z')              //ASCII value of z = 122
                    {
                        cout << "Less or equal to z" << endl;
                        cout<<endl<<"Lowercase";
                    }
                    else
                    {    
                        cout<<endl<<"Input character>z";
                    }
             }
             else
             {

                    cout<<endl<<"Input character>Z but lessthan a";
             }
        }     
        else
        {
             cout<<endl<<"Input character less than A";
        }
};

Sorry for my delayed response, wasnt keeping well, many thanks for your prompt responses.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.