Hello everyone i am new in C language and need some basic help.

see this program,

void main(void)
{
int a,b;
clrscr ();
{
for (a=1;a<5;a++)
{
for (b=2;b<6;b++)
{
printf ("value of a is %d , value of b is %d \n",a,b);
b=a+1;
a=a++;
}
}
}
getch();
}

i've used 'for' loops in this program and i am getting this output.

value of a is 1, value of b is 2
value of a is 2, value of b is 3
value of a is 3, value of b is 4
value of a is 4, value of b is 5

and it is perfectly fine.

but i want to know that why i need to increment the value of a again since i've already incremented it in this statement

{
for (a=1;a<5;a++)
{

why i need to increment it again after printf function ?
why it is not working if i am not incrementing it again ?


2nd question:

see this program,

void main(void)
{
int a,b;
clrscr ();
a=0, b=2;
while (a<1)
{
while (b<7)
{
printf ("a is %d, b is %d\n",a,b);
b=b+2;
}
a=a+1;
}
getch ();
}

i am getting this output and it is perfectly ok.
a is 0, b is 2
a is 0, b is 4
a is 0, b is 6

but when i am writing a=a+1; after b=b+2;
i.e

void main(void)
{
int a,b;
clrscr ();
a=0, b=2;
while (a<1)
{
while (b<7)
{
printf ("a is %d, b is %d\n",a,b);
b=b+2;
a=a+1;
}
}
getch ();
}

the program is giving different output
a is 0, b is 2
a is 1, b is 4
a is 2, b is 6

why is this happening ??
why the value of a is increasing from 0 when i am writing a=a+1; after b=b+2 ??
why should i have to write a=a+1; after delimeter ?

Edited 6 Years Ago by Xufyan: n/a

See inline....
You need to have two loops. The outer loop should run from 0,1,2,3.... and the inner loop should run from 2,4,6.....

The strategy to achieve this is to set the outer loop to 0 and then run the entire inner loop from 2,4,6... etc. Once the inner loop is over, then set the outer loop to 1 and then again run the inner loop from 2,4,6... The same steps are followed until the outer loop terminates.

void main(void)
      {
         int a,b;
         clrscr (); 
         a=0, b=2;
         while (a<1)      
        {
               while (b<7)
                 {
                     printf ("a is %d, b is %d\n",a,b);
                     b=b+2;
                  }   //newly added
                     a=a+1;
                //}  this while loop should end after b=b+2
        }
      getch (); //not a standard function. Don't use it.
      }

Hello everyone i am new in C language and need some basic help.

see this program,

void main(void)
{
int a,b;
clrscr ();
{
for (a=1;a<5;a++)
{
for (b=2;b<6;b++)
{
printf ("value of a is %d , value of b is %d \n",a,b);
b=a+1;
a=a++;
}
}
}
getch();
}

i've used 'for' loops in this program and i am getting this output.

value of a is 1, value of b is 2
value of a is 2, value of b is 3
value of a is 3, value of b is 4
value of a is 4, value of b is 5

and it is perfectly fine.

Are you sure this the O/P that you are getting, I ran the code and I got an infinite loop at
a=1, b=3

The statement b=a+1 make the value of b 2 and then because of the for loop, b becomes 3. Then again 2 then again 3 and so on


but i want to know that why i need to increment the value of a again since i've already incremented it in this statement

{
for (a=1;a<5;a++)
{

why i need to increment it again after printf function ?
why it is not working if i am not incrementing it again ?


2nd question:

see this program,

void main(void)
{
int a,b;
clrscr ();
a=0, b=2;
while (a<1)
{
while (b<7)
{
printf ("a is %d, b is %d\n",a,b);
b=b+2;
}
a=a+1;
}
getch ();
}

i am getting this output and it is perfectly ok.
a is 0, b is 2
a is 0, b is 4
a is 0, b is 6

but when i am writing a=a+1; after b=b+2;
i.e

void main(void)
{
int a,b;
clrscr ();
a=0, b=2;
while (a<1)
{
while (b<7)
{
printf ("a is %d, b is %d\n",a,b);
b=b+2;
a=a+1;
}
}
getch ();
}

the program is giving different output
a is 0, b is 2
a is 1, b is 4
a is 2, b is 6

why is this happening ??
why the value of a is increasing from 0 when i am writing a=a+1; after b=b+2 ??
why should i have to write a=a+1; after delimeter ?

Google for nested loops in C. I think your doubts will get answered there

Also dont use void main, make it int main

Edited 6 Years Ago by abhimanipal: n/a

The statement a = a++; gives what is called undefined behavior because it is changing the variable a twice. Therefore the result may not be as expected.

Use either a = a+1; or a++; but not both.

yes

void main(void)
{
int a,b;
clrscr ();
{
for (a=1;a<5;a++)
{
for (b=2;b<6;b++)
{
printf ("value of a is %d , value of b is %d \n",a,b);
b=a+1;
a=a++;
}
}
}
getch();
}

this program is surely giving the following output

value of a is 1, value of b is 2
value of a is 2, value of b is 3
value of a is 3, value of b is 4
value of a is 4, value of b is 5

i just want to know why should we have to increment the value of a in line number 12 ??? since we have already incremented it in line number 6 ?

Hi Xufyan:

The body of a loop starts from { and ends at }. If you look at your examples you have what we call nested loops - loop inside loop. In both of your examples there is an internal loop inside an external loop.

1st question:

For every value of a in the external loop (first for statement) the entire internal loop will execute, every time initializing b to 2 until the condition b<6 is false. This means that since a=a+1 (or a=a++ which should be changed to a=a+1 as WaltP pointed out) is part of the internal loop therefore it will be executed for every value of b in the internal loop.

Whereas the statement a++ is part of your external for loop it will be executed when the internal loop is finished executing for every value of b.

So these two statement are doing different jobs and have their own significance and their placement will depend on what you are trying to do.

2nd question

The same argument applies. Since you are moving a=a+1 to inside of the internal loop from outside of the internal loop, now it runs for every value of b, and hence it runs many more times than it used to when it was outside the loop.

The keypoints are:
1. The loops start with { and end with }
2. If there are two nested loops, the internal loop executes entirely for each and every time when the external loop executes.

Hope this helps.

Edited 6 Years Ago by shah1248: n/a

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