अभी तक आपन C programming tutorials में सभी basic concept पढ़े और सीखे , अब आप कुछ important statements के बारे में पढ़ेंगे जिनमे से if else statement से शुरुआत करेंगे।


If Else statement किसी भी programming language का सबसे important feature है , C if else conditions के according किसी Code Of Block को run करने के लिए use होता है। Means जब हमें Condition True होने पर कोई दूसरा code run करना हो condition गलत होने पर कुछ और तब हम If else का use करते हैं।

C if Statement

जब हमें किसी condition के true होने पर code of block run करना होतो वहां पर simple if statement use करते हैं। हमें इसमें कोई भी ऐसा expression pass करना होता है जिसका result कोई boolean value return करता हो।

if Syntax

if(condition) {
  // code of block
}

C if example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  int age = 23;
  if(age >= 18) {
    printf("Yes ! you are eligible to vote.");
  }
  return 0;
}
Output
Yes ! you are eligible to vote.

C if else

जब condition true होने पर दूसरा code of block run करना हो और condition गलत होने पर कोई और तब हम if else का use करते हैं। इस statement में अगर condition true हुई तो if block run होगा और गलत हुई तो else block का code run होगा।

if(condition) {
  // code of block if condition true.
}
else {
  // code of block.
}

C if else example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  int age;
  printf("Enter your age : ");
  scanf("%d", &age);
  if(age >= 18) {
    printf("Yes ! you are eligible to vote.");
  }
  else {
    printf("Sorry ! you are not eligible to vote.");
  }
  return 0;
}
Output
Enter your age : 14
Sorry ! you are not eligible to vote.

Skip curly brackets for single line

if else में अगर आपको simple एक line का ही code लिखना है तो आप curly brackets को skip कर सकते हैं।
For Example -

if(age >= 18)
  printf("Yes ! you are eligible to vote.");
else 
  printf("Sorry ! you are not eligible to vote.");

C if else if

if else if जैसा की नाम से पता चलता है कि अगर upper condition condition false होती है और else if में दी हुयी condition true return करती है तो else if code of block run होगा। हालाँकि if के बाद यह जरूरी नहीं की एक ही else if use हो condition के according एक से ज्यादा भी use हो सकते हैं।

C if else if example

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  int marks;
  printf("Enter your marks : ");
  scanf("%d", &marks);
  if(marks >= 80)
    printf("Amazing Grade : A");
  else if(marks >= 70)
    printf("Grade B");
  else if(marks >= 50)
    printf("Grade C");
  else if (marks >= 33) 
    printf("Grade D");   
  else 
    printf("Failed !");
  return 0;
}
Output
Enter your marks : 67
Grade C

C if else shorthand

इन सबके अलावा हम short hand trick भी use में ले सकते हैं जिसे ternary operator कहते हैं , क्योंकि इसमें 3 operands को use किया जाता है। इससे आप multiple lines के code को single line से replace कर सकते हैं। इसे mostly if else की जगह ही use करते हैं।

CopyFullscreenClose FullscreenRun
#include <stdio.h>
int main() {
  int age;
  printf("Enter your age : ");
  scanf("%d", &age);
  (age >=18) ? printf("Yes ! You are eligible to vote") : printf("Sorry ! You are not eligible to vote");
  return 0;
}
Output
Enter your age : 23
Yes ! You are eligible to vote

I Hope, अब आपको C language में if else के बारे में अच्छे से समझ आ गया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers