Is If Else statement indentation important or not in C duplicate

0 votes

Is indentation in an if else statement important for code execution, or is it just something to do for cleaner code?

while(c != cols) {
    if(r == pad + 1 && c == pad + 1) {
      cout << greet;
      c += greet.size();

    } else {

      if(r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
        cout << "*";

      else
        cout << " ";
        ++c;
    }
  }

I'm not sure why the prefix increment of c gets done regardless of whether r=0 or not.

An asterisk is written if the if statement is true. 

Otherwise, a blank space is written and c is increased. 

That's how I see it, however c is increased regardless of the values of r or c.

This is what it says in the book, however I couldn't locate an explanation:

Notice how the distinct indentation of ++c; emphasises the fact that it gets performed regardless of whether we are on the border or not.

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
569 views

1 answer to this question.

0 votes

White space has no effect on the understanding of code in C and C++. 

That is not to say that the programmer should be unconcerned about its misuse.

The easiest method to demonstrate what the above code truly represents is to specify all of the inferred braces directly, as seen below. 

The 'if then' or 'otherwise' clause only affects one line of code in the if statement with no brackets.

This is one of the reasons why people strive to insist on 'proper coding standards' to guarantee that other people can clearly grasp the programmer's flow and meaning.

while(c != cols) {
    if(r == pad + 1 && c == pad + 1) {
        cout << greet;
        c += greet.size();
    } else {
        if(r == 0 || r == rows - 1 || c == 0 || c == cols - 1) {
            cout << "*";
        } else {
            cout << " ";
        }
        ++c;
    }
}
answered Jun 27, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

what is containership or nesting in C++?

According to what I believed, a class ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
383 views
0 votes
0 answers

Why is the size of an empty class in C++ not zero?

Why does the following output 1? #include <iostream> class Test { }; int ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
573 views
0 votes
1 answer

Function default argument value depending on argument name in C++ [duplicate]

When the function is called with no argument for the corresponding parameter, the default argument is evaluated.  In a default argument, a parameter must not appear as a potentially evaluated expression.  A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name. It provides the following example: int h(int a, ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
611 views
0 votes
1 answer

In C++, what is a virtual base class?

When employing multiple inheritance, virtual base classes are used to prevent several "instances" of a particular class from appearing in an inheritance hierarchy. Consider the following example: class A { public: void Foo() {} ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
536 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
704 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,431 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,465 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,889 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
857 views
0 votes
1 answer

Check if element is in the list (contains)

The simplest and quickest method. You could also ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
2,114 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP