2 Replies - 295 Views - Last Post: 17 July 2011 - 01:45 AM Rate Topic: -----

#1 waji10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-May 10

regarding conditional operator

Posted 17 July 2011 - 01:02 AM

#include<stdio.h>
#include<stdlib.h>
void main()
{
int i=1;
while(i<=10)
{
printf("%s\n",(i%2)?"****":"++++++++");
i++;
}

system("pause");


}



In the above code i couldn't understand the condition part of the conditional operator. i mean there isn't anything like ((i%2)==0) or else. and it prints both the strings alternately.
Is This A Good Question/Topic? 0
  • +

Replies To: regarding conditional operator

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: regarding conditional operator

Posted 17 July 2011 - 01:11 AM

In C and C++, expressions which evaluate to a numeric value can be used as true/false statements, where zero is false, and non-zero is true.

So, for conditional expressions, (i%2) would evaluate in the same way as (i%2) != 0

There's a deeper explanation here: http://c-faq.com/~sc...notes/sx3c.html

This post has been edited by Bench: 17 July 2011 - 01:12 AM

Was This Post Helpful? 0
  • +
  • -

#3 sudheesh1995  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 30
  • Joined: 15-July 11

Re: regarding conditional operator

Posted 17 July 2011 - 01:45 AM

this code is the same as


if((i%2)!=0)
{ printf("%s\n","****");}
else
{ printf("%s\n","++++++++");}




Hope that helps .
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1