LESSON 07
WORK PROGRAM ANALYSIS Analyze the programs below
and state what will be displayed on the Serial Monitor. Copy and paste the program and run it to
see if you are correct. Careful, some are tricky. You will have to write a
quiz that will have this exact format (but no computer to check your
answers). PROGRAM #1 - IF void setup() { Serial.begin(9600); int a = 400; if (a < 500) { Serial.println("yo"); } } void loop() {} PROGRAM #2 – IF ELSE void setup() { Serial.begin(9600); int a = 400; if (a < 500) { Serial.println("yoyo"); } else { Serial.println("bobo"); } } void loop() {} PROGRAM #3 – IF ELSE void setup() { Serial.begin(9600); int a = 400; if (a >= 500) { Serial.println("coco"); } else { Serial.println("nono"); } } void loop() {} PROGRAM #4 – EQUAL TO void setup() { Serial.begin(9600); int a = 300; if (a == 500) { Serial.println("boca"); } else { Serial.println("yoda"); } } void loop() {} PROGRAM #5 – NOT EQUAL TO void setup() { Serial.begin(9600); int a = 300; if (a != 500) { Serial.println("mama"); } else { Serial.println("papa"); } } void loop() {} PROGRAM #6 – NOT EQUAL TWO ß (See what I did there?) void setup() { Serial.begin(9600); int a = 3; if (a != 3) { Serial.println("wola"); } else { Serial.println("soda"); } } void loop() {} PROGRAM #7 – ELSE IF void setup() { Serial.begin(9600); int a = 7; if (a < 5) { Serial.println("caca"); } else if (a < 10) { Serial.println("lala"); } else { Serial.println("wawa"); } } void loop() {} PROGRAM #8 – ELSE IF #2 void setup() { Serial.begin(9600); int a = 3; if (a < 5) { Serial.println("yes"); } else if (a < 10) { Serial.println("wes"); } else { Serial.println("less"); } } void loop() {} PROGRAM #9 – NO ELSE void setup() { Serial.begin(9600); int b = 42; if (b == 5) { Serial.println("five"); } else if (b > 100) { Serial.println("alive"); } } void loop() {} PROGRAM #10 – MULTIPLE ELSE IF void setup() { Serial.begin(9600); int b = 4; if (b == 1) { Serial.println("one"); } else if (b == 2) { Serial.println("two"); } else if (b == 3) { Serial.println("three"); } else if (b == 4) { Serial.println("four"); } else { Serial.println("five"); } } void loop() {} PROGRAM #11 – MULTIPLE ELSE IF
#2 void setup() { Serial.begin(9600); int b = 6; if (b == 1) { Serial.println("one"); } else if (b == 2) { Serial.println("two"); } else if (b == 3) { Serial.println("three"); } else if (b == 4) { Serial.println("four"); } else { Serial.println("five"); } } void loop() {} PROGRAM #12 – COMPOUND OPERATOR AND void setup() { Serial.begin(9600); int mark = 74; if (mark >= 0 && mark <=
100) { Serial.println("legal"); } else { Serial.println("illegal"); } } void loop() {} PROGRAM #13 – COMPOUND OPERATOR
OR void setup() { Serial.begin(9600); int mark = 74; if (mark < 0 || mark > 100) { Serial.println("illegal"); } else { Serial.println("legal"); } } void loop() {} PROGRAM #14 – EXPRESSION IN IF
STATEMENT void setup() { Serial.begin(9600); Serial.print("abc"); int x = 4; if (x – 1 > 3) { Serial.println("def"); } else { Serial.println("ghi"); } } void loop() {} PROGRAM #15 – DOUBLE TROUBLE void
setup() { Serial.begin(9600); double value = 74.135; if (value == 74.1) { Serial.println("same"); } else { Serial.println("different"); } } void
loop() {} PROGRAM #16 – NESTED IF
STRUCTURES void
setup() { Serial.begin(9600); int value = 6; if (value < 100) { if (value > 0) { Serial.println("yuk"); } else { Serial.println("duck"); } } else { Serial.println("stuck"); } } void
loop() {} PROGRAM #17 – NESTED IF
STRUCTURES #2 void
setup() { Serial.begin(9600); int a = 7; int b = 4; int c = 5; if (a >= b + c) { Serial.println(a); } else { if (a < b) { Serial.println(b); } else { Serial.println(c); } } } void
loop() {} |