# how does increment & for loops work in c?



## clmlbx (Dec 13, 2007)

a=2

printf(++a * a++ * a++ * ++a)

answer is 216

how ??

and how is for loop used ?


----------



## anantkhaitan (Dec 13, 2007)

```
printf(++a * a++ * a++ * ++a)
```
bcoz we start count from right side.. now look
++a -> 3
a++ -> 4
a++ -> 5
++a -> 6
and ultimately 6*6*6*6 gives 216


```
how is for loop used ?
```
as per the syntax is 

```
for(initialization;condition;value modification){loop body}
```
Example:

```
for(int i=0;i<10;i++)cprintf("%d",i);
```


----------



## clmlbx (Dec 13, 2007)

what about this 

printf(++a*a++*++a)

ans  45

^^^ you mean to say left to right

why did u took 

6*6*6*6

what is the difference between pre & post increment


----------



## timemachine (Dec 14, 2007)

Pre increment : Increment then operate
Post increment : Operate then increment


----------



## Yamaraj (Dec 14, 2007)

Guess you people skipped sequence points and their importance while reading your books. And BTW, that code is non-standard.


----------



## anantkhaitan (Dec 14, 2007)

@clmlbx
You are correct brother..What I told you was my CBSE based bookish knowledge.. I am sorry for that..
It should have been : 4*3*3*3 thats what logic says ..Well today I tried the program (GCC) and output was *108* equals to 4*3*3*3

```
#include<stdio.h>
int main()
{
int a=2;
printf("%d",++a*a++*a++*++a);
}
```

For your second problem the answer should be *36* i.e. 4*3*3 and that is what I got after compiling


----------



## Pathik (Dec 14, 2007)

clmlbx said:
			
		

> a=2
> 
> printf(++a * a++ * a++ * ++a)
> 
> ...


See 
++a means a is first incremented then used and a++ means a is first used then incremented.
So this would mean (3*3*4*6) = 216


			
				clmlbx said:
			
		

> what about this
> 
> printf(++a*a++*++a)
> 
> ...


And this would mean (3*3*5) = 45..
Though the ansewrs may vary with the compiler used.


----------



## The_Devil_Himself (Dec 14, 2007)

^^exactly guys listen up!!different compiler interprets these increments inside printf statements differently afaik,so its like try before you use kind of thing.


----------



## clmlbx (Dec 15, 2007)

can it be more clear ?

add one before or after it is the same thing ..............

how will this program work?

1
12
123
1234
12345

I know using "for" loops and using "\t,\n" but how ............

*
**
***
****
*****,


1
2 4
3 6 9
4 8 12
5 10 15 20



*
**
***
****
*****


----------



## fun2sh (Dec 15, 2007)

its simple. wen priority is same then unirary exp are evaluated from right to left.
so here is wat happens for 
1. a++ -----> means the current value of 'a' is used in exp then it is incremented
2. ++a------> means the current value is incremented by 1 first then that new incremented value is used in the code.



> so for ur exp
> ++a..................*...............a++................... *........a++ ....................*     ++a
> 3 (first incre ..................3 (use then ............. 4(use curren ......................6(coz last a=5)
> then use,a=3 now)..........incre,a=4 now)..........then incre,a=5now


----------



## clmlbx (Dec 15, 2007)

guys I know this is not correct but can someone write  that programme for me ..
these are like examples, I have many of them ,so if I got to know how u did this then I will try others

post no. 9


----------



## QwertyManiac (Dec 15, 2007)

Here you go. 3 simple loop constructs. The i-loop is used for the number of lines to print, and the j-loop is used to display the content needed.


```
#include<stdio.h>

int main(void)
{
	int i,j;
	
	for(i=1;i<6;i++)
	{
		for(j=1;j<i+1;j++)
		{
			printf("%d ",j);
		}
		printf("\n");
	}
	
	printf("\n");
	
	for(i=1;i<6;i++)
	{
		for(j=1;j<i+1;j++)
		{
			printf("* ");
		}
		printf("\n");
	}
	
	printf("\n");
	
	for(i=1;i<6;i++)
	{
		for(j=1;j<i+1;j++)
		{
			printf("%d ",i*j);
		}
		printf("\n");
	}
	
	printf("\n");
	return 0;
}
```
Outputs as:

```
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

* 
* * 
* * * 
* * * * 
* * * * * 

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25
```


----------



## Pathik (Dec 15, 2007)

#include<stdio.h>

int main(void)
{
	int i,j;

	for(i=1;i<6;i++)
	{
		for(j=1;j<i+1;j++)
		{
			printf("*");
		}
		printf("\n");
	}

	printf("\n");
           return 0;
}
Arey just remove the SPACE after the * in the printf.


----------



## clmlbx (Dec 15, 2007)

hi

this all are from left 

how can I give output from right ?

means output to right side of screen


----------



## Pathik (Dec 15, 2007)

Do u need output starting from the right side of the screen or aligned to the right??


----------



## clmlbx (Dec 15, 2007)

STARTING FROM THE RIGHT SIDE AND ALSO ALIGNED TO RIGHT

AGAIN CAPS.......*gigasmilies.googlepages.com/actions1.gif


----------



## Faun (Dec 15, 2007)

clmlbx said:
			
		

> STARTING FROM THE RIGHT SIDE AND ALSO ALIGNED TO RIGHT
> 
> AGAIN CAPS.......*gigasmilies.googlepages.com/actions1.gif


Use ur intuition to solve this

I know u can do it*farm3.static.flickr.com/2074/2106526281_d24c2bda14_o.png, ,and stop forgetting,probably u will suffer from Alzhemeir later. Make ur barin sharp*farm3.static.flickr.com/2398/2107306002_6240bfbc60_o.png


----------



## aditya_v (Dec 16, 2007)

guys, what solutions u are giving is wrt TC only
C++ doesn't enforce this issue, as said by Bjarne Stroustrup



> What's the value of i++ + i++?
> 
> It's undefined. Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:
> 
> ...


----------



## QwertyManiac (Dec 16, 2007)

clmlbx said:
			
		

> hi
> 
> this all are from left
> 
> ...


Look for Output flags (printf flags) in your text book. Do the guys at Ape-tech not teach you ANYTHING at all? These questions are very trivial even for a new programmer!


----------



## clmlbx (Dec 16, 2007)

QwertyManiac said:
			
		

> Look for Output flags (printf flags) in your text book. Do the guys at Ape-tech not teach you ANYTHING at all? These questions are very trivial even for a new programmer!


 
I missed one class and then to I want to complete this assignment to make a good impression   .( my prevoious was not good  ) 


nothing more, and here guys are to help so I asked.....


----------



## Faun (Dec 16, 2007)

clmlbx said:
			
		

> I missed one class and then to I want to complete this assignment to make a good impression   .( my prevoious was not good  )
> 
> 
> nothing more, and here guys are to help so I asked.....


 put ur brain into the subject, logic is all u need.

These things are the simplest ones, the real nightmare is advanced data structures. So save ur questions for that time.

U posted now without CAPS on


----------



## QwertyManiac (Dec 16, 2007)

All you have to do is search.

What you are asking for is left justification I think, so read up on the printf function at CPlusPlus, read the flags part:
*www.cplusplus.com/reference/clibrary/cstdio/printf.html


----------



## MetalheadGautham (Dec 16, 2007)

I think the only reason this thread has these many replies is because this being a simple question, everyone can "prove" that he/she is a programmer


----------



## clmlbx (Dec 17, 2007)

ok guys whatever you say ?

I did  not try and tomorrow morning is my class so can anybody give e the code ?

please......please

*****
-****
--***
---**
----*

and

----*
---**
--***
-****
*****

"-" = spaces

please some one give me code


----------



## QwertyManiac (Dec 17, 2007)

Don't ask code again, its time you set into dusted off your brain-cobwebs and put it to some use!


```
#include<stdio.h>

int main(void)
{
    int i,j;
    char a='*';
    for(i=6;i>0;i--)
    {
        for(j=6;j>i;j--)
        {
            printf("  ");
        }
        
        for(j=1;j<i;j++)
        {
            printf("%c ",a);
        }
        printf("\n");
    }
    
    printf("\n");
    
    for(i=1;i<6;i++)
    {
        for(j=5;j>i;j--)
        {
            printf("  ");
        }
    
        for(j=1;j<i+1;j++)
        {
            printf("%c ",a);
        }
        printf("\n");
    }
    
    printf("\n");
    return 0;
}
```
Outputs as:

```
As you expect.
```


----------



## clmlbx (Dec 17, 2007)

thank you very much

thanxx a lotttttttt *gigasmilies.googlepages.com/1.gif*gigasmilies.googlepages.com/36.gif


----------

