थोड़ा और कस्टमाइज़ करें: else if
else if स्टेटमेंट आपको अपनी control structure को और भी कस्टमाइज़ करने देता है। आप अपनी जरूरत के मुताबिक जितने चाहें उतने else if स्टेटमेंट जोड़ सकते हैं। ध्यान रखें कि जैसे ही कोई कंडीशन TRUE मिलती है और उससे जुड़ी expressions चल जाती हैं, R बाकी control structure को अनदेखा कर देता है। आपकी याद ताज़ा करने के लिए सिंटैक्स का एक ओवरव्यू यहाँ दिया है:
if (condition1) {
expr1
} else if (condition2) {
expr2
} else if (condition3) {
expr3
} else {
expr4
}
फिर से ध्यान दें: else if कीवर्ड्स पिछले हिस्से के क्लोज़िंग ब्रैकेट वाली उसी लाइन पर होने चाहिए!
यह अभ्यास पाठ्यक्रम का हिस्सा है
इंटरमीडिएट R
अभ्यास निर्देश
दोनों control structures में कोड ऐसे जोड़ें कि:
- यदि
medium"Facebook" के बराबर हो, तो R "Showing Facebook information" प्रिंट करे। याद रखें कि R case sensitive है! - यदि
num_views15 (inclusive) और 10 (exclusive) के बीच हो, तो "Your number of views is average" प्रिंट हो। वैरिएबल्सmediumऔरnum_viewsबदलने के लिए स्वतंत्र हैं ताकि आप देख सकें control structure कैसे प्रतिक्रिया देता है। दोनों मामलों में, मौजूदा कोड कोelse ifस्टेटमेंट में ही बढ़ाएँ। किसी मौजूदा कोड को संशोधित न करें।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Variables related to your last day of recordings
medium <- "LinkedIn"
num_views <- 14
# Control structure for medium
if (medium == "LinkedIn") {
print("Showing LinkedIn information")
} else if (medium == "Facebook") {
# Add code to print correct string when condition is TRUE
} else {
print("Unknown medium")
}
# Control structure for num_views
if (num_views > 15) {
print("You're popular!")
} else if (num_views <= 15 & num_views > 10) {
# Add code to print correct string when condition is TRUE
} else {
print("Try to be more visible!")
}