Question about if else and conditional operator ? :

Hi!

I read this example

#include<stdio.h>
#include<windows.h>
int main ()
{
    int count=1;
    while(count<=4)
    {
    printf( "%s\n", count % 2 ? "****" : "++++++++" );
     contador++;
     }
        system("pause");
    return 0;
}

In this example ¿why I don't need define contador==0 or contador==1? I realized a translation using if and else
my translate is ;

#include<stdio.h>
#include<windows.h>
int main ()
{
    int count=1;
    while(count<=4)
    {
if (count%2==1);
printf("****");
else
printf("+++++");
     }
        system("pause");
    return 0;
}

Regards

why I don't need define contador==0 or contador==1?

printf( "%s\n", contador % 2 ? "****" : "++++++++" );

uses the ternary function.

It equates to

if (contador % 2 == true)
{
  print "****" 
}
else
{
  print "++++++++"
}

Do you understand what the modulo operator ( % ) does ?

Yes;
this operator divide two entire numbers , take the remainder ;
in the example give 1 or 0 depending if count is odd or even, isn't?

Thanks for the answer !!! you save me from mindblown

Am I the only one to not be able to see a variable called "contador"?

AWOL:
Am I the only one to not be able to see a variable called "contador"?

I took the question that way at first but I think that there is a language problem in the question and he did not write what he meant.

in the example give 1 or 0 depending if count is odd or even, isn't?

Yes, and they are equivalent to true and false so can be tested without the need to test for equality

(deleted)

Neodimio58:
In this example ¿why I don't need define contador==0 or contador==1? I realized a translation using if and else
my translate is ;

You can use numerical values in statements that expect a true/false value like if and while statements. In those cases, 0 is treated as false and all other numbers are treated as true.

author=UKHeliBob date=1514017782 link=msg=3533743]

printf( "%s\n", contador % 2 ? "****" : "++++++++" );

uses the ternary function.

It equates to

if (contador % 2 == true)

{
  print "****"
}
else
{
  print "++++++++"
}




Do you understand what the modulo operator ( % ) does ?

Its not a function, its the conditional expression syntax, which is a distfix operator, and it equates to

if (contador % 2 != 0)
{
  print "****" 
}
else
{
  print "++++++++"
}

Boolean values are treated as false if zero, otherwise treated as true. Functions evaluate all their arguments, the conditional expression doesn't.