Search code examples
cif-statementconditional-statementsvarying

Varying arguments for if () statement


I have a problem as stated below: i have an array(say) a[]={10,24,56,33,22,11,21} i have something like this

for(i=0;i<100;i++){
    if(a[i]==10)
        // do something
}

next when i=1

if(a[i]==10 && a[i+1]==24)

so on so at each iteration the arguments / conditions within if should be varying now this will be a very big sequence i cant explicitly write
if(a[i]==10 && a[i+1]==24 && a[i+2]==56 ...... a[i+100]=2322)

how can i achieve this varying conditions?


Solution

  • You have to have a cumulative "boolean" variable that checks a[i] at the i-th iteration and update that variable:

    int a[] = {...};   /* array with some values to verify */
    int v[] = {...};   /* these are the actual desired values in a[] */
    
    /* the verifying loop */
    int i;
    int cond = 1;
    for (i = 0; i < 100; i++)
    {
        cond = cond && (a[i] == v[i]);
        if (cond)
        {
           /* do something */
        }
    }