तार्किक ऑपरेटर्स
Logical operators हां-या-नहीं वाले सवालों को and, or, और not जैसे शब्दों से जोड़ते हैं। वे किसी program को एक से ज़्यादा fact के आधार पर decide करने में मदद करते हैं।
In this page:
Syntax
condition1 && condition2
condition1 || condition2
!condition
Logical Operators
Logical operators Boolean expressions को combine करते हैं: && (AND), || (OR), और ! (NOT)। && और || short-circuit करते हैं -- अगर left side पहले ही result तय कर दे, तो right side कभी evaluate नहीं होता।
यह तब मायने रखता है जब right side के side effects हों या वह throw कर सके।
Note:
Short-circuiting आपको safely
if (list.nonEmpty && list.head > 0) लिखने देता है बिना किसी खाली list पर error का खतरा उठाए।उदाहरण: Logical Operators
object Main extends App {
val isLoggedIn = true
val isAdmin = false
println(isLoggedIn && isAdmin)
println(isLoggedIn || isAdmin)
println(!isAdmin)
}
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
&&और||के इरादे की जगह&और|का उपयोग करना, जिससे right side हमेशा evaluate होता है।- यह उम्मीद करना कि
&&और||दोनों sides evaluate करेंगे, जबकि वे short-circuit करते हैं और left side result तय करे तो right side skip कर देते हैं। !a == bलिखना और!(a == b)की उम्मीद करना, जबकि!ज़्यादा कसकर bind होता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: