When you upload a sketch it will run for about 150 milliseconds before the Serial Monitor connects. Serial Monitor has to wait for the uploader to close the serial connection before it can open the serial connection. When the serial connection opens, it resets the board.
I've started adding a 200 millisecond delay after Serial.begin(); to make sure none of my sketch gets run between the time of upload and time Serial Monitor connects.
This mostly causes problems when a sketch reads and writes EEPROM immediately on startup. For example, if you read a location in EEPROM, increment the value, and write it back, the behavior will show it incremented twice on each upload.