C Handout 3
Comments and Logical Statements

(10/10/00 - Page 1)

 

 

Comments

Comments are any text between /* and the next */. You should use them liberally throughout your program to improve the readability, and understandability, of your code.

 

 

Logical Statements

To work with logical statements in C, it is important to realize that an integer that is zero is interpreted as FALSE, any non zero integer is interpreted as TRUE.

 

void main() {

int i, j, k;

i=0;

j=3;

 

/*Comparisons*/

k= i<j; /*k is TRUE because i is less than j*/

k= i>j; /*k is FALSE*/

k= i<=j; /*k is TRUE because i is less than or equal to j*/

k= i==j; /*k is FALSE because i is not equal to j*/

k= i!=j; /*k is TRUE because i is not equal to j*/

 

k= i=j; /*k is TRUE. This example sets i=j, then sets k=i, so k will

be 3, which is TRUE. This is a subtle, and very common

mistake. Make sure you use == to check for equality. */

i=0;

k= j=i; /*k is FALSE.*/

 

/*Logical Operators*/

i=0; /*i is FALSE*/

j=3; /*j is TRUE*/

 

k= i&&j; /*k is FALSE*/

k= i||j; /*k is TRUE*/

k= !i && j; /*k is TRUE. NOT (!) has precedence over AND (&&)*/

}

 

 

 


Review Sheet
 Comments and Logical Statements

 

Evaluate k (either TRUE or FALSE) at the locations shown, and fill in the blanks.

 

void main() {

int i,j,k;

 

i=0;

j=3;

 

k= i;                   /*k=_______________*/

k= j;                   /*k=_______________*/

k= !j;                  /*k=_______________*/

 

k= !(i==j);             /*k=_______________*/

k= i==!j;               /*k=_______________*/

k= i=!j;                /*k=_______________*/

 

k= !(i<j);              /*k=_______________*/

k= i>=j;                /*k=_______________*/

 

k= (i<j) && (i>j);      /*k=_______________*/

k= !(!(i<j) || !(i>j)); /*k=_______________*/

}