0
0
R Programmingprogramming~15 mins

Why control flow directs execution in R Programming - See It in Action

Choose your learning style9 modes available
Why control flow directs execution
📖 Scenario: Imagine you are organizing a small event and need to decide what to do based on the weather forecast.
🎯 Goal: You will write a simple R program that uses control flow to decide what activity to do depending on the weather.
📋 What You'll Learn
Create a variable called weather with the value "sunny"
Create a variable called activity and set it to an empty string
Use an if statement to set activity to "Go for a walk" if weather is "sunny"
Use an else if statement to set activity to "Read a book indoors" if weather is "rainy"
Use an else statement to set activity to "Stay home and relax" for any other weather
Print the value of activity
💡 Why This Matters
🌍 Real World
Control flow is used in real life to make decisions, like choosing what to do based on the weather or other conditions.
💼 Career
Understanding control flow is essential for programming jobs because it allows software to react differently depending on inputs or situations.
Progress0 / 4 steps
1
Create the weather variable
Create a variable called weather and set it to the string "sunny".
R Programming
Need a hint?

Use the assignment operator <- to assign the value "sunny" to weather.

2
Create the activity variable
Create a variable called activity and set it to an empty string "".
R Programming
Need a hint?

Assign an empty string "" to the variable activity.

3
Use if-else to decide the activity
Use an if statement with the condition weather == "sunny" to set activity to "Go for a walk". Then use else if with weather == "rainy" to set activity to "Read a book indoors". Finally, use else to set activity to "Stay home and relax".
R Programming
Need a hint?

Use if, else if, and else blocks to check the value of weather and assign the correct string to activity.

4
Print the activity
Write a print() statement to display the value of the variable activity.
R Programming
Need a hint?

Use print(activity) to show the chosen activity.