/* 24/07/2021 mathematicsv2 Subtraction How integers can over run with subtraction */ int q; int w; int myAnswer; void setup() { // Serial.begin(9600) starts serial communication. 9600bps bits per second = 1200 characters per second Serial.begin(9600); //Send script name Serial.println("mathematicsv2..."); //this will print a blank line Serial.println(" "); } void loop() { if (q < 1) { //Example 1 //make q = 1 so the if statement will fail next time round q = 1; Serial.print("q = "); Serial.println(q); //Simple addition Serial.println("Simple subtraction"); for (w = 0; w < 10; w++) { myAnswer = w - q; Serial.print(" w ="); Serial.print(w); Serial.print(" myAnswer ="); Serial.println(myAnswer); } //Example 2 q = -10; Serial.println("Simple subtraction... - negative number double negative = positive"); Serial.print("q = "); Serial.println(q); for (w = 0; w < 10; w++) { myAnswer = w - q; Serial.print(" w ="); Serial.print(w); Serial.print(" myAnswer ="); Serial.println(myAnswer); } //Example 3 q = -10; Serial.println("Simple subtraction...negative numbers"); Serial.print("q = "); Serial.println(q); for (w = 0; w < 10; w++) { myAnswer = q - w; Serial.print(" w ="); Serial.print(w); Serial.print(" myAnswer ="); Serial.println(myAnswer); } //example 4 //change the value of q to 32760 q = -32760; Serial.println("Simple subtraction...The problem of overflowing the variable type"); Serial.print("q = "); Serial.println(q); for (w = 0; w < 10; w++) { myAnswer = q - w; Serial.print(" w ="); Serial.print(w); Serial.print(" myAnswer ="); Serial.println(myAnswer); } //make q equal to 1 to make sure the if loop does not run again q = 1; } }