Codegym यूनिवर्सिटी कोर्स के भाग के रूप में एक परामर्शदाता के साथ एक व्याख्यान स्निपेट। पूर्ण पाठ्यक्रम के लिए साइन अप करें।


"हाय, अमीगो। आज हम if/else कथनों के बारे में बात करेंगे ।"

"कार्यक्रम बहुत कम उपयोगी होंगे यदि वे बाहरी परिस्थितियों को बदलने का जवाब नहीं देते हैं। एक कार्यक्रम को यह जानने की जरूरत है कि परिस्थितियों को कैसे अनुकूलित किया जाए और एक मामले में एक कार्रवाई करें और अन्य मामलों में अन्य कार्रवाई करें। जावा में, इसका उपयोग करके हासिल किया जाता है। 'if/else Statement' - एक विशेष निर्माण जो एक शर्त पूरी होने पर अलग-अलग कोड ब्लॉक करना संभव बनाता है।"

"इसमें तीन भाग होते हैं: ' कंडीशन ', ' कमांड 1 ' और ' कमांड 2 '। यदि शर्त सही है, तो ' कमांड 1 ' निष्पादित किया जाता है, अन्यथा 'कमांड 2' निष्पादित किया जाता है। इन आदेशों को कभी भी निष्पादित नहीं किया जाता है। बयान कमोबेश इस तरह दिखता है:"

if/else स्टेटमेंट के लिए कोड
if (condition)
    command_1;
else
    command_2;

"कितना रोमांचक! मुझे लगता है कि यह कथन प्रोग्रामिंग को और अधिक रोचक बना देगा!"

"हां। यहां आपके लिए कुछ उदाहरण दिए गए हैं:"

कोड व्याख्या
1
if (a < b)
    System.out.println("A is less than B");
else
    System.out.println("B is less than  A");
यदि a, b से कम है, तो पहली कमांड निष्पादित होगी। अन्यथा दूसरा आदेश निष्पादित किया जाएगाआदेश दोनों कभी निष्पादित नहीं होते हैं।
2
if (a < b)
{
    System.out.println("A is less than B");
    System.out.println("B is greater than A");
}
else
{
     System.out.println("B is less than A");
     System.out.println("A is greater than B");
}
आप एक कमांड को कोड ब्लॉक से बदल सकते हैं। बाकी वही है।
3
if (a < b)
{
    a = 0;
}
else
{
}
यदि यह खाली है तो आप और ब्लॉक को छोड़ सकते हैं।
ये तीन उदाहरण पूरी तरह समकक्ष हैं।
यदि आपको केवल एक कमांड निष्पादित करने की आवश्यकता है तो आप कर्ली ब्रैकेट को छोड़ सकते हैं। यदि आपके पास एक से अधिक आदेश हैं, तो आपको कोष्ठक रखने की आवश्यकता है।
4
if (a < b)
{
    a = 0;
}
5
if (a < b)
    a = 0;

"डिएगो ने अभी-अभी मुझे तुम्हें कुछ कार्य देने के लिए कहा है।"

2
टास्क
Java Syntax,  स्तर 4सबक 4
लॉक
Good or bad?
Student robot Peter is an overachiever. Previously, his server was configured to receive scores on a five-point scale, but now his teachers have switched to a 12-point scale. But Peter doesn't know this. He's still focused on getting fives. Let's write him a compare method that compares any number with five.
4
टास्क
Java Syntax,  स्तर 4सबक 4
लॉक
Closest to 10
Ten is extremely popular and attractive number. Everyone wants to be a ten. Or at least as close to it as possible. Two numbers are standing around wondering which of them is cooler. Answer: whichever is closer to ten. Let's write these numbers a displayClosestToTen method that will determine which of them is cooler.
4
टास्क
Java Syntax,  स्तर 4सबक 4
लॉक
Come on, lucky seven!
Dice games are popular on the planet Foggy Multidimensions. The rules are different than the Earth version: Multidimensionals perceive far more dimensions than primitive three-dimensional earthlings. Their die has 4294967295 faces. Players win only if they roll a number between 50 and 100. We'll write a method that checks whether the die roll is in this range.
4
टास्क
Java Syntax,  स्तर 4सबक 4
लॉक
Seasons on Terra
An Earth year consists of four seasons, each of which lasts 3 months. While our ship was parked on this cradle of humanity, the Interplanetary Tax Service asked us to write a program to determine the season based on a number corresponding to the month of the year. We don't know why they want it. They say that it's none of our business. But they promised not to remain in our debt.
4
टास्क
Java Syntax,  स्तर 4सबक 4
लॉक
Positive and negative numbers
Diego is tall, but Alfredo is short. Rishi is experienced, but you're a "noob" programmer. Comparisons are unavoidable in life. And the same thing is true in programs. So we'll continue practicing comparisons and displaying stuff on the screen. This time let's compare the entered number with zero and manipulate it based on the result of the comparison.
4
टास्क
Java Syntax,  स्तर 4सबक 4
लॉक
Day of the week
Planet Terra still has "offices"—an outdated type of workspace. With current technology, there is no need for them, but earthlings tend to be nostalgic and haven't been in a hurry to eradicate them. Terrestrial office workers develop "TGIF syndrome": they constantly want to know what day of the week it is. Let's write a program for them!

Codegym यूनिवर्सिटी कोर्स के भाग के रूप में एक परामर्शदाता के साथ एक व्याख्यान स्निपेट। पूर्ण पाठ्यक्रम के लिए साइन अप करें।