Skip to main content
Engineering LibreTexts

7.1: Conditionals

  • Page ID
    35829
  • C uses a fairly standard if/else construct for basic conditionals. They may be nested and each portion may consist of several statements. The condition itself may have multiple elements and be formed in either positive or negative logic. The basic construct is:

    if( test condition(s)... )
    {
        //...do stuff...
    }
    

    The else portion is optional and looks like:

    if( test condition(s).. )
    {
        //...do stuff...
    } else
    {
        //...do other stuff...
    }
    

    If there is only a single statement in the block (i.e., between the braces), the braces may be removed if desired:

    if( test condition(s).. )
        //...single statement...
    else
        //...do other statement...
    

    The test condition may check for numerous possibilities. The operators are:

    Table \(\PageIndex{1}\): Relational operators.
    == equality
    != inequality
    > greater than
    < less than
    >= greater than or equal to
    <= less than or equal to

    It is very important to note that equality uses a double equal sign. A single equal sign is an assignment operation. Don’t think “equals”, think “same as”, with one symbol for each word. You may also use Boolean (logic) operators, as shown in Table \(\PageIndex{2}\).

    Table \(\PageIndex{2}\): Logical operators.
    || OR
    && AND
    ! NOT

    Note that the logical operators do not behave the same as the similarly named bitwise operators. For example, a logical AND returns TRUE if its two arguments are non-zero, not necessarily the same bits. That is 1 & 2 yields 0, but 1 && 2 yields TRUE. TRUE is taken to be any non-zero value. Any variable or expression that evaluates to a value other than zero is logically TRUE. If the result is zero, then it is logically FALSE. Time for some examples. The conditional is written as a fragment with an explanation following:

    if( a==6 )
        /* taken only if the variable a is a 6 */
    
    if( b!=7 )
        /* taken as long as the variable b isn’t 7 */
    
    if( (a==6) && (b!=7) )
        /* taken as long as a is 6 and b is something other than 7 */
    
    if( (a==6) || (b!=7) )
        /* taken as long as a is 6 or b is something other than 7 */
    
    if( a==0 )
        /* taken if a is zero */
    
    if( !a )
        /* another way of saying taken if a is zero */
    
    if( a!=0 )
        /* taken if a is not zero */
    
    if( a )
        /* another way of saying taken if a is not zero */
    

    How you word something is up to you. The following two code fragments are equivalent:

    if( a==b )
        do_x();
    else
        do_y();
    
    if( a!=b )
        do_y();
    else
        do_x();
    

    It is very common for new programmers to use = when they want ==. This can have disastrous results. Consider the following code fragment:

    if( a=b )

    What does this do? At first glance, you might think it tests to see if a and b are the same. It does nothing of the kind! Instead, it assigns the value of b to a and then checks to see if that value is non-zero. In other words, it does this:

    a=b;
    if( a )

    A trick to help you with this, at least with constants, is to reverse the normal order. Instead of writing if( a==6 ), use if( 6==a ). This way, if you accidentally use a single equal sign, the compiler will cough up a syntax error.

    • Was this article helpful?