← Back to Scala Course | Chapter 3: Operators & Expressions | Lesson 3 of 7

तार्किक ऑपरेटर्स

Logical operators हां-या-नहीं वाले सवालों को and, or, और not जैसे शब्दों से जोड़ते हैं। वे किसी program को एक से ज़्यादा fact के आधार पर decide करने में मदद करते हैं।

In this page:

  1. Logical Operators
Syntax
scala
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

markup
object Main extends App {
  val isLoggedIn = true
  val isAdmin = false

  println(isLoggedIn && isAdmin)
  println(isLoggedIn || isAdmin)
  println(!isAdmin)
}
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. && और || के इरादे की जगह & और | का उपयोग करना, जिससे right side हमेशा evaluate होता है।
  2. यह उम्मीद करना कि && और || दोनों sides evaluate करेंगे, जबकि वे short-circuit करते हैं और left side result तय करे तो right side skip कर देते हैं।
  3. !a == b लिखना और !(a == b) की उम्मीद करना, जबकि ! ज़्यादा कसकर bind होता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.