The learner will review code and answer questions about an if else statement.

Conditional If Else Statements

All the flashcards in this set deal with the following code:

/*Assume a temperature sensor is connected to the analog pin and an alarm is connected to the digital output*/

const int tempPin = 1;
const int alarmPin = 0;
const int tempthreshold = 200;
int analogValue;

void setup()
{
   pinmode(alarmPin, OUTPUT);
   Serial.begin(9600);
}

void main()
{
   analogValue = analogRead(tempPin);
   if (analogValue > tempthreshold)
   {
      digitalWrite(alarmPin, HIGH);
   }
   else
   {
      digitalWrite(alarmPin, LOW);
   }

   Serial.println(analogValue);
}

This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

All the flashcards in this set deal with the following code:

/*Assume a temperature sensor is connected to the analog pin and an alarm is connected to the digital output*/

const int tempPin = 1;
const int alarmPin = 0;
const int tempthreshold = 200;
int analogValue;

void setup()
{
   pinmode(alarmPin, OUTPUT);
   Serial.begin(9600);
}

void main()
{
   analogValue = analogRead(tempPin);
   if (analogValue > tempthreshold)
   {
      digitalWrite(alarmPin, HIGH);
   }
   else
   {
      digitalWrite(alarmPin, LOW);
   }

   Serial.println(analogValue);
}

This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.

question image

Correct Answer

Got it!

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

All the flashcards in this set deal with the following code:

/*Assume a temperature sensor is connected to the analog pin and an alarm is connected to the digital output*/

const int tempPin = 1;
const int alarmPin = 0;
const int tempthreshold = 200;
int analogValue;

void setup()
{
   pinmode(alarmPin, OUTPUT);
   Serial.begin(9600);
}

void main()
{
   analogValue = analogRead(tempPin);
   if (analogValue > tempthreshold)
   {
      digitalWrite(alarmPin, HIGH);
   }
   else
   {
      digitalWrite(alarmPin, LOW);
   }

   Serial.println(analogValue);
}

This code is displayed in the image below, which will be on each card, but you may want to make note of it before going on.

question image

Your Answer

Correct Answer

Got it!

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: The alarm will come on if the analog input value is 150.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: The alarm will come on if the analog input value is 150.

question image

Correct Answer

False

Explanation:

The alarm will only be turned on if analogValue > tempthreshold.  If the value read is 150, then analogValue > tempthreshold is 150 > 300, which is false.  Therefore, the else code will run.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: The alarm will come on if the analog input value is 150.

question image

Your Answer

Correct Answer

False

Explanation:

The alarm will only be turned on if analogValue > tempthreshold.  If the value read is 150, then analogValue > tempthreshold is 150 > 300, which is false.  Therefore, the else code will run.

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: The analogValue > tempthreshold condition of the if statement must be true in order for the alarm to be activated.  

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: The analogValue > tempthreshold condition of the if statement must be true in order for the alarm to be activated.  

question image

Correct Answer

True

Explanation:

The only place we turn on the alarm, digitalWrite(alarmPin, HIGH), is within the code of the if statement.  The if statement will only execute its code when analogValue > tempthreshold.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: The analogValue > tempthreshold condition of the if statement must be true in order for the alarm to be activated.  

question image

Your Answer

Correct Answer

True

Explanation:

The only place we turn on the alarm, digitalWrite(alarmPin, HIGH), is within the code of the if statement.  The if statement will only execute its code when analogValue > tempthreshold.

 Next Question
Question 1 of 10

Conditional If Else Statements

In order for the alarm to be turned off, the analog input value would have to be less than...

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

In order for the alarm to be turned off, the analog input value would have to be less than...

question image

Correct Answer

201

Explanation:

The alarm is turned off in the else case's code. That code will execute when the if case does not, so it will execute when analogValue > tempthreshold is false.  Since tempthreshold is 200, analogValue needs to be less than 201 to make the statement false.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

In order for the alarm to be turned off, the analog input value would have to be less than...

question image

Your Answer

Correct Answer

201

Explanation:

The alarm is turned off in the else case's code. That code will execute when the if case does not, so it will execute when analogValue > tempthreshold is false.  Since tempthreshold is 200, analogValue needs to be less than 201 to make the statement false.

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: In this example, the curly brackets after the if and the else could have been eliminated.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: In this example, the curly brackets after the if and the else could have been eliminated.

question image

Correct Answer

True

Explanation:

Since there is only one line of code after both the if and the else cases, we could have eliminated the curly braces and the program would have run the same.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: In this example, the curly brackets after the if and the else could have been eliminated.

question image

Your Answer

Correct Answer

True

Explanation:

Since there is only one line of code after both the if and the else cases, we could have eliminated the curly braces and the program would have run the same.

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: In this program, analogValue is a constant.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: In this program, analogValue is a constant.

question image

Correct Answer

False

Explanation:

analogValue is declared as an int type, but not a const.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: In this program, analogValue is a constant.

question image

Your Answer

Correct Answer

False

Explanation:

analogValue is declared as an int type, but not a const.

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: If more than one line of code is to be executed when the if condition is true, curly brackets must be put around them.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: If more than one line of code is to be executed when the if condition is true, curly brackets must be put around them.

question image

Correct Answer

True

Explanation:

If there are not curly brackets after the if statement, it will only execute (exactly) the next line of code when the condition is true.  The program will then execute the line of code after that regardless of the condition's truth value.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: If more than one line of code is to be executed when the if condition is true, curly brackets must be put around them.

question image

Your Answer

Correct Answer

True

Explanation:

If there are not curly brackets after the if statement, it will only execute (exactly) the next line of code when the condition is true.  The program will then execute the line of code after that regardless of the condition's truth value.

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: Once the program is running, the value of tempthreshold can be changed.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: Once the program is running, the value of tempthreshold can be changed.

question image

Correct Answer

False

Explanation:

tempthreshold is a constant in this program, so its value cannot be changed. 

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: Once the program is running, the value of tempthreshold can be changed.

question image

Your Answer

Correct Answer

False

Explanation:

tempthreshold is a constant in this program, so its value cannot be changed. 

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: If more than one line of code is to be executed when the else condition is true, curly brackets must be put around them.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: If more than one line of code is to be executed when the else condition is true, curly brackets must be put around them.

question image

Correct Answer

True

Explanation:

Just like the if case, the else case also only executes (exactly) the next line of code by default.  If you want it to execute more lines then that, you must use curly brackets.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: If more than one line of code is to be executed when the else condition is true, curly brackets must be put around them.

question image

Your Answer

Correct Answer

True

Explanation:

Just like the if case, the else case also only executes (exactly) the next line of code by default.  If you want it to execute more lines then that, you must use curly brackets.

 Next Question
Question 1 of 10

Conditional If Else Statements

True or False: The code in both the if and the else portions can be executed in the same pass of the program run.

Question Image
Question 1 of 10

Conditional If Else Statements

Correct!

True or False: The code in both the if and the else portions can be executed in the same pass of the program run.

question image

Correct Answer

False

Explanation:

Only one of the if or else cases can be executed in one pass.  If the if condition is true, the if code will be executed.  Otherwise, the else code will be executed.

 Next Question
Question 1 of 10

Conditional If Else Statements

Incorrect

True or False: The code in both the if and the else portions can be executed in the same pass of the program run.

question image

Your Answer

Correct Answer

False

Explanation:

Only one of the if or else cases can be executed in one pass.  If the if condition is true, the if code will be executed.  Otherwise, the else code will be executed.

 Next Question
Question 1 of 10

Conditional If Else Statements

Correct!

What do the following lines of code do?

if (analogValue == tempthreshold)
   digitalWrite(alarmPin, HIGH);

question image

Correct Answer

Check if the value of analogValue is equal to tempthreshold and, if they are equal, turn the alarm on.

Explanation:

The two equal signs check equality, so this statement sees if analogValue is equal to tempthreshold and, if so, turns the alarm on, digitalWrite(alarmPin, HIGH);.

 Finish
Question 1 of 10

Conditional If Else Statements

Incorrect

What do the following lines of code do?

if (analogValue == tempthreshold)
   digitalWrite(alarmPin, HIGH);

question image

Your Answer

Correct Answer

Check if the value of analogValue is equal to tempthreshold and, if they are equal, turn the alarm on.

Explanation:

The two equal signs check equality, so this statement sees if analogValue is equal to tempthreshold and, if so, turns the alarm on, digitalWrite(alarmPin, HIGH);.

 Finish
Question 1 of 10
Conditional If Else Statements

Score

You have answered 5 of 10 questions correctly.

50%

Start Over

See other flashcards and apps related to:

Question 1 of 10
Author
Published
6/2/2015
Last Updated
6/2/2015
Tags

No Comments Yet

Creative Commons License
Conditional If Else Statements by Fox Valley Technical College is licensed under a Creative Commons Attribution 3.0 Unported License.