switch বনাম if

 switch vs if
আমরা কোডিং করার সময়(in Pascal, Ada, C/C++, C#, Java etc. ল্যাঙ্গুয়েজ) কন্ডিশন চেকিং নিয়ে কাজ করতে if - else কিংবা switch - case ব্যবহার করে থাকি। কিন্তু আপনি কি জানেন উভয়ই কন্ডিশন চেকের জন্য ব্যবহার করা হলেও switch - case এর ব্যবহার if - else এর চেয়ে বেশী ইফিসিয়েন্ট ??

switch - case কন্ডিশন চেকিং এর জন্য মূলত branch table কিংবা binary search(লগারিদমিক টাইম কম্পেক্সিটি) ব্যবহার করে যেখানে if - else এর মাধ্যমে কন্ডিশন চেক করলে কন্ডিশন ট্রু না হওয়া পর্যন্ত প্রতিটি কন্ডিশন একের পর এক চেক হতেই থাকে !!!
branch table অনেকটা hash table এর মত। একারনে যখন
switch(x){
........
........
}
এভাবে switch ব্যবহার করা হয় তখন x এর জন্য প্রোগ্রামিং কন্ট্রোল যে case এর জন্য ট্রু হয় হয় সেই কেজ এ সরাসরি ট্রান্সফার হয়ে যায়(অন্য কোন case চেক না করেই) এবং ঐ case এর আওতাধীন কোড কাজ করে।
যদিওবা অল্প সংখ্যক কন্ডিশন চেকিং এর ক্ষেত্রে switch - case অথবা if - else ব্যবহারে এক্সিকিউশন টাইমের খুব একটা হেরফের হয় না।
তবে যে বিষয়টি অধিক গুরুত্বপূর্ণ সেটি হল কোডের রিডঅ্যাবিলিটি(readability)। প্রোগ্রামাররা if-else এর পরিবর্তে switch-case এর ব্যবহারকেই বেশি উৎসাহ প্রদান করে থাকেন। কেননা if-else এর চেয়ে switch-case যুক্ত কোড পড়া অনেকটা স্বাচ্ছন্দের।

Comments

  1. Acronis International
    Firstly it would not be possible for every single BO or company owner (eCosway member) to have their own shop. After all, running an internet business will involve numerous computer related duties. Generally, this company doesn’t have anything to get discussed by other people in concern to have best and trustworthy printers support to become their printers work well and to make sure its customers obtain their printer tech error-free. MLM companies, however, usually just have a concise description on their company on the front page and then you have to be a member to see the remaining portion of the website.

    ReplyDelete

Post a Comment