Branching: if else-if, switch

শাখা: যদি অন্যথা- যদি, সলনি কৰক

Let us first have a look in the C program as shown is Example 9.4. The program compares two numbers entered by a user from the keyboard do check whether one number is greater then the other, smaller then the other or both the numbers are equal. When we run the program, we see that depending on the value we enter from the keyboard, we see output from only one printf() statement (from the last three printf()statements.

প্ৰথমে আমি চি প্ৰ’গ্ৰামত এবাৰ চাওঁ আহক যেনেকৈ দেখুওৱা হৈছে উদাহৰণ 9.4। প্ৰ’গ্ৰামটোৱে কীবৰ্ডৰ পৰা এজন ব্যৱহাৰকাৰীয়ে প্ৰবিষ্ট কৰা দুটা সংখ্যাতুলনা কৰে এটা সংখ্যা ডাঙৰ হয় নে নাই পৰীক্ষা কৰক তাৰ পিছত আনটো, সৰু তাৰ পিছত আনটো বা দুয়োটা সংখ্যা সমান। যেতিয়া আমি প্ৰ’গ্ৰামটো চলাওঁ, আমি দেখিছোঁ যে আমি কীবৰ্ডৰ পৰা প্ৰবিষ্ট কৰা মূল্যৰ ওপৰত নিৰ্ভৰ কৰি, আমি কেৱল এটা প্ৰিণ্টফ() বিবৃতিৰ পৰা (অন্তিম তিনিটা প্ৰিণ্টফ()বিবৃতিৰ পৰা আউটপুট দেখিবলৈ পাওঁ।

This means that out of three print statements, only one executes. We achieved this using a if statement. Whenever the condition inside if() is true, the enclosed statement of the if executes.

ইয়াৰ অৰ্থ হৈছে যে তিনিটা প্ৰিণ্ট বিবৃতিৰ ভিতৰত, কেৱল এটাকাৰ্যকৰী হয়। আমি এইটো এটা যদি বিবৃতি ব্যৱহাৰ কৰি প্ৰাপ্ত কৰিছিলো। যেতিয়াই ভিতৰৰ অৱস্থা টো সঁচা হয়, যদি কাৰ্যকৰী হয় তেনেহ’লে আৱদ্ধ বিবৃতি।

Whenever we want to enclose more than one statement with an if, we put the statements inside two curly braces. See the following code segment below.

যেতিয়াই আমি এটাতকৈ অধিক বিবৃতি এটাৰ সৈতে সংলগ্ন কৰিব বিচাৰো, আমি বিবৃতিবোৰ দুটা কোঁকড়া ব্ৰেছৰ ভিতৰত ৰাখিম। তলত নিম্নলিখিত কোড খণ্ডটো চাওক।

There is another clause called else in C. Whenver only one condition will be true out of a set of a set of conditions, we may put them using if – else or if-else if-else construct. The last three if statements of Example 9.4 can be replaced with the following set of statements.

চি-ত আন এটা ধাৰা কোৱা হয়। যেতিয়াই চৰ্তৰ এটা সংহতিৰ পৰা কেৱল এটা চৰ্ত সঁচা হ’ব, আমি সেইবোৰ ব্যৱহাৰ কৰিব পাৰোঁ যদি – অন্যথা বা অন্য যদি- অন্য কোনো নিৰ্মাণ হয়। অন্তিম তিনিটা যদি উদাহৰণ 9.4-ৰ বিবৃতিবোৰ নিম্নলিখিত বিবৃতিৰ সৈতে সলনি কৰিব পাৰি।

We now combine the if clause with the logical AND operator in another program as shown in Example 9.5. The program checks whether the first number is the largest one among three given numbers.

উদাহৰণ 9.5-ত দেখুওৱাৰ দৰে আমি এতিয়া আন এটা প্ৰ’গ্ৰামত যুক্তিসঙ্গত আৰু অপাৰেটৰৰ সৈতে যদি ধাৰাটো একত্ৰিত কৰোঁ। প্ৰ’গ্ৰামটোৱে পৰীক্ষা কৰে যে প্ৰথম সংখ্যাটো তিনিটা প্ৰদত্ত সংখ্যাৰ ভিতৰত আটাইতকৈ ডাঙৰ।

In this example, we first saw that we can accept inputs to more than one variable using scanf() function. Then we saw using logical AND operator in between two expressions. The if statement in the program checks whether num1is larger than both num 2 and num 3. if yes, the print statement is executed.

এই উদাহৰণত, আমি প্ৰথমে দেখিছিলো যে আমি স্কেনফ() ফাংচন ব্যৱহাৰ কৰি একাধিক চলকত ইনপুট গ্ৰহণ কৰিব পাৰোঁ। তেতিয়া আমি দুটা অভিব্যক্তিৰ মাজত যৌক্তিক আৰু অপাৰেটৰ ব্যৱহাৰ কৰা দেখিছিলো। যদি প্ৰ’গ্ৰামৰ বিবৃতিটোৱে সংখ্যা 2 আৰু সংখ্যা 3 দুয়োটাতকৈ সংখ্যা1ডাঙৰ হয় নে নাই পৰীক্ষা কৰে। যদি হয়, প্ৰিণ্ট বিবৃতিটো কাৰ্যকৰী কৰা হয়।