← Back to C Course | Chapter 4: Control Flow | Lesson 1 of 11

C का if Statement

एक if statement सिर्फ तब code चलाता है जब एक condition true हो, ठंड होने पर ही coat लेने जैसा। अगर यह false हो, C इसे skip कर देता है।
Syntax
c
if (condition) {
  // runs when condition is true
}

if Statement क्या है?

एक if statement एक condition evaluate करता है और सिर्फ तब अपना code block चलाता है जब वह condition true हो -- अगर यह false हो, block पूरी तरह skip हो जाता है और execution इसके बाद जो भी आता है उससे जारी रहता है।

उदाहरण: What is an if Statement?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	// Check whether age >= 18
	if (age >= 18) {
		// Print formatted text to the screen
		printf("Adult");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Simple Positive Check

आप एक value inspect करने और उस पर react करने के लिए एक single if statement इस्तेमाल कर सकते हैं, जैसे positive numbers पहचानने के लिए if (number > 0) check करना, false case के लिए कुछ specific न करना हो तो एक else branch की ज़रूरत नहीं।

उदाहरण: Simple Positive Check

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare number and set it to 5
	int number = 5;
	// Check whether number > 0
	if (number > 0) {
		// Print formatted text to the screen
		printf("Positive");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Even Numbers Check करना

modulo operator को एक if condition के साथ combine करना -- if (n % 2 == 0) -- एक program को यह check करके even numbers detect करने देता है कि 2 से divide करने पर कोई remainder बचता है या नहीं।

उदाहरण: Checking Even Numbers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 8
	int n = 8;
	// Check whether n % 2 == 0
	if (n % 2 == 0) {
		// Print formatted text to the screen
		printf("Even");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Single if Checks

sequence में कई independent if statements लिखने का मतलब है हर condition अपने आप check होती है, चाहे पहले वाली true रही हो या नहीं -- एक else-if ladder के उलट, इनमें से कोई भी सिर्फ इसलिए skip नहीं होता कि पिछला check पहले ही match कर चुका।

उदाहरण: Multiple Single if Checks

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 8
	int n = 8;
	// Check whether n > 0
	if (n > 0) {
		// Print formatted text to the screen
		printf("Positive ");
	}
	// Check whether n % 2 == 0
	if (n % 2 == 0) {
		// Print formatted text to the screen
		printf("Even");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested if

एक दूसरे if के block के अंदर रखा गया if statement सिर्फ तब चलता है जब outer और inner दोनों conditions true हों, आपको layered logic बनाने देते हुए जैसे पहले यह check करना कि एक number positive है, फिर अलग से यह check करना कि यह even भी है।

उदाहरण: Nested if

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 8
	int n = 8;
	// Check whether n > 0
	if (n > 0) {
		// Check whether n % 2 == 0
		if (n % 2 == 0) {
			// Print formatted text to the screen
			printf("Positive and even");
		}
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. x == 5 की बजाय if (x = 5) लिखना, जो assign करता है और हमेशा true होता है।
  2. condition के बाद एक semicolon लगाना, if (x > 5);, ताकि block हमेशा चले।
  3. कई statements के आस-पास braces छोड़ देना ताकि सिर्फ पहला ही if से belong करे।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.