সি প্রোগ্রামিংঃ if – else

if

যদি কোন কিছু সত্য হয়, তাহলে একটা কিছু কোড রান করার জন্য if স্ট্যাটমেন্ট ব্যবহার করা হয়। এটি নিচের মত করে লেখা হয়ঃ
If (expression) statement.
এখানে expression মানে যে কোন লজিক্যাল বা রিলেশনাল অপারেশন ব্যবহার করা হয়। অপারেটর অধ্যায়ের রিলেশনাল এবং লজিক্যাল সেকশনে আমরা যা শিখেছি, এখন তা কাজে লাগবে। যেমনঃ

#include <stdio.h>
int main(void)
  
{
    int temperature = 35;
 
    if (temperature > 30){
            printf("Todays weather is too hot.");
}
  return 0;
} 
copy C Code & Paste it below the Compiler & Run

আজকের আবহাওয়া গরম না ঠান্ডা, তা বের করার একটা প্রোগ্রাম লিখছি। যেখানে temperature নামে একটা ইন্টিজার ভ্যারিয়েবল নিয়েছি এবং যার ভ্যালু এসাইন করেছি ৩৫; এরপর আমরা if দিয়ে temperature টা ৩০ এর সাথে মিলিয়ে দেখেছি। দেখেছি আজকের temperature কি ৩০ থেকে বড় কিনা। মিলিয়ে দেখার জন্য আমরা greater then লজিক্যাল অপারেটর ব্যবহার করেছি।
যেহেতু temperature ৩০ থেকে বড়, তাই if এর ভেতরে থাকা কোডটি মানে printf টি কাজ করেছে। যদি temperature 30 থেকে ছোট হত, তাহলে প্রোগ্রামটি কোন আউটপুটই দিত না। নিচের প্রোগ্রামটি দেখি, যেখানে temperature ভ্যারিয়েবলটির ভ্যালু হচ্ছে 25;

#include <stdio.h>
int main(void) 
{
    int temperature = 25;
 
    if (temperature > 30){
            printf("Todays weather is too hot.");
}
    return 0;
}
copy C Code & Paste it below the Compiler & Run

প্রোগ্রামটি রান করে দেখলে কনসোলে আমরা কিছুই দেখতে পাবো না। কারণ if দিয়ে দেখেছি temperature এর মান 30 থেকে বড় কিনা। যেহেতু temperature এর মান 30 থেকে বড় না, তাই if এর ভেতরে থাকা কোড গুলো রান হবে না।
এখন একটা প্রোগ্রাম লিখব, যেটা ব্যবহারকারী থেকে temperature ভ্যালুটা নিবে। এরপর কনসোলে একটা মেসেজ দেখাবেঃ

#include <stdio.h>
int main(void)
 
{
    int temperature;
 
    printf("Enter temperature  value:");
    scanf("%d", &temperature );
 
    if (temperature > 30)
    printf("Todays weather is too hot.");
 
    if (temperature <30){
        printf("Todays weather is cool.");
    }
 
    return 0;
}
copy C Code & Paste it below the Compiler & Run

এখানে প্রথমে ইউজার থেকে temperature নিয়েছি। একটা if ব্যবহার করে চেক করেছি যদি temperature 30 থেকে বেশি হয়, তাহলে কনসোলে আউটপুট দিবে Todays weather is too hot. আর যদি temperature 30 থেকে কম হয় তাহলে আউটপুট দিবে Todays weather is cool।

If এর ভেতর আমরা চাইলে একের অধিক স্ট্যাটমেন্ট লিখতে পারি। আবার একটা if এর ভেতর আরেকটা if ব্যবহার করতে পারি। যাকে বলা হয় nested if।

if – else

একটা লজিক্যাল টেস্ট যদি সত্য হয়, তাহলে কিছু কাজ করবে। যদি মিথ্যে হয়, তাহলে অন্য কাজ। এ লজিক থেকেই if else. যেমনঃ যদি আজ শুক্রবার হয়, ঘুমাবো। না হলে ব্যাগ গুছিয়ে স্কুল/কলেজে যাবো। এমন ধরনের “যদি, তা নাহলে” থেকেই if – else।

if(expression) statement 1
else statement2;

এখানে এই statement দিয়ে বুঝানো হয়, যদি Expression টি সত্য হয় তাহলে statement 1 কাজ করবে। আর যদি মিথ্যে হয় তাহলে statement2 টি কাজ করবে। একটা প্রোগ্রাম লিখে ফেলিঃ

#include <stdio.h>
int main(void)
{
    int day;
    printf("Enter value of day: ");
    scanf("%c",&day);
    if((day=='F')|| (day=='f')){
      printf("You can sleep today! :D");
    } else {
        printf("oh no! have to go to class :  '( ");
    }
    return 0;
} 
copy C Code & Paste it below the Compiler & Run

এখানে করছি কি, আমরা আজকের দিনের প্রথম অক্ষর ইনপুট নিয়েছি এবং তা day নামক ভ্যারিয়েবলে রেখেছি। তারপর if(day==’F’) এখানে একটা লজিক্যাল টেস্ট করেছি। যদি day এর মান F [মানে ফ্রাইডে, শুক্রবার] কারেকটারের সাথে মিলে, তাহলে প্রিন্ট করবে “You can sleep today! :D” কারণ আজ শুক্রবার। আর যদি অন্য কোন  কারেকটার ইনপুট দেয়, মানে সপ্তাহের অন্য কোন দিন হয়, তাহলে লেখা উঠবে “oh no! have to go to class :'(”

if – else  সহজ, তাই না?

উপরের প্রোগ্রামে হয়তো কেউ কেউ শুক্রবারের প্রথম লেটার হিসেবে F অথবা f যে কোন একটা দিতে পারে। এখন কেউ যদি উপরের প্রোগ্রামে f ইনপুট দেয়, তাহলেও লেখা উঠবে “oh no! have to go to class :'(” । অথচ আজ শুক্রবার। আমাদের প্রোগ্রামটি ঠিক করতে হবে। যেন কেউ  F অথবা f যে কোন একটা ইনপুট দিলেই লেখা উঠে  “You can sleep today! :D”

তার জন্য নিচের প্রোগ্রাম লিখিঃ

#include <stdio.h>
int main(void)
{
    int day;
    printf("Enter value of day: ");
    scanf("%c",&day);
    if((day=='F')|| (day=='f')){
      printf("You can sleep today! :D");
    } else {
        printf("oh no! have to go to class :  '( ");
    }
    return 0;
}
copy C Code & Paste it below the Compiler & Run

আমরা কন্ডিশনটা একটু পরিবর্তন করেছি। লিখছি: if ((day==’F’)|| (day==’f’))

এখানে || মানে or মানে অথবা। আমরা তা লজিক্যাল অপারেটর সেকশনে জেনেছি। এখন (day==’F’) অথবা (day==’f’) যে কোন একটা সত্য হলেই আমাদের প্রোগ্রাম প্রিন্ট করবে “You can sleep today! :D” অন্যথায় প্রিন্ট করবে “oh no! have to go to class :'(”

if – else এ কিন্তু else না থাকলে ও হয়। এটা একটি বাড়তি অংশ। নিচে কয়েকটি statement দেওয়া হলঃ

if (x==1) pfrintf(“%d”,1);

এখানে যদি x=1 হয় তাহলে pfrintf(“%d”,1); statement টি কাজ করবে এবং 1 Print করবে। আর যদি x=1 না হয় তাহলে pfrintf(“%d”,1); statement টি কাজ করবে না। সম্পুর্ণ একটি প্রোগ্রামঃ

#include <stdio.h>
int main(void)
{
    int x;
    printf("Enter value of X: ");
    scanf("%d", &x);
 
    if(x==1){
        printf("value of X is 1");
    }
    return 0;
}
copy C Code & Paste it below the Compiler & Run

if(a>b) printf("a is greater then b");
else printf("a is less then or equal to b");

এখানে যদি a<b হয়, মানে a যদি  b থেকে ছোট হয় তাহলে printf(“a is greater  then b”); statement টি কাজ করবে এবং a is grater then b লেখা টি প্রিন্ট করবে। আর যদি a<b না হয় তাহলে printf(“a is greater  then b”); কাজ করবে না। else অংশে যাবে এবং printf(a is less then b”); statement টি কাজ করবে তারপর a is less then b লেখাটি প্রিন্ট করবে।

সম্পুর্ণ প্রোগ্রামঃ

#include<stdio.h>
int main(void)
{
    int a,b;
    printf("input the valu of a:");
    scanf ("%d",&a);
    printf("input the valu of b:");
    scanf ("%d",&b);
    if(a>b) printf("a is greater then b");
    else printf("a is less then or equal to b");
 
    return 0;
}
copy C Code & Paste it below the Compiler & Run

শুধু মাত্র if statement ব্যবহার করে দুটি সংখ্যার মধ্যে ছোট বড় নির্নয়ের জন্য একটি প্রোগ্রাম লিখিঃ

#include<stdio.h>
int main(void)
{
    int x,y;
     
    printf("input the valu of x:");
    scanf ("%d",&x);
     
    printf("input the valu of y:");
    scanf ("%d",&y);
     
    if(x>y) printf("x is greater then y\n");
    if(x<y)printf("x is less then y\n");
    if(x==y)printf("x is equal to y\n");
     
    return 0;
}
copy C Code & Paste it below the Compiler & Run

এ প্রোগ্রামে আপনার কাছ থেকে দুটি নাম্বার ইনপুট নিবে। তার পর তাদের মধ্যে কোনটা বড়, ছোট বা সমান তা দেখাবে। If-else statement ব্যবহার করে দুটি সংখ্যার মধ্যে ছোট বড় নির্নয়ের জন্য একটি প্রোগ্রাম লিখিঃ

#include<stdio.h>
int main(void)
{
    int x,y;
    printf("input the valu of x:");
    scanf ("%d",&x);
      
    printf("input the valu of y:");
    scanf ("%d",&y);
      
    if(x>y) printf("x is greater then y\n");
    else printf("x is less then y\n");
}
copy C Code & Paste it below the Compiler & Run

এখানের প্রোগ্রামটি আগের টির মতই। তবে এখানে equality দেখাবে না। equality দেখার জন্য নিচের প্রোগ্রামটি দেখিঃ

#include<stdio.h>
int main(void)
{
    int x,y;
    printf("input the valu of x:");
    scanf ("%d",&x);
 
    printf("input the valu of y:");
    scanf ("%d",&y);
 
    if(x>y) printf("x is greater then y\n");
    if(x==y) printf("x is equal to y\n");
    else printf("x is less then y\n");
}
copy C Code & Paste it below the Compiler & Run

পাস এবং ফেল নির্নয় জন্য একটি প্রোগ্রামঃ

#include<stdio.h>
int main(void)
{
    int x;
    printf("input your number:");
    scanf ("%d",&x);
      
    if (x>40 && x<100)
    printf("Congratulation!!! You have passed");
    else("Ops!! You have failed.");
}
copy C Code & Paste it below the Compiler & Run

এখানে যদি এক্সামের মার্ক ইনপুট দেওয়া হয়ত, তাহলে পাস করছে নাকি ফেল করছে তা দেখাবে।

nested if-else

একটা if ব্লকের ভেতর যদি আরেকটা if/else ব্লক লিখি, তাই হচ্ছে nested if-else। নিচের প্রোগ্রামটি দেখিঃ

#include <stdio.h>
int main ()
{
  int grade;
  printf("Type in your grade: ");
  scanf ("%d", &grade);
 
  if (grade < 10){
    printf ("Oh no!");
  }
  else{
    if (grade < 40){
      printf ("You failed.\n");
    }
    else{
      printf ("You passed!\n");
      if (grade >= 90){
        printf ("And you got an A!\n");
      }
    }
  }
  return 0;
}
copy C Code & Paste it below the Compiler & Run