Actually i am trying to create some patterns using loop like pascal's triangle etc. But the concept of loop is still not very clear to me as i am learning c language myself without any professional help. would anyone please elaborate me with the help of an example and explain me the reason of each line of code and its functions for better understanding.
THANK YOU............

Actually i am trying to create some patterns using loop like pascal's triangle etc. But the concept of loop is still not very clear to me as i am learning c language myself without any professional help. would anyone please elaborate me with the help of an example and explain me the reason of each line of code and its functions for better understanding.
THANK YOU............

There are tons of online resources about the loops with examples and the C language as a whole, you might wanna check that out starting from here.(unless you are bounded by some rules or something like that).

You need to practice questions on loops , first try to solve basic problems patterns and then step up to high level, little practice will make you clear about the ideas of loops and you will visualize the functionality , working and code just by seeing the pattern.
Good luck..

Well, here's my crash course on loops:

Loops are things which helps you do repetitive jobs easily (but i think you knew that already, right?). So basically, say i wanna print your name 10 times; i do something like this:

for (int i = 0; i < 10; i++)
{
    printf ("deva89");
}

Now lets explain each part:
for is the type of loop (you have others, like: while, do-while, etc.)
(int i = 0; i < 10; i++) this can be broken up into three segments:
1. int i = 0; is the initialization of the loop variable (the variable that's going to keep track of the iterations.
2. i < 10; is the loop check. At each iteration the loop is going to check whether the condition holds; if yes it proceeds with the iteration, else it terminates.
3. i++ is incrementing your loop variable (in this case i). can you see why this is important? Also, this increment will happen after the loop body has been executed.
After that we have a curly brace to mark the beginning of the loop body.
printf ("deva89"); is what is inside the loop body.
Finally another curly brace ends the loop body.

Now go through the code once again and try to understand the format.
Now try to be the compiler yourself and give your code a mental run...something like:

first i is 0...is it less than 10...yes...so go inside loop body...what do we have here?...o..a printf...fine...print it...we have nothing else...so go back...increment the loop variable (remember, i said the increment part will happen after the loop body has been executed?)...so now i is 2...is it less than 10... ... ... i think you got it... (but don't cheat; do it a few more times; don't stop right now)

Now, if you have understood about the basic loop, lets go into nested loops (which you'll frequently encounter while playing with patterns)

Well, nested loops are loops inside loops...and, if you have followed the logic about a single loop you can easily extend that for nested loops.

Here's an example:

for (int i = 5; i > 0; i--)
{
    for (int j = i; j > 0; j--)
    {
        printf ("*");  // inside the inner(second) loop
    }
    printf ("\n");  // inside the outer(first) loop (see indentation)
}

Okay, take a deep breath...:)

Now, the i-- and j-- are similar to i++ (seen in the previous loop) only that they decrement instead of incrementing. (like, i starts with 5 and its greater than o, so goes inside and afterwards gets decremented by one...ie it becomes 4...and so on)

I'll leave you to figure out what this snippet is going to print...don't use an actual compiler to tell me that...use the strategy as earlier...be the compiler yourself and run it mentally...

let me know what you think...:)

This article has been dead for over six months. Start a new discussion instead.