|< C/C++ Statements, Expressions & Operators 1 | Main | C/C++ Statements, Expressions & Operators 3 >| Site Index | Download |


 

 

 

 

 

MODULE 3a

MORE ON C/C++ STATEMENTS, EXPRESSIONS

AND OPERATORS 2

 

 

 

 

My Training Period:  xx hours

 

The source code for this Module is: Module 3 C/C++ source codes. and the related worksheet for your practice is: C lab worksheets 5.

 

The C and C++ skills: Able to understand and use:

 

 

 

 

 

 

 

 

 

 

 

 

 

3.6    Relational Operators

  • Used to compare expressions, asking questions such as, “is x greater than 200?” or  “is y equal to 10”.

  • An expression containing a relational operator evaluates as either TRUE (1) or FALSE (0).

  • C / C++ has six relational operators as shown in Table 3.5:

 Operator

Symbol

Means

Example

Equal

==

Is operand 1 equal to operand 2?

x  == y

Greater than

>

Is operand 1 greater than operand 2?

x  >  y

Less than

<

Is operand 1 less than operand 2?

x  <  y

Greater than

>=

Is operand 1 greater than or equal to operand 2?

x  >= y

Less than

<=

Is operand 1 less than or equal to operand 2?

x  <= y

Not equal

!=

Is operand 1 not equal to operand 2?

x != y

 

Table 3.5: Relational operators

  • Another simple examples:

Expression

Evaluates As

Read As

5 == 1

0 (false)

Is 5 equal to 1?

5 > 1

1 (true)

Is 5 greater than 1?

5 != 1

1 (true)

Is 5 not equal to 1?

( 5 + 10 ) == ( 3 * 5 )

1 (true)

Is (5 + 10) equal to (3 * 5)?

 

3.6.1    Expressions And The if Statement

  • Relational operators are used mainly to construct the relational expressions used in if and while statements. 

  • This is the introduction of the basic if statement, used to create program control statements.  Till now we only deal with the top down approach or line by line code but that is not the limitation.

  • We will learn more detail about program control in program control Module.  Assume this part as an introduction.

3.6.2    A Program Control Statement: An Introduction

if ( expression )

      statement(s);

next_statement;

  1. Evaluate an expression and directs program execution depending on the result of that evaluation.

  2. If the expression evaluate as TRUE, statement(s) is executed, if FALSE, statement(s) is not executed, execution then passed to the code follows the if statement, that is the next_statement.

  3. So, the execution of the statement(s) depends on the result of expression.

if ( expression )

{    

      statement1;

      statement2;

           ...

           ...

      statement-n;

}

next_statement;

// demonstrates the use of the if statements

#include <stdio.h>

 

int main()

{

     int x, y;

     // input the two values to be tested

     printf("\nInput an integer value for x: ");

     scanf("%d", &x);

     printf("Input an integer value for y: ");

     scanf("%d", &y);

     // test values and print result

     if (x == y)

     {

            printf("\nx  is equal to y");

     }

     if (x > y)

     {

            printf("\nx is greater than y");

     }

     if (x < y)

     {

            printf("\nx is smaller than y");

     }

     printf("\n\n");

     return 0;

}

 

Possible outputs:

 

C C++ if statement

 

C  C++ if statement

 

C C++ if statement

if ( expression )

statement1;

else

statement2;

next_statement;

// demonstrates the use of if-else statement

#include <stdio.h>

 

int main()

{

     int   x, y;

     // input two values to be tested

     printf("\nInput an integer value for x: ");

     scanf("%d", &x);

     printf("Input an integer value for y: ");

     scanf("%d", &y);

     // test the values and print result

     if (x == y)

     {

               printf("\nx is equal to y");

     }

     else if (x > y)

     {

            printf("\nx is greater than y ");

     }

     else {

            printf("\nx is smaller than y ");

     }

     printf("\n\n");

     return 0;

}

 

Possible outputs:

 

C C++ if-else statement

 

C C++ if-else statement

 

C C++ if-else statement

Form 1:

 

if ( expression )

statement1;

next_statement;

 

Form 2:

 

if ( expression )

statement1;

else

statement2;

next_statement;

 

Form 3:

 

if ( expression )

      statement1;

else if ( expression )

statement2;

else  if ( … )

statement3;

else

       statementN;

next_statement;

 

3.7    Relational Expressions

// demonstrates the evaluation of relational expression

#include <stdio.h>

 

int main()

{

     int  a;

     // evaluates to 1, TRUE

     a =  (5 == 5);

     printf ("\na = (5 == 5)\n  Then a = %d\n", a);

     // evaluates to 0, FALSE

     a = (5 != 5);

     printf ("\na = (5 != 5)\n  Then a = %d\n", a);

     // evaluates to 1 + 1, TRUE

     a = (12 == 12) + (5 != 1);

     printf("\na = (12 == 12) + (5 != 1)\n  Then a = %d\n", a);

     return 0;

}

               

Output:

 

C C++ relational operators

if(x = 5)

printf("x is equal to 5");

3.7.1    Precedence of Relational Operators

(x + 2 > y)

((x + 2) > y)

 

Operators

Relative precedence

<     <=     >    >=

1

!=     ==

2

 

Table 3.6: Precedence of the relational operators

x == y > z      equivalent to     x == (y > z)

if(x != 5)

      statement1;

else

      statement2;

if(x == 5)

      statement2;

else

      statement1;

 

3.8    Logical Operators

 

Operator

Symbol

Example

AND

&&

expression1 && expression2

OR

||

expression1 || expression2

NOT

!

!expression1

 

Table 3.7:  Logical operators

 

 

Expression

Evaluates As

(expression1  &&   expression2)

True (1) only if both expression1 and expression2 are true; false (0) otherwise.

(expression1 || expression2)

True (1) if either expression1 or expression2 is true; false (0) only if both are FALSE.

(! expression1)

False (0) if expression1 is true; true (1) if expression1 is true.

 

Table 3.8: Evaluation of the logical expressions

Expressions

Evaluates As

(5 == 5)  && (6  !=  2)

True (1) because both operands are true

(5 > 1) || (6 < 1)

True (1) because one operand is true

(2 == 1)  &&  (5 == 5)

False (0) because one operand is false

 ! (5 == 4)

True (1) because the operand is false

NOT (FALSE)  = TRUE

 

Table 3.9: Examples of logical expressions

Operand1

Operand2

Output

0

0

0 ( F )

0

1

0 ( F )

1

0

0 ( F )

1

1

1 ( T )

 

Table 3.10:  Logical AND Operation

 

 

Operand1

Operand2

Output

0

0

0 ( F )

0

1

1 ( T )

1

0

1 ( T )

1

1

1 ( T )

 

Table 3.11:  Logical OR Operation

 

3.8.1    TRUE And FALSE Values

         A value of 0, represents FALSE.

         Any non zero (including negative numbers) value represents TRUE.

x  =  125;

if(x)

    printf("%d", x)

 

3.8.2    Precedence of Logical Operators

  • C / C++ logical operators also have a precedence order.

  • ! is same level with unary mathematical operators ++ and -- and it has higher precedence than all relational operators and all binary mathematical operators.

  • && and || operators lower than all mathematical and relational operators.

  • Parentheses also can be used to modify evaluation order when using the logical operators.

  • When the parentheses are absent, the results are determined by operator precedence then the result may not be desired.

  • When parentheses are present, the order in which the expressions are evaluated changes.

  • Try the following program example and study the output and the source code.

#include <stdio.h>

 

int main()

{

    // initialize variables and note that c is not less than d, which is one of the conditions to test for

    // therefore the entire expression should be evaluated as false

     int  a = 5, b = 6, c = 5, d = 1;

     int  x;

     // evaluate the expression without parentheses

     x = a < b || a < c && c < d;      // Form 1

     printf("Given expression:\n");

     printf("x = a < b || a < c && c < d\n");

     printf("Without parentheses the expression evaluates as %d", x);

     // evaluate the expression with parentheses

     x = (a < b || a < c) && (c < d);  // Form 2

     printf("\n\nWith parentheses:\n");

     printf("x = (a < b || a < c) && (c < d)\n");

     printf("With parentheses the expression evaluates as %d\n", x);

     return 0;

}

 

Output:

 

C C++ logical operator precedence

  1. Is a less than b?  ,               a < b    // Condition 1

  2. Is a less than c?  ,               a < c    // Condition 2

  3. Is c less than d?  ,               c < d    // Condition 3

3.9    Compound Assignment Operators

x  =  x  +  5;

  x  += 5;

expression1 = expression1 operator expression2

 expression1 operator  =  expression2

Expression

Equivalent

x *=  y

x  =  x  *  y

y -= z + 1

y  =  y – (z + 1)

a  /  =  b

a  =  a  /  b

x  +=  y / 8

x = x  +  (y / 8)

y  %=  3

y  =  y  %  3

 

Table 3.12: Examples of compound assignment operator

If             x = 12;

Then,

z = x += 2;

z =  x = x + 2

          =   12 + 2

          =   14

#include <stdio.h>

 

int main()

{

    int a = 3, b = 4;

 

    printf("Initially: a = 3, b = 4\n");

    printf("\na += b ---> a = a + b = %d\n", a+=b);

    printf("a last value = %d\n", a);

    printf("\na *= b ---> a = a * b = %d\n", a*=b);

    printf("a last value = %d\n", a);

    printf("\na -= b ---> a = a - b = %d\n", a-=b);

    printf("a last value = %d\n", a);

    printf("\na/=b ---> a = a / b = %d\n", a/=b);

    printf("a last value = %d\n", a);

    printf("\na-=(b+1)---> a = a - (b + 1) = %d\n", a-=(b+1));

    printf("a last value = %d\n", a);

    return 0;

}

 

Output:

 

C C++ compound statement

 

3.10    The Conditional Operators

Expression1  ?  expression2  :  expression3;

x  =  y  ?  1  :  100;

 

                Assign value 1 to x if y is true.

                Assign value 100 to x if y is false.

z = (x > y)? x : y;

                if(x > y)

                    z  =  x;

                else

                    z  =  y;

#include <stdio.h>

 

int main()

{

    int a, b = 4, c= 50;

 

    // here b is less than c, so the statement

    // (b>c) is false, then 200 should be assigned

    // to a, reflected through the output

    a = (b>c) ? 100 : 200;

    printf("Given: b = 4, c= 50\n");

    printf("The statement:a = (b>c) ? 100 : 200\n");

    printf("will be evaluated to a = %d\n", a);

    return 0;

}

 

Output:

 

C C++ conditional operator output snapshot

 

C & C++ programming tutorials

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Related C and C++ reading and digging:

  1. Check the best selling C/C++ books at Amazon.com.

  2. The source code for this Module is: Module 3 C/C++ source codes. and the related worksheet for your practice is: C lab worksheets.

 

 

 

 

 

|< C/C++ Statements, Expressions & Operators 1 | Main | C/C++ Statements, Expressions & Operators 3 >| Site Index | Download |


C and C++ Statements, Expressions & Operators:  Part 1 | Part 2 | Part 3 |