Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:52 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: C++ Boolean Operations
PostPosted: Thu Nov 13, 2008 1:53 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

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

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

2. 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:

1. When an integer is used in a boolean context, it is converted under similar rules: zero becomes false, and nonzero becomes true.

2. 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


== != < <= > >=

C: Result is 1 or 0.

C++: Result is true or false.

Logical Operators

&& ||

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
if(n != 0 && sum / n > 1.0) ...

Conditional Operator

expr_test ? expr_true : expr_false

When you want an if, but need an expression.

max = a < b ? b : a;

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


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 Program for "Selection of arithmetic operations" in Java
 Character Operations
 difference between the Boolean & operator and the &&
 Use the boolean compare operators
 Boolean type constant
 Define boolean Class properties
 Matrix Operations

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 2 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team