Home  >  Article  >  Backend Development  >  Conditions (If-else-elif) Part-08

Conditions (If-else-elif) Part-08

王林
王林Original
2024-09-04 14:32:24570browse

Conditions (If-else-elif) Part-08

When I was younger, I used to go home late in the evening. almost every day My mother doesn't like this stuff. One day he gave a warning.

“If you don't come home before evening from tomorrow, then there is news for you.”

“Okay okay.”

“I told you not tomorrow, I will come home before evening. Any news on what time it is in the evening”

“And if I come on time”

“Then I will give you 2 boiled eggs to eat”

Next day evening

Long-term practice goes so easily. Then Uttam Madhyam began. It's been like this for a while. No work done.

Then my mother called me one day and said

“Listen I've made a decision.”

“What decision”. I'm a little worried. I don't know what to say.

“From tomorrow if you come home within 5 minutes of evening then you will be given 2 cakes for 5 rupees.”

I am happy to hear that.

“No need to be so happy. And if you are there within 15 minutes, then I will get 1 cake"

“Wow good”

“There is one more thing. If you can't do any of the above, you won't get the cake. Eat and drink at night with you."

“This is not going well.”

“Right or wrong I will understand. I have given you options, I will do whatever you like"

Do you understand anything from here? I have sadness on my forehead. If you do not act according to the conditions, it will remain. In the case of employment, jobs may also be lost.

If you notice

here, you will understand that

If you notice in the first scene, then you will notice that there are 2 tasks here, but any one will do. Which work will be done depends on one condition (returning home before evening).

Because I could not return home on time that day. In my case, the first job is tied. Arki gave good middle.

This is the same thing in Python. Notice the code below

if condition:
    # condition সত্য হলে এই নিচের কোড রান হবে
    # expression
else:
    # condition সত্য না হলে বা মিথ্যা হলে এই নিচের কোড রান হবে
    # expression

Here if is the Python keyword. After if there will be a condition and based on this condition it will be determined which block of code will be run. If the condition is true, the first block will run and if it is not true, it will go to the next else block. You can understand only by running the following code.

if 1==1: # ১ আর ১ সমান
    print('Condition is true')
else:
    print('Condition is false')

বিঃদ্রঃ পাইথন অপারেটরের মাধ্যমে এখানে condition চেক করতে হবে।

এবার আসুন দ্বিতীয় দৃশ্যে

এখানে কিন্তু ২ টা condition আছে। এখন কি করবেন? খুব সহজ। আপনি একটু ভাবলেই পেরে যাবেন। আগে একটু ভাবেন।

এখানে প্রথম condition এর জন্য উপরে মত করে if else ব্যবহার করব। তারপর else ব্লক এর ভিতরে ২য় condition এর জন্য if else লিখবে। confusing লাগছে? নিচের কোড টা দেখলে বুঝতে পারবেন।

if 1==4: # 1st layer condition
    print('1st Condition is true')
else:
    if 2*3 == 6: # 2nd layer condition
        print('2nd Condition is true')
    else: 
        print('No Condition is true')

উপরে কোড কে পরিবর্তন করে নিজের মত করে কোন condition দেখতে পারেন।

কাজঃ আপনার এই if else ব্যবহার করে একটা Marking Grading System বানিয়ে ফেলুন। আর কোডের screenshot কমেন্ট এ শেয়ার করুন।

উপরে কোডে লক্ষ্য করলে দেখবেন এখানে 1st layer condition চেক করা পর 2nd layer condition এর চেক করছে । এখানে চাইলে একটু ভালোভাবে কোড লেখা যায়। নিচের কোড টা দেখুন

if 1==4:
    print('1st Condition is true')
elif 2*3 == 6:
    print('2nd Condition is true')
else: 
    print('No Condition is true')

এর আগের লেখা কোড আর এই কোডের আউটপুট একই আসবে। কিন্তু এই কোড আগের কোডের থেকে সহজে বোঝা যায়। পরবর্তীতে আমাদের সময় বাচাবে।

এখানে শুধু একটা নতুন condition add করেছি elif কিওয়ার্ড দিয়ে। এখানে খেয়াল করলে দেখবেন এখানে কিন্তু কোডের লেয়ার একটা কমে গেছে

কাজঃ এবার আপনার if else ব্যবহার করে Marking Grading System কে elif এ convert করে ফেলুন । আর কোডের screenshot কমেন্ট এ শেয়ার করুন।

আজ এই পর্যন্ত। এতক্ষন সাথে থাকার জন্য ধন্যবাদ। আপনার মতামত অবশ্যই জানাতে ভুলবেন না। কমেন্ট এ যদি বলতে সমস্যা হয় তাহলে আমার inbox তো খোলায় আছে।

The above is the detailed content of Conditions (If-else-elif) Part-08. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn