ItzEdInYourBed

69 - C++ LOGICAL OPERATORS

Jul 12th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. [SIZE=7][B]Logical Operator: &&[/B][/SIZE]
  2. The [ICODE]and[/ICODE] logical operator is denoted by [ICODE]&&[/ICODE].
  3. It returns [ICODE]true[/ICODE] if the condition on the left [I]and[/I] the condition on the right are both [ICODE]true[/ICODE]. Otherwise, it returns [ICODE]false[/ICODE].
  4. Here’s the truth table:
  5. [TABLE]
  6. [TR]
  7. [TH]a[/TH]
  8. [TH]b[/TH]
  9. [TH]a && b[/TH]
  10. [/TR]
  11. [TR]
  12. [TD]false[/TD]
  13. [TD]false[/TD]
  14. [TD]false[/TD]
  15. [/TR]
  16. [TR]
  17. [TD]false[/TD]
  18. [TD]true[/TD]
  19. [TD]false[/TD]
  20. [/TR]
  21. [TR]
  22. [TD]true[/TD]
  23. [TD]false[/TD]
  24. [TD]false[/TD]
  25. [/TR]
  26. [TR]
  27. [TD]true[/TD]
  28. [TD]true[/TD]
  29. [TD]true[/TD]
  30. [/TR]
  31. [/TABLE]
  32.  
  33. For instance:
  34. [LIST]
  35. [*][ICODE]( 1 < 2 && 2 < 3 )[/ICODE] returns [ICODE]true[/ICODE]
  36. [*][ICODE]( 1 < 2 && 2 > 3 )[/ICODE] returns [ICODE]false[/ICODE]
  37. [/LIST]
  38. [B]Note:[/B] The keyword [ICODE]and[/ICODE] can also be used in the place of [ICODE]&&[/ICODE].
  39.  
  40. [SIZE=7][B]Logical Operator: II[/B][/SIZE]
  41. The [ICODE]or[/ICODE] logical operator is denoted by [ICODE]||[/ICODE].
  42. It returns [ICODE]true[/ICODE] when the condition on the left is [ICODE]true[/ICODE] [I]or[/I] the condition on the right is [ICODE]true[/ICODE]. Only one of them needs to be [ICODE]true[/ICODE].
  43. Here’s the truth table:
  44. [TABLE]
  45. [TR]
  46. [TH]a[/TH]
  47. [TH]b[/TH]
  48. [TH]a || b[/TH]
  49. [/TR]
  50. [TR]
  51. [TD]false[/TD]
  52. [TD]false[/TD]
  53. [TD]false[/TD]
  54. [/TR]
  55. [TR]
  56. [TD]false[/TD]
  57. [TD]true[/TD]
  58. [TD]true[/TD]
  59. [/TR]
  60. [TR]
  61. [TD]true[/TD]
  62. [TD]false[/TD]
  63. [TD]true[/TD]
  64. [/TR]
  65. [TR]
  66. [TD]true[/TD]
  67. [TD]true[/TD]
  68. [TD]true[/TD]
  69. [/TR]
  70. [/TABLE]
  71. For instance:
  72. [LIST]
  73. [*][ICODE]( 1 < 2 || 2 > 3 )[/ICODE] returns [ICODE]true[/ICODE]
  74. [*][ICODE]( 1 > 2 || 2 > 3 )[/ICODE] returns [ICODE]false[/ICODE]
  75. [/LIST]
  76. [B]Note:[/B] The keyword [ICODE]or[/ICODE] can be used in the place of [ICODE]||[/ICODE].
  77.  
  78. [SIZE=7][B]Logical Operator: ![/B][/SIZE]
  79. The [ICODE]not[/ICODE] logical operator is denoted by [ICODE]![/ICODE].
  80. It reverses the [ICODE]bool[/ICODE] outcome of the expression that immediately follows.
  81. Here’s the truth table:
  82. [TABLE]
  83. [TR]
  84. [TH]a[/TH]
  85. [TH]!a[/TH]
  86. [/TR]
  87. [TR]
  88. [TD]false[/TD]
  89. [TD]true[/TD]
  90. [/TR]
  91. [TR]
  92. [TD]true[/TD]
  93. [TD]false[/TD]
  94. [/TR]
  95. [/TABLE]
  96. For instance:
  97. [LIST]
  98. [*][ICODE]( !true )[/ICODE] returns [ICODE]false[/ICODE]
  99. [*][ICODE]( !false )[/ICODE] returns [ICODE]true[/ICODE]
  100. [*][ICODE]( !(10 < 11) )[/ICODE] returns [ICODE]false[/ICODE]
  101. [/LIST]
  102. [B]Note:[/B] The keyword [ICODE]not[/ICODE] can be used in the place of [ICODE]![/ICODE].
  103.  
  104. [SIZE=7][B]Review[/B][/SIZE]
  105. Awesome! In this mini-lesson, we’ve added more operators to our toolbox:
  106. [LIST]
  107. [*][ICODE]&&[/ICODE]: the [ICODE]and[/ICODE] logical operator
  108. [*][ICODE]||[/ICODE]: the [ICODE]or[/ICODE] logical operator
  109. [*][ICODE]![/ICODE]: the [ICODE]not[/ICODE] logical operator
  110. [/LIST]
Add Comment
Please, Sign In to add comment