4 Replies - 533 Views - Last Post: 31 December 2010 - 01:03 PM Rate Topic: -----

#1 Guest_allen*


Reputation:

conditional operation in c++

Posted 30 December 2010 - 03:21 AM

why these conditional operators return true in c++
if()
{//code here excecutes*/}
as well as
while()
{//code here excecutes*/}
for(; ; )
{//code here excecutes*/}
Is This A Good Question/Topic? 0

Replies To: conditional operation in c++

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: conditional operation in c++

Posted 30 December 2010 - 03:33 AM

those keywords do not return anything at all, so i'm not exactly sure what you're asking.

Could you elaborate on your question to clarify what you're asking? Do you have a working code example which demonstrates what you're seeing?
Was This Post Helpful? 0
  • +
  • -

#3 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: conditional operation in c++

Posted 30 December 2010 - 04:59 AM

if you try to execute if statement and while statement with no condition it will give error

but what you have specified about for statement will last forever...it is an infinite loop,if there are no statements which will cause a break out of the loop.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: conditional operation in c++

Posted 31 December 2010 - 04:59 AM

View Postallen, on 30 December 2010 - 07:21 PM, said:

if()
{//code here excecutes*/}

If that compiles and executes the code indicated then your compiler is rubbish.
Upgrade it urgently.


View Postallen, on 30 December 2010 - 07:21 PM, said:

while()
{//code here excecutes*/}

If that compiles and executes the code indicated then your compiler is rubbish.
Upgrade it urgently.
Was This Post Helpful? 1
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: conditional operation in c++

Posted 31 December 2010 - 01:03 PM

View Postallen, on 30 December 2010 - 05:21 AM, said:

why these conditional operators return true in c++
if()
{//code here excecutes*/}
as well as
while()
{//code here excecutes*/}
for(; ; )
{//code here excecutes*/}



In C++ there is only one "condition operator" and that is the ternary operator ?:

Example:
int max = ( a > b ) ? a : b;


The conditional operator (:?) forms a conditional expression. The conditional expression above is ( a > b ) ? a : b -- the value of the expression is determined by the first boolean expression ( a > b ) if that evaluates to true then the value of the expression is "a", else the value of the expression is "b". Note that in C++ both a, b, must be of the same type.


The three bits of code you expressed all use "statements" not operators and therefore do not form "expressions" and thus they DO NOT evaluate to any value, they are not true, false or otherwise.

These can be thought of as conditional structures -- although you left out an important part, the "boolean expression" which is what forms the "condition" needed for the inner code to execute.

For example:
if(boolean_expression) { /* do this */ }


if the "boolean_expression" evaluates to true (non-zero) then the code in the block is executed, else it is not.

int max = b;
if( a > b ) { max = a; }

in this code if the value of the variable max is set to "a" if "a" happens to be larger than "b".
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1