Loops, if, else

Pages: 12
No. I forget the technical term, but C++ allows for unlimited whitespace, but only in certain instances, like you need to put white space between datatypes and a variable name, but you have the option of putting spaces and extra lines just about anywhere. My only suggestion is try to limit tons of whitespace. I've seen people post code who have 3-4 empty lines towards the end of their program, especially between closing braces.

As for what you were asking in regards to my previous posts, the indentation didn't affect the action of the while loop, the brackets did. The first two cases demonstrate how, without brackets, the loop only applies to the next statement. Typically a while statement will have brackets purely because unless you know what you're doing, it will run forever. Cases where brackets are omitted frequently is like in for loops and if conditions.

For Example:
1
2
3
if (false)
   dontDoMe();
   doMe();


In the example, you can see how dontDoMe() doesn't get run, but because a condition/loop that doesn't have brackets only affects the following statement, doMe() does get called. This is why I said the indentation will help a lot. When you start working with multidemensional arrays, you'll have nested for loops and the brackets can get in your way. I've already had four for loops that only affected one statement.

Another Example:
1
2
3
4
5
6
for (int i = 0; i < 2; i ++)
   for (int j = 0; j < 2; j ++)
      for (int k = 0; k < 2; k ++)
         for(int l = 0; l < 2; l ++)
            doMe();
            fooBar();


From the code above, doMe() actually gets called 16 times. Each for loop will affect only the next thing after it, and nesting fors can be helpful, but can get messy too. If you don't know for loops yet, each loop runs exactly twice, well if the first one runs twice, the next one runs four times, and the next one runs 8 times, and the one after that runs 16 times, which is why doMe() gets called 16 times. Now fooBar() will only get called once since it's not following any of the loops. It's something worth trying out sometime so you understand how different things work.

Those were just some quick examples to show what happens when you don't use brackets. I can show you the same previous fors with brackets in the correct spaces:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < 2; i ++)
{
   for (int j = 0; j < 2; j ++)
   {
      for (int k = 0; k < 2; k ++)
      {
         for(int l = 0; l < 2; l ++)
         {
            doMe();
         }
      }
   }
}
            fooBar();


Nothing changed except adding the "implied" brackets in. Does it make sense why sometimes certain things don't have brackets? It saves unnessecary lines, but some beginners find it easier to understand when you can see the physical scope of each loop.
Last edited on
I think I understand. Forgive me I started a few days ago, but have been hooked and determined.

Brackets are instilling regulation. Making it strict to your demands and desired output. As without brackets your allowing it to run, without certain limitation to meet another output?

The challenge for me is I have a hard time understanding these examples without practical application. When I started loops the online tutorial and books I have only cover them as increasing a number, to a defined limit. I had no clue how to implement a loop to my need until you and Stewbond.
Taking your knowledge and applying it is one of the hardest steps in programming in my opinion, and I can't think of anything very practical to show you an example of, but one that you can try is this:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main() {
   for(int i = 10; i > 0; i --)
      std::cout << i << "\n";
   std::cout << "Blast Off";
   std::cin.ignore();

   return 0;
}


The code is simple, and there is a lot of different things you can do with them, but like I said, any loop or condition without brackets will only affect the following line after them. While you play around with something like that, let me go through some of my projects and see if I can't show you something that has more loops like these.

Edit: Here is another example that I did a few days ago as well. It's a simple program that doesn't include too much, but it was to solve a problem that would have taken a very long time by hand.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {
    int sum = 0;

    for (int i = 1; i < 1000; i ++)
        if (i % 3 == 0)
            sum += i;
        else if (i % 5 == 0)
            sum += i;

    std::cout << sum;

    return 0;
}


The code takes all of the numbers 1 - 999 (the for loop) sees if they're evenly divisible by 3 (the first if condition) then if it is, adds it to sum. If it's not divisible by 3 (the else part of the else if) then it sees if it is evenly divisible by 5, (the second if) and if it is, adds it to sum.

The last statement before return just simply prints out all numbers 1-999 that are evenly divisible by 3 or 5. (numbers were not allowed to be used twice, for example 15 can only be used once.)
Last edited on
Topic archived. No new replies allowed.
Pages: 12