Lesson 7: Assigning, Comparison, IF Conditions


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



Seventh lesson on Assigning Operators, Comparison Operators, If conditional and coding is up! Remember to Bookmark (CTRL + D) my website if you find it helpful. Also please point on errors and mistakes you encounter reading my tutorials. Remember, English is not my native language so here and then it can be problematic to understand a word or two.




ASSIGNIN OPERATOR

Assigning Operator
we use in C programming is =. This operator is different than conditional operator used to compare two values (==). Assigning Operator assigns value on right side (r-value) to operand on left side (l-value). Difference between l-value and r-value is that l-value operand has to have well defined address even after assigning value to it.


Example:


We can put:

a = b + 3;


but we can’t:

b + 3 = a;


C language also allows multiple assigning:



Example:



a = b = c = 0;


All three variables are initialized to 0 value. Priority of assigning is from right to left as if we wrote in it this way:
a = (b = (c = 0));
First we assign 0 to variable c, then to value of this whole expression (c = 0) is assigned value 0; then value of this expression is assigned to variable b, then this whole expression is valued 0, and assigned to variable a.

a = b = c + 3;



Is this possible?


a = b + 3 = c = d * 3; // NO IT ISN’T!!! (b+3 isn’t l-value)


In programming we often calculate new value of variables based on value of the other variables. This is why C also allows shorter equations for assigning..



Some of the operators used for shorter assigning:
+=, -= , *= , /= , %=



Example:



Phrase:

i = i + 5;

can also be written this way:


i += 5;




Phrase:


i = i * a;

can also be written this way:


i *= a;




OPERATORS FOR HIGHERING AND LOWERING PARTICULAR VALUE

C language has two operators that are able to resize variable’s value to higher or lower value. Cause of their existence is to provide more efficient executions (there are some special processor instructions that are capable executing this operations very fast).

  • prefixing (operator in front of variable)
++a; --a;

  • post fixing (operator after variable)
a++; a--;


Using
++ or operators with simple numeric types (variants of char, int and real types), resizes to higher or lower value just by one. These operators are useful when working with index fields, pointers and counters inside a loop.


It is advised to use prefix operators, because they are more efficient (postfix operators create a temporary variable where they assign previous operand’s value, which is used through the whole expression).



Example:



a = 5;

b = ++a * 2;
c = b++;

After execution of these orders, a gets value 6, b gets value 13, and c gets value 12



Its better to avoid dual use of these operators. Example:


a = fn1( i++ ) + fn2( i++);

In this expression we’re not sure which function will be processed first..




COMPARISON OPERATORS

Comparison operators are used when we need to compare data. We can compare constants, variables and phrases. Program language C has following comparison operators:







Comparison operator as a result gives TRUTH (value different than 0) or FALSE (zero). C language doesn’t support logical type of data; instead logical values are shown with help of numerical values.




CONDITION PHRASE IF

Comparison operators are usually included in condition phrases. Depending on a comparison operator’s result, the phrase gets executed or doesn’t. C language supports three shapes of conditional IF phrases.


  1. Simple selection

  2. Two-sided selection

  3. Multi-sided selection



Example:


What’s the result we get printed on screen, after this block of orders is executed?

1.) a = 25 and b = 4.
2.) a = 0, b = 0

if (a = b)

printf ("Values of both variables are equal\n");

else
printf ("Values of both variables are different\n");



Result
:


1) When
a = 25 , b = 4, after assigning a = b, a gets value 4 and the whole expression gets value 4. Block of orders after if is executed only if statement is TRUE (different than zero, our expression is 4) it will be printed: "Values of both variables are equal".

2) After assigning whole expressions equals zero, so
"Values of both variables are different" is going to be written:



Question
: What would happen in both situations, if the condition were (a == b)?


Answer
: Since we have logical comparison operator, (not assigning), the result is printed:

1)
"Values of both variables are different"

2)
"Values of both variables are equal"



Important:


if (a = b)

is the same as:

a = b; if (a)




Example:



Write your own program block that will add int variables a and b if the value of char variable c is equal to '+'. If the value of c is '-', then it will subtract mentioned variables. If the value of c is some other sign, then program must return error sentence. Result must be assigned to variable r.

int a,b,r;
char c;

if( c == '+' )
r = a + b;
else if( c == '-')
r = a - b;
else
printf("Error – wrong operation");




Example:


Write your own program that will calculate crossing point of two lengths in a real number axis (assuming that the first length is left on the axis and the second right on the axis).

Important:

Pay attention in following example that "else" belongs to second "if condition", and not the first one. If else condition is needed to be assigned to first (outer) if condition, then whole block of orders after first if order must be placed inside brackets { }.




int a1, a2, b1, b2; // lengths [a1, a2] and [b1, b2]
int r1, r2; // result [r1, r2]


if( a2 > b1 ) /* means they are crossing each other */
if( a1 > b1 ) /* means whole length A is inside length B */
{
r1 = a1;
r2 = a2;
}
else
{
r1 = b1;
r2 = a2;
}












9 Responses to “Lesson 7: Assigning, Comparison, IF Conditions”

  1. Anonymous laufer 


    a = 5;
    b = ++a * 2;
    c = b++;

    After execution of these orders, a gets value 6, b gets value 13, and c gets value 12


    Don't you think b gets 12, and c gets 13?

  2. Anonymous vurdlak 

    No, because its stated b++, which is different than ++b. (post block asignning)

    In this case c is valued b (c=12), then after whole program block b is valued 12 + 1=13

    This is the procedure:

    a = 5; // a =5, b=?, c=?
    b = ++a * 2; // a=6, b=12, c=?
    c = b++; // a=6, b=12, c=12

    // a=6, b=13, c=12

  3. Anonymous Pavel 

    if( a2 > b1 ) /* means they are crossing each other */

    Actually this does not necessarily mean that the segments intersect, as it could also be true that b2 < a1. I think this problem requires a little more attention.

  4. Anonymous Pavel 

    I was wrong, actually, I missed the assumption on the position of the segments on the axis.

  5. Anonymous Mortis 

    1. a = 5;
    2. b = ++a * 2;
    3. c = b++;

    After execution of these statements:
    a=6
    b=13
    c=12

    After executing statement 1:
    a = 5; results in a = 5, b = ?, c = ?

    The execution of statement 2 can be divided into several statements to show it more clearly.
    The ++a operator causes the following order, if the '++' operator would follow the operand (a), rather than precede it, then the execution of the following statements would be the reverse.
    a = a + 1; results in a = 6, b = ?, c = ?
    b = a * 2; results in a = 6, b = 12, c = ?

    The execution of statment 3 would look like this when divided:
    c = b; results in a = 6, b = 12, c = 12
    b = b + 1; results in a = 6, b = 13, c = 12.

    So the above is correct, I hope this provides some clearification.

  6. Anonymous ton 

    Congratulations... this tutorial is very simple.
    but the if conditions can be condensed this is possible as:

    'normal if:
    int a = 2
    char b = '';
    if (a>0)
    {
    b = 'true condition return';
    }
    else
    {
    b = 'false condition return';
    }

    ' condensed if:
    int a = 2;
    char b = ( a>0 ? 'true condition return' : 'false condition return' )

  7. Anonymous Trace 

    Very useful, thanks. :)

  8. Anonymous tatarao 

    if(text comdition)
    {
    dothis
    and this
    }

  9. Anonymous Mr. Shrikant 

    I like it very much

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




Daily Lessons for programming in Visual Studio, using C code.