Control Flow in R and IF Statements Module

How does this code accounts for ages not over 21?

But if age is 18+ but not 21+, you have to check whether the current_hour is earlier than 11. If it is, you can set access_event to TRUE and you should print "I can stay until 11!“ Hint: you can either nest the first check into the second, or use an & (AND) operator to combine them.

If age is not 18+ or 21+, both access_event and access_music_show should stay FALSE and you should print “I guess I’ll go somewhere else.”

write your conditionals code here:

if (age >= 21) {
access_event <- TRUE
print(“I got into the event for the night!”)
if (possible_codeword == real_codeword) {
access_music_show <- TRUE
print(“I need to get a good spot!”)
}
} else if (age >= 18 & current_hour < 11) {
access_event <- TRUE
print(“I can stay until 11!”)
} else {
print(“I guess I’ll go somewhere else.”)
}

Hello @codezena2452328029, welcome to the forums!

This code checks to see if the age is greater than or equal to, 21. Since it gets evaluated first, and the other control statements that are on its nesting level are not if, if this line is TRUE, none of these lines will run:

However, if the age is less than 21, the else if line evaluates. This will only check its condition if this if is FALSE:

Lastly, if neither the if or else if condition (the bit inside the parentheses-if (condition)), the else runs.

I hope this helps!

2 Likes

As a follow up to this message, can I ask why there are no additional else statements where I have typed HERE in the code copied from above?

if (age >= 21) {
access_event <- TRUE
print(“I got into the event for the night!”)
if (possible_codeword == real_codeword) {
access_music_show <- TRUE
print(“I need to get a good spot!”)
} HERE
} else if (age >= 18 & current_hour < 11) {
access_event <- TRUE
print(“I can stay until 11!”)
} else {
print(“I guess I’ll go somewhere else.”)
} HERE

I’m struggling with this one and the module is really buggy, can’t even check my code in the web version of r used in the lessons as it won’t work!

Hello @ruby2070270826, welcome to the forums! I don’t quite understand your question. Is there any reason for there to be more logic and else if blocks?

Hello. Thank you for your reply. Problem with this is that this is my very first time coding and suddenly the programming section seems to jump with no explanations as to why something works and something else doesn’t and introduces new things such as empty brackets after c in a code and doesn’t explain things. Am finding it very frustrating! The above example, I looked at what was an example and what was supposed to form part of the syntax. At no point did it say it is OK for some bits to be missing relative to the examples provided. I get that bit now. Lack of a tutor is holding me up to bounce questions off on this section! Doesn’t help that there are no answers to work through if unsure. I mostly want r for cleansing data, analysing stats and to produce plots. Am interested in other bits, but lack of other experience in this is causing issues. Anyhow - thank you so much for responding.

1 Like

If you’ve got any other questions, feel free to use the forums!

2 Likes