# Programmers: please learn when to not use while loops

**URL:** <https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841>\
**Category:** The BBQ Pit\
**Created:** [May 12, 2004, 7:54pm UTC](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841 "2004-05-12T19:54:27Z")\
**Posts on this page:** 6\
**Page:** 6

<div class="post-metadata">

**Author:** ![JThunder](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@JThunder](https://boards.straightdope.com/u/JThunder)\
**Post date:** [May 21, 2004, 2:34pm UTC](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841/101 "2004-05-21T14:34:24Z")

</div>

> [@Khadaji](#):
>
> K&R stands for Kernigan and Ritchie. They wrote C Programming Language and were the developers of the C language. They promote the
> 
> if(somthing){  
> dosomething();  
> };
> 
> style of coding. I detest and forbid it in my coding standards.

K&R style made more sense in the old days of 24-line text-only computer screens. It allowed programmers to see more of their code at a glance.

Nowadays, with GUIs, integrated development environments, high-resolution graphics screens and high-speed laser printers, this is no longer a major consideration.

---

<div class="post-metadata">

**Author:** ![tracer](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tracer/32/20578_2.png) [@tracer](https://boards.straightdope.com/u/tracer)\
**Post date:** [May 21, 2004, 3:57pm UTC](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841/102 "2004-05-21T15:57:13Z")

</div>

And wouldn’t you know it, I’m **surrounded** by K&R-brace-style bigots at my current job. Some of them have even tried to make up and enforce a “coding style guide” where everyone **has** to use the K&R brace style. When the old Jargon File jokingly referred to K&R as “the one true brace style”, these bozos took it as gospel.

All right, K&R fanatics, if your brace style is so good, tell me **quickly** – at a glance – which of the following lines of code lies within the brace-delimited block, and which of them are part of the for statement:

```auto

for (pointerA = pointerB->OrphanAPointer();
    pointerA && pointerA->GetName() && pointerA->GetName()[0];
    pointerA = pointerB->GetNextAPointer(pointerA)) {
    ++ count;
    pointerA->AssumeANSIAttribution();
}

```

---

<div class="post-metadata">

**Author:** ![alterego](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@alterego](https://boards.straightdope.com/u/alterego)\
**Post date:** [May 22, 2004, 11:16pm UTC](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841/103 "2004-05-22T23:16:43Z")

</div>

This thread is funny. I just got an A in my Intro to Programming and Problem Solving with C++ class.

On both the midterm and the final I wrote my program and then went back and did the problem solving. Everytime i do problem solving before I write my code I find that I was unable to forsee something and it was a waste of time anyway. But i guess if you can type 120 wpm you can get away with that stuff 🙂

Just wanted to add that all you teachers dissin’ on your students, suck.

---

<div class="post-metadata">

**Author:** ![Sam\_Stone](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@Sam\_Stone](https://boards.straightdope.com/u/Sam_Stone)\
**Post date:** [May 23, 2004, 5:24am UTC](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841/104 "2004-05-23T05:24:18Z")

</div>

> [@](#):
>
> All right, K&R fanatics, if your brace style is so good, tell me quickly – at a glance – which of the following lines of code lies within the brace-delimited block, and which of them are part of the for statement:
> 
> ```auto
> 
> for (pointerA = pointerB->OrphanAPointer();
> pointerA && pointerA->GetName() && pointerA->GetName()[0];
> pointerA = pointerB->GetNextAPointer(pointerA)) {
> ++ count;
> pointerA->AssumeANSIAttribution();
> }
> 
> ```

Well, you CAN indent and format the code better than that. If you had written it like this:

```auto

for ( pointerA = pointerB->OrphanAPointer();
          pointerA && pointerA->GetName() && pointerA->GetName()[0];
          pointerA = pointerB->GetNextAPointer(pointerA)) {
    ++ count;
     pointerA->AssumeANSIAttribution();
}

```

Then it would be pretty clear. Any code can be formatted to make it hard or easy to read. Althought I will admit that multi-line control statements can be a mess.  
As for ‘kids’ doing it, I used to code exclusively using K&R style braces, and I wrote my first ‘C’ code in 1979. Never even saw the other style for probably a decade. I suspect the K&R style was popular at a time when monitors displayed 80 x 24 characters, and every line was precious when you were trying to follow code on a screen.

We had a lot of rules back then to prevent mistakes. For instance, when you can’t see the start of a block because the brace follows the control statement, you don’t do this:

```auto

  If (x == y)
    do_something();
  else
    do_something_else();

```

The rule for us when using KR formatting was that even for single line clauses, you enclose them in braces. First, because it only takes up one extra line instead of two it doesn’t pad your code much. And second, it helps catch those nasty bugs where someone adds a line thinking it’s inside a block.

Imagine you’re looking at a screen that can only display 24 lines. What’s more readable? This:

```auto

  if (x == y) {
      z=do_something();
      if (z) {
          do_the_other_thing();
      }
  }
  else {
       z=do_something_else();
      if (z) {
          do_yet_another_thing();
      }
  }

```

or

```auto

  if (x == y) 
  {
      Z=do_something();
      if (z) 
      {
          do_the_other_thing();
      }
  }
  else 
  {
    z=do_something_else();
    if (z)  
    {
        do_yet_another_thing();
    }
  }

```

The second construct fills up 2/3 of my screen. If there were a few more lines of code in there, I wouldn’t even be able to see the whole thing at once. The first construct saves me four lines of screen real-estate - something that was actually pretty valuable back then.

You can write good code either way, and enforcing certain standards can keep you out of the weeds. But I no longer make a big hairy thing out of coding standards, and neither does anyone else on my team. As long as people are consistent and don’t do blatantly stupid things, good enough. We have to work with so many different languages, legacy codebases, and different coding teams that trying to maintain a single standard is folly, so it’s better to just roll with it.

---

<div class="post-metadata">

**Author:** ![Monstre](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Monstre](https://boards.straightdope.com/u/Monstre)\
**Post date:** [May 23, 2004, 5:36am UTC](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841/105 "2004-05-23T05:36:06Z")

</div>

> [@](#):
>
> from _Sam Stone_  
> As long as people are consistent and don’t do blatantly stupid things, good enough.

Nicely said. That’s the biggest point I try to make in terms of the readability and stylistic issues to my students. I have my preferred style for my own coding, but I basically try to get across to them – there are lots of styles, there’s no one that’s “right” or “wrong”, but whatever you do, code so it’s readable, and be **consistent** in what you do.

Gosh, this thread is getting downright civil. Are we still in the Pit? 😉

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [May 23, 2004, 11:34am UTC](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841/106 "2004-05-23T11:34:42Z")

</div>

> [@](#):
>
> And wouldn’t you know it, I’m surrounded by K&R-brace-style bigots at my current job. Some of them have even tried to make up and enforce a “coding style guide” where everyone has to use the K&R brace style. When the old Jargon File jokingly referred to K&R as “the one true brace style”, these bozos took it as gospel.

I’m surprised that there isn’t already a coding style where you work. I was under the impression that standard coding styles were pretty much the default?

> [@](#):
>
> You may be right. I took my information from the publishers of C Prog Language Ansi C 2ND Edition who say:

> **[C (programming language)](https://en.wikipedia.org/wiki/C_programming_language)**
>
> C (/ˈsiː/, as in the letter c) is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions. It has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computer architectures that range from supercomputers to...

[Previous page](https://boards.straightdope.com/t/programmers-please-learn-when-to-not-use-while-loops/244841.md?page=5)
