could you please tell me what is the logic behind different variations of priniting stars

e.g

****
**
*



what loop will determine how many coloumns and rows are to be printed and how?

Thank you very much!

Recommended Answers

All 3 Replies

another question is it must to use curly braces in nest loops like if I write the below program without curly braces in while loop then the program will generate error but

for (i=1;i<=7; i++)
    {
        j=i;
    while (j<=7){

    cout<<"*";
    j++;
}

if I don't use the curly braces in nested for loop's body only then it'll execute correctly..why is this.
for(m=1; m<=5; m++)

    for(m=1; m<=5; m++)
    {
        for(n=1; n<=5; n++)

        cout<<"*";
        cout<<endl;
    }


    for(n=1; n<=5; n++)

    cout<<"*";
    cout<<endl;
}

Well, what you have to understand is that the braces aren't directly connected with the loop or conditional, per se; in fact, the control structures are actually defined as to accept the one statement immediately following the conditional. In EBNF, it would be defined as:

<if-statement> ::= 'if' '(' <conditional> ')' <statement>

For those unfamiliar with EBFN, what this says is, "an if-statement is defined as the word 'if', followed by an open parenthesis, followed by a conditional, followed by a close paqrenthesis, followed by a statement". Note that this

What the braces are used for is to form a compound statement or block which can be placed anywhere there could be a single statement. Indeed, in the formal definiton, a statement is defined in terms of either simple-statements or blocks. Again, in EBNF, you would have:

<statement> ::= <simple-statement> ';'
               | <block>

<block> ::= '{' <statement>* '}'

What the first says is, "A statement is defined as either a simple-statement followed by a semi-colon, or a block.", while the second says, "A block is defined as an open brace, followed by one or more staatements, followed by a close brace". You'll notice two things: one, that you can put a block anywhere you can put a statement, and two, that the definitions are mutually recursive, that is to say, they are defined in tems of each other. This recursive expansion is part of what makes complex compound statements possible.

If this is still not clear, don't worry. What it comes down to is this: you can have either one statement as the body of a for() loop, or a block of statements. However, you have to have a multi-line loop body inside of a block, otherwise, only the first line is actually inside the loop. This can cause a lot of confusion, so it is usually best to always put a loop or conditional's body inside a block, even if it is only a single statement, just as a way of covering yourself (it is also a good idea because the bodies have a way of changing during development, so by having them in a block to start with, you don't need to change as much if you need to add to it later).

Going from the general to the specific, the reason you have an error in the fist code fragment becomes clear once you apply a more consistent brace style to the code:

for (i=1;i<=7; i++)
{
    j=i;
    while (j<=7)
    {
       cout<<"*";
       j++;
    }

You'll notice that when done this way, the braces don't line up! See how there are two open braces, but only one close brace? The outer block (the one associated with the for() loop) isn't getting closed, which is why you are getting a syntax error. This is one of the main reasons why applying a consistent indent style is important in C.

With the second fragment, again, suitable indentation makes the code much easier to understand:

    for(m=1; m<=5; m++)
    {
        for(n=1; n<=5; n++)
            cout<<"*";
        cout<<endl;
    }

    for(n=1; n<=5; n++)
        cout<<"*";
    cout<<endl;
}

Notice how, in the inner for() loop and the second outer for(), only the first statement following the loop conditional (cout<<"*";) is actually part of the loop, while the statement following that one (cout<<endl;) is not. You could re-write this for clarity's sake as the following:

    for(m=1; m<=5; m++)
    {
        for(n=1; n<=5; n++)
        {
            cout<<"*";
        }
        cout<<endl;
    }

    for(n=1; n<=5; n++)
    {
        cout<<"*";
    }
    cout<<endl;
}

While this is a bit more verbose, it is obvious from the brace matching and the indentation what is and is not inside the loop body.

As an aside, I shoul emntion something about stylistic concerns. There are a few different ways to style your braces and indentation in C and its syntactic relatives, and different programmers prefer different approaches. You'll hear many different arguments put forward supporting one or the other approach, but the truth is, consistency is far more important than the specific style you use. If you use one style in a part of the program, you should use the same style throughout the whole program; otherwise, it looks inelegant and can be confusing. So, if you have your open braces on the same line as the conditionals:

    for(m=1; m<=5; m++) {
        for(n=1; n<=5; n++) {
            cout<<"*";
        }
        cout<<endl;
    }

    for(n=1; n<=5; n++) {
        cout<<"*";
    }
    cout<<endl;
}

You shouldn't mix that with open braces set on a line by themselves. Pick one approach, and stick with it, at least as much as you can.

Finally, I would mention that you could easily reduce the redundancy of the second fragment by writing the two matching pieces of code as a function, like so:

void stars(int n)
{
    for(n = 0; n < 5; n++) 
    {
        cout << "*";
    }
    cout << endl;
}

int main()
{
    for(m = 0; m < 5; m++) 
    {
        stars(m);
    }

    stars(5);
}

Hope this helps.

nice explanation Schol-R-Lea

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.