Total members 11890 |It is currently Fri Apr 19, 2024 5:00 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





The C language does not have a boolean type. Instead, it uses integer under the following general rules:

  • An operation which requires a boolean value takes an integer and treats zero as false and any other value as true.

  • An operation which produces a boolean value generates 1 or 0 for true and false.

The C++ committee eventually added a boolean type to C++ (and called it bool, just to keep you from spelling right the first time). To maintain compatibility with C, it converts freely to and from integer under similar rules. Specifically:
  • When an integer is used in a boolean context, it is converted under similar rules: zero becomes false, and nonzero becomes true.

  • When a bool is used in an int context, it is converted to 1 or 0, for true or false.

The result is very similar behavior in either dialect, produced from different formal type rules.

Relational Operators

cpp code
==       !=       <       <=       >       >=


C: Result is 1 or 0.

C++: Result is true or false.

Logical Operators

cpp code
&&       ||


In C: expect integer values, treating zero as false and non-zero as true. Produce 1 or 0.

In C++: expect boolean values. If integer(s) are present, convert to bool using the rule that zero converts to false, nonzero converts to true. Produce true or false.

Short circuit
cpp code
if(n != 0 && sum / n > 1.0) ...


Conditional Operator

cpp code
expr_test ? expr_true : expr_false


When you want an if, but need an expression.
cpp code
max = a < b ? b : a;

printf("Max is ", a < b ? b : a);




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : C++ Boolean Operations
 Boolean type constant     -  
 Use the boolean compare operators     -  
 difference between the Boolean & operator and the &&     -  
 Define boolean Class properties     -  
 Matrix Operations     -  
 Operations on Sucxent++     -  
 Character Operations     -  
 Input-Output Operations     -  
 Bit operations-set-get-xor-rotate on bits arrays     -  
 solve the complex numbers and do operations on it     -  



Topic Tags

C++ Basics
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com