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 in C++ by Nicholas
• 5,020 points
29 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 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 in C++ by Nicholas
• 5,020 points
8 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 in C++ by Damon
• 4,960 points
21 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 in C++ by Damon
• 4,960 points
24 views
0 votes
1 answer

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

answered Jun 10 in C++ by Damon
• 4,960 points
45 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,080 points
206 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,080 points
1,414 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
808 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,720 points
636 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 in C++ by Damon
• 4,960 points
36 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 in C++ by Damon
• 4,960 points
26 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP