FAQ: Introduction to R Syntax - Conditionals

This community-built FAQ covers the “Conditionals” exercise from the lesson “Introduction to R Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn R

FAQs on the exercise Conditionals

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I am confused by the IF/ElSE exercise. What exactly is being evaluated here?
I would expect IF/ELSE to be more along the lines of
Variable1 = 2
Variable2 = 3

If Variable1 > Variable2: do something
Else: do something different.

So what exactly are we evaluating to see whether or not it is true?

message <- "I change based on a condition."
if (TRUE) {
  message <- "I execute this when true!"
} else {
  message <- "I execute this when false!"
}
print(message)
9 Likes

You did nothing wrong. It’s very buggy/messed up. You have the same solution as the answer

1 Like

How do I find the curly brackets?

I was stuck on this questions as well. I believe the difference is single quotes vs double quotes surrounding each message. (Double quotes give an error?)

Probably an obvious question, but it does not say in the cheat sheet or on the exercise, but do conditionals only work with ‘logical’ data types.

For example, could the if/else statements be dependent on ‘numerical’ or ‘character’ data as well?

i.e. if(x>10) {print(‘x is greater than 10’)} else {print(‘X is not greater than 10’)}

this is cofusing.

if (true) … if what is true? there is no condition here. Is this testing if the variable exists?

Shouldn’t this be

if (message = “I execute this when false!”) {message <- “I execute when true!”}
else {message <- “I execute this when false!”}

4 Likes

I learn better when I understand the fundamentals of the evaluation. What is being evaluated as true or false in this situation?

It was easy to pass the lesson but it feels like I am only learning the R syntax and not the logic behind R. I hope this will go into more detail in later sections.

3 Likes

I used double quotes and passed the exercise with no problems.

@text9311646494 @danielsinclair030878 @jmac32935
yes, the logic behind simply typing TRUE into an if statement (which takes an evaluation of a logical statement) isn’t fully explained here. But in other programing languages true always evaluates to being true, it will never print the else statement. hope this helps!

So to understand the exercise correctly, there would not be a situation in which the “else”-statement would be printed the way it is coded?

And how would you go about adding the condition that needs to evaluated as either true or false?

1 Like

Single/double quotes did not matter to me, but changing the spacing so else is on the same line as the end curly braces and an extra empty line between the end curly brace and print statement passed it for me. Is R white-space sensitive or is this just Codecademy’s grading system being wonky for this exercise?

1 Like

Hi! I ended the else line with }; as I usually do in SQL and I passed but I would say the space is more correct in R.

Answer:
message <- “I change based on a condition.”
if (TRUE) {
message <- “I execute this when true!”
} else {
message <- “I execute this when false!”
}

print(message)

2 Likes

Second this answer. Thanks for sharing

Hello People. I was also annoyed., But then I tried to replace the TRUE with something else.

I created to variables. One x <- 5 and the other one y <- 4

First I evaluated if x was bigger than y.


It printed: I execute this when true!

And then if x was smaller than y


it printed: I execute this when false!

I hope that this helps.

1 Like

@davidgarcia719776693 @text9311646494
Basically a logical / boolean is an unassigned variable that has an initialized value. In some languages it is initialized as FALSE and in some, like R, it is initialized as TRUE. To change the value you have to explicitly assign it to a variable container and state the value.

What an awful lesson!
How do they not give an example of something like this? lmao it’s almost comical.
They reference a condition in the parentheses and then just have a boolean T/F in the parentheses.
That is NOT a condition, that is the RESULT of a condition.
Glad I tried the free account before paying money for this garbage. I figured this lesson out by Googling and stumbling across a FREE site. So you obviously don’t always get what you pay for.

1 Like

message ← “I change based on a condition.”
if (TRUE) {
message ← “I execute this when true!”
} else {
message ← “I execute this when false!”
}
print(message)

Shouln´t it just execute the else statement because it is defined as false?

message ← FALSE
if (TRUE) {
message ← “I execute this when true!”
} else {
message ← “I execute this when false!”
}
print(message)

Just a short heads up:

You do not need to write a full program, only the IF / ELSE statement, it will be run by the system passing a TRUE and a FALSE value and validating the result.

Curly braces, on an US keyboard are on top of the square braces =, next to the P key and left of the RETURN key.

:wink:

I had this same question! Changing the spacing, and line breaks, passed the exercise for me.