This If and While loop issue

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
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
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.
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
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.