C का if Statement
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Simple Positive Check
आप एक value inspect करने और उस पर react करने के लिए एक single if statement इस्तेमाल कर सकते हैं, जैसे positive numbers पहचानने के लिए if (number > 0) check करना, false case के लिए कुछ specific न करना हो तो एक else branch की ज़रूरत नहीं।
उदाहरण: Simple Positive Check
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Even Numbers Check करना
modulo operator को एक if condition के साथ combine करना -- if (n % 2 == 0) -- एक program को यह check करके even numbers detect करने देता है कि 2 से divide करने पर कोई remainder बचता है या नहीं।
उदाहरण: Checking Even Numbers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई Single if Checks
sequence में कई independent if statements लिखने का मतलब है हर condition अपने आप check होती है, चाहे पहले वाली true रही हो या नहीं -- एक else-if ladder के उलट, इनमें से कोई भी सिर्फ इसलिए skip नहीं होता कि पिछला check पहले ही match कर चुका।
उदाहरण: Multiple Single if Checks
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Nested if
एक दूसरे if के block के अंदर रखा गया if statement सिर्फ तब चलता है जब outer और inner दोनों conditions true हों, आपको layered logic बनाने देते हुए जैसे पहले यह check करना कि एक number positive है, फिर अलग से यह check करना कि यह even भी है।
उदाहरण: Nested if
// 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;
}
Login to try C/C++/Java/PHP code in the editor
x == 5की बजायif (x = 5)लिखना, जो assign करता है और हमेशा true होता है।- condition के बाद एक semicolon लगाना,
if (x > 5);, ताकि block हमेशा चले। - कई statements के आस-पास braces छोड़ देना ताकि सिर्फ पहला ही
ifसे belong करे।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: