[try Beta version]
Not logged in

 
This If and While loop issue

Jan 1, 2013 at 7:45pm
I am still really new to c++ and I want to know about these loops so i was experimenting and I saw something pretty different here.

Why does 1 generate two dots, and 2 generate a whole line?

1.
1
2
3
4
5
6
	 
         int o =100;
         if(o < 700){
	 gfx.PutPixel(o,300,255,255,50);
	 gfx.PutPixel(o,301,255,255,50);
	 o++;	}


2.
1
2
3
4
5
         int o= 100;	
         while(o < 700){
	 gfx.PutPixel(o,300,255,255,50);
	 gfx.PutPixel(o,301,255,255,50);
	 o++;	}

Last edited on Jan 1, 2013 at 7:56pm
Jan 1, 2013 at 7:48pm
Because the first is not a loop? If does something once if the condition is true, while keeps doing it as long as the condition is true.
Last edited on Jan 1, 2013 at 7:48pm
Jan 1, 2013 at 7:48pm
Because, in the first case, the PutPixel just runs twice, resulting in two dots.
In the second case, the PutPixel runs from the 'o' coordinate to the '700' coordinate, so it draws all pixel from o to 700, provided that o is less than 700.
Jan 1, 2013 at 7:49pm
1 will run the code between { and } once if o is < 700

2 is a while loop. The code between { and } will repeat as long as o is < 700
Jan 1, 2013 at 8:56pm
You could even have in an if-statement a condition that contradicts the condition that started the if statement. For example, the following code would run normally:
1
2
3
4
5
6
if (x == 100)
{
 //some stuff here
 x++;
 //some more stuff
} //end if 
Topic archived. No new replies allowed.