C Programming

Conditional Operator in C Programming

  1. Conditional Operator: একটা condition দিয়ে দুটি মান select করার একটা পদ্ধতি। এটি নিচের মতো করে লেখা হয়ঃ

Expression1? Expression2: Expression3


মনে করি i=5, তাহলে নিচের Conditional Operator টা দেখিঃ


এখানে Z এর জন্য Conditional Operator টা লেখা হয়েছে। এখানে লিখা হয়েছেঃ Z=(i<8)?10:100; অর্থাত যদি i এর মান 8 থেকে ছোট হয় তাহলে Z এর মান হবে 10। আর তা না হলে z এর মান হবে 100. আমি নিচের প্রোগ্রামে সব কিছু বুঝানোর চেষ্টা করছিঃ if-else statement এর পরিবর্তে Conditional Operator (?:) ব্যবহার করে সহজেই দুইটি statement অথবা valu এর মধ্যে তুলনা করে একটি মান নির্বাচিত করা যায়। Conditional Operator সি প্রোগ্রামিং এ নিচের মত করে লেখা হয়ঃ condition ? first_expression : second_expression; এখানে condition হচ্ছে যে কোন একটা শর্ত। যা সত্য হলে first_expression নির্বাচিত হবে। আর কন্ডিশন ভুল হলে second_expression। নিচে ছোট্ট একটা প্রোগ্রাম। যা দিয়ে দুটি সংখ্যার মধ্যে বড়টা নির্বাচিত করা হয়েছে।

#include <stdio.h>
int main()
    int x, y , result;
    scanf("%d %d", &x , &y);
    result = (x>=y) ? x : y ;
    printf("max is %d", result);
    return 0;

একই প্রোগ্রাম, কন্ডিশন পরিবর্তন করে দুটি সংখ্যার মধ্যে ছোটটা নির্বাচিত করা হয়েছে।

#include <stdio.h>

int main()
    int x, y , result;
    scanf("%d %d", &x , &y);
    result = (x<=y) ? x : y ;
    printf("min is %d", result);
    return 0;

যদিও একই কাজ if -else বা অন্য অনেক ভাবে করা যায়।

24 thoughts on “Conditional Operator in C Programming

  1. Excellent blog! Do you have any hints for aspiring writers? I’m hoping to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely overwhelmed .. Any ideas? Thanks a lot!

  2. Fantastic goods from you, man. I have understand your stuff previous to and you are just too excellent. I actually like what you have acquired here, really like what you’re saying and the way in which you say it. You make it enjoyable and you still care for to keep it smart. I can not wait to read much more from you. This is really a terrific site.

  3. Good site! I really love how it is easy on my eyes and the data are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your RSS which must do the trick! Have a nice day!

  4. Hi would you mind sharing which blog platform you’re working with? I’m looking to start my own blog soon but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique. P.S Apologies for getting off-topic but I had to ask!

  5. I definitely wanted to type a small note so as to express gratitude to you for some of the pleasant pointers you are sharing on this website. My extensive internet research has now been recognized with really good tips to share with my good friends. I would tell you that many of us site visitors actually are unequivocally lucky to exist in a wonderful network with so many lovely individuals with beneficial tips. I feel really privileged to have discovered the site and look forward to some more excellent minutes reading here. Thanks a lot once again for a lot of things.

  6. I like the valuable information you provide in your
    I will bookmark your weblog and check again
    here regularly. I’m quite sure I will learn lots of new
    stuff right here! Best of luck for the next!

  7. There are some interesting points in time in this article but I don?t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

  8. I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  9. We are a bunch of
    volunteers and starting a brand new scheme in our community.

    Your site offered us with useful info to
    work on. you’ve performed an impressive
    task and our entire neighborhood will be grateful to you.

  10. An impressive share, I just given this onto a colleague who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post!

Leave a Reply

Your email address will not be published. Required fields are marked *