I always get confused with while when I need to use multiple condition. I believe this is correct.

1 and 1 = 1
1 and 0 = 0
1 or 1 = 1
1 or 0 = 1

Is there something special I need to do with while loops? If statements always seem to behave like I expect with multiple conditions. Is there a good rule of thumb to follow?

Like here. Since I usually try || and it never seems to work I used && first. But Of course I needed an || in this case just because I did the opposite of what I usually do.

const char *s = buffer;
parser_comment_level = 0
while (ispunct((unsigned char)*s) || parser_comment_level != 0)
{
}

I would like to change the above to. Is that correct or do I need to use an &&? Would it be a good idea to use parenthesis and if so how?

const char *s = buffer;
parser_comment_level = 0
while (ispunct((unsigned char)*s) || parser_comment_level != 0 || (unsigned char)*s != '\0')
{
}

I would say, for while loops, that it depends on the trigger(s) needed to break out of the loop. If you need one of several triggers to break out of the loop then OR (||) would be needed. If you need several triggers to be set at the same time to break out of the loop then AND(&&) would be needed.

I would say, for while loops
if the loop depends on a number of things being true (eg more data waiting, no errors found, server available), then AND is needed

Since you posted your question as a C programming question, here is a little C demo program that uses || ... (OR) ... you may find helpful.

/* bp.c */  /* revised 2016-01-31 */


#include <stdio.h>

const char* MY_FILE = "BP.txt";

/* (make a test) data file structured as per the following: */
/*
Smith Jane 111/76
Jones Rob 98/70
Costello Judy-Ann 144/90
Frank-Anderson Bibby-Bonnie 190/30
Sue Peggy 10/5
James-Thomas Andrew 190/111
*/


const char* UNKNOWN = "*** Unrecognized diagnosis ***\n"
                      "*** Please check validity of data entered ***";


typedef struct
{
    char lname[20];
    char fname[20];
    int sys;
    int dia;

} BP ; /* BloodPressure*/


const char* get_diagnosis( int sys, int dia ) /* Note: need 'const' since returning 'literal' */
{
    if( sys < 120/2 || dia < 80/2 ) /* ?? */
        return UNKNOWN;
    if( sys > 120*2 || dia > 80*2 ) /* ?? */
        return UNKNOWN;

    if( sys >= 160 || dia > 100 )
        return "*** Check age re. possible Stage 2 Hypertension ***";
    if( sys >= 140 || dia >= 90 )
        return "*** Check age re. possible Stage 1 Hypertension ***";
    if( sys >= 120 || dia >= 80 )
        return "*** Check age re. possible Pre-Hypertension ***";

    /* else ... */
    return "Normal";
}



void print( const BP* p )
{
    char buf[40] = {'.'};
    sprintf( buf, "%s, %s ", p->fname,  p->fname );

    printf( "%s blood pressure was %d/%d\n", buf, p->sys, p->dia );
    printf( "%s\n", get_diagnosis( p->sys, p->dia ) );
}

int takeIn( FILE* fin, BP* p )
{
    char skip;
    int num = fscanf( fin, "%19s %19s %d %c %d", p->lname, p->fname, &p->sys, &skip, &p->dia );
    return num == 5;
}




int main()
{
    FILE* fin = fopen( MY_FILE, "r" );

    if( fin )
    {
        BP patient;
        while( takeIn( fin, &patient ) )
            { print( &patient ); putchar('\n'); }

        fclose( fin );
    }
    else
        printf( "There was a problem opening file %s\n", MY_FILE );

    printf( "Press 'Enter' to continue/exit ... " );
    getchar();

    return 0;
}
This article has been dead for over six months. Start a new discussion instead.