Error with && operand

Hi!!

I’m very, very newbie ic C#. I’m getting error with this piece of code
I get the following error:
Operator '&&' cannot be applied to operands of type 'bool' and 'int'

I’ve programming in C (Turbo C) and there are a lot of things I’m not remember about C

Somebody knows what is the cause of the error

Thanks in advanced.



private void ordBurbuja(int[] a, int n)
{
int interrupt = 1;
int pass, j;

for (pass = 0; pass < n - 1 && interrupt; pass++) <--Here I get the error
{
interrupt = 0;
for (j = 0; j < n - pass - 1; j++)
if (a[j] > a[j + 1])
{
int aux;
interrupt = 1;
aux = a[j];
a[j] = a[j + 1];
a[j + 1] = aux;
}
}
}





Answer this question

Error with && operand

  • chr1zis

    It looks to me like you're assuming that you can do this

    int i = DoSomething()

    if (i)

    {

    //xxx

    But, C# does not allow implicit int to bool, so you need to do this:

    for (pass = 0; pass < n - 1 && interrupt != 0; pass++) <--Here I get the error

    I'd actually do this differently, I'd do this:

    private void ordBurbuja(int[] a, int n)
    {
    int pass, j;

    for (pass = 0; pass < n - 1; pass++) <--Here I get the error
    {
    for (j = 0; j < n - pass - 1; j++)
    if (a[j] > a[j + 1])
    {
    int aux;
    aux = a[j];
    a[j] = a[j + 1];
    a[j + 1] = aux;

    break;
    }
    }
    }

    Of course, you can also make interrupt a bool instead of an int :-)



  • Wasting Body

    Thanks a lot for the help!

    I thought the same like you but I was not sure of use interrupt !=0.

    I tried the code and now runs.

    Thanks


  • Error with && operand