FOR Loop Problem

Hi

I hope this is the right section for my Problem. I wrote this Code:

int pz;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  
                        for(int zahl = 50; zahl < 500; zahl++)
			{
			
				for(pz = 0; pz > zahl; pz++);
				{


                                        Serial.println(zahl);
					Serial.println(pz);
                                        Serial.println("\n");
                                        
					
					delay(50);

				}
                        
										
			}
}

Via Serial I got this wit this Program:

50
0


51
0


52
0


53
0


54
0


55
0


56
0


57
0


58
0


59
0

But I like to get something like this:

50
1

50
2

50
3

[...]

50
50

51
1

51
2

51
3

[...]

51
51

52
1

[...]

etc etc etc.

But I can’t find my wrong part of the Code. I don’t know how I can fix that.

Berkutta

I presume you want to do

for(pz = 0; pz < zahl; pz++);

Yeah. But now it stays on the value.

50
50


51
51


52
52


53
53


54
54


55
55


56
56


57
57


58
58

Hi, try

for(pz = 0; pz < zahl; pz++)

Tom… :slight_smile:

Also select TOOLS and click Auto Format.

Removing of the Semikolon did the whole trick. Wow! Thank you!

So the next Problem is here. I want to divide the variable zahl (integer) through pz (integer). But I only get a weird result in a float variable without koma provides.

int pz;
float rechnung;

void setup()
{
  Serial.begin(9600);
}

void loop()
{

  for(int zahl = 50; zahl < 500; zahl++)
  {

    for(pz = 0; pz < zahl; pz++)
    {


      Serial.println(zahl);
      Serial.println(pz);



      rechnung = zahl/pz;

      Serial.println(rechnung,3);




      Serial.println("\n");

      delay(0);
    }



  }


}

The first is zahl, second pz, and the last should be the rechnung float. But I only get .000 and I don’t know why.

58
9
6.000

58
10
5.000

58
11
5.000

But I only get .000 and I don't know why.

Because C does the int division first and then assigns it to a float.

If you want to do float arithmetic you have to cast them as floats first like this:-

rechnung = (float)zahl/(float)pz;

Why all the blank lines in your code?

Now everything works great. Thank You!