Lesson 8: Conditional and Logical Operators
Published Saturday, February 18, 2006 by Vurdlak | E-mail this post
It is weekend, but I won't give you day off... Instead I'll provide you with new lesson about Logical and Conditional Operators, along with some practical program coding and few interesting examples. You'll notice when you follow this lesson you'll have no problem following the text and learning more "advanced" steps. You'll already be ready for writing your own simple lines of C language!
CONDITIONAL OPERATOR
Conditional Operator (?:) is ternary operator (demands 3 operands), and is used in certain situations, replacing if-else condition phrases. Conditional operator’s shape is:
Condition_phrase ? phrase1 : phrase2;
If conditional_phrase is true, phrase1 is executed and whole phrase is assigned with value of this phrase1. If the conditional phrase is false, then phrase2 is executed, and whole phrase is assigned with phrase2 value.
Example:
int a, b, c;
...
c = a > b ? a : b; // if a>b "execute" a, else b
PRE-PROCESSOR’S COMMANDS
Before source code is translated (compiled), to a language “machine” understands, pre-processor is started. Depending on pre-processor’s commands, pre-processor changes the source code, and this changed code is now ready to be translated. Changes pre-processor made are saved into temp file, and the source code file is left untouched. Some of pre-processors commands are:
#include includes other files and links it with program
#define defines symbolic constant or so called macro
#include <stdio.h>
#include "MyFunctions.h"
#define PI 3.14159
#define PROGRAM NAME "Program"
With little help of pre-processor’s "define" command, macros can be produced (they remind of function but they aren’t one).
Example:
What’s the value assigned to b variable after following commands?
int a, b;
#define MAX(x,y) ((x) > (y)) ? (x) : (y)
a = 3;
b = MAX (3, a++);
/* Pre-processor physically changes this line with following:
b = ((3) > (a++)) ? (3) : (a++);
In the end values are
a = 5, b = 4 */
a = 3;
b = MAX (a++, 3);
/* Pre-processor physically changes this line with following:
b = ((a++) > (3)) ? (a++) : (3);
In the end values are
a = 4, b = 3 (3++ > 3) ? 3++ : 3*/
LOGIC OPERATORS
Simple relation phrases can be combined into more complex ones with help of logic operators. C language includes 3 logic operators.
Pay attention to difference between logic operators and logic operators above bits!
Example:
if ((x>20) && (x<100)) printf("x is inside open interval 20-100");
if ((x<5) || (x>20)) printf("x is not inside closed interval 5-20");
if (!(x>20)) printf("x is smaller or equal to 20");
Example:
What does following program block do?
int a, b, c, d;
a = 0;
b = 4;
c = (a++) + b; /*(a++) +b 0+4 4*/
printf ("a = %d, b = %d,c = %d " , a, b, c); /* 1, 4, 4 */
d = c && b + 3 * a;
printf ("d = %d", d);
Because priority of arithmetic operators is bigger then priority of logical operators:
d = c && b + 3 * a /* this is the same as expression in next row */
d = c && (b + (3 * a))
d = 4 && (4+ (3 * 1))
d = 4 && 7
d = 1
Complex phrases are often wrongly stated because people tend to “literally rewrite” stated conditions:
Statement “if x is higher than 20 and lower than 100” (pay attention that “lower than 100” reefers to x smaller than 100), is often wrongly (literally) written:
If (x>20 && <100) instead of correct one: If (x>20 && x<100)
This produces error translation. Another thing, people often mistake putting “,” instead of operator “AND”, so previous statement is again wrongly interpreted:
if(x>20 , x<100)
which produces “logic” error (which is much harder to notice, since compiler - translator program, can’t find it and report as error) .
Phrase "x>20,x<100" only matches the statement "x<100".
OPERATORS PRIORITY AND ASSIGNMENT
Term “assignment” reefers to assigning two operators of the same priority, one after another (from left to right and sideways). Table of priorities and assignment of all previously mentioned operators:
Pay attention that all comparing operators don’t use same priority!
Example:
Write your own program which calculates results (roots) of square equation (image placeholder).
Additional Info:
- First #include <stdio.h> command "includes" header file with functions and constants description which reefer to work with input/output (inputting from keyboard/printing on screen).
- Second #include <math.h> command "includes" header file with description of mathematical functions and constants.
- When translating the code (linking), MS Visual C++ 6.0 includes data files with implementation of mathematical functions, by definition. When translating it isn’t necessary to give any additional parameters. When working with some Unix OS programming tools, C translator doesn’t include that data file by definition, so you have to add "-lm" parameter (option) manually (when starting translation).
#include <stdio.h>
#include <math.h>
main() {
float a, b, c, x1, x2, q, d, x1r, x2r, x1i, x2i;
printf("Input coefficients of square equation a,b,c:");
scanf("%f %f %f", &a, &b, &c);
d=b*b -4.0*a*c;
if (d > 0) {
/* results are real numbers */
q = pow (d, 1./2);
x1 = (-b + q)/(2*a);
x2 = (-b - q)/(2*a);
printf ("X1=%f X2=%f\n", x1, x2);
} else if (d == 0) {
/* there’s only one result */
x1 = -b/(2*a);
printf ("X1=X2=%f\n", x1);
} else {
/* results are conjugated complex numbers */
q = pow(-d, 1./2);
x1r = -b/(2*a) ;
x2r = x1r;
x1i = q/(2*a);
x2i = -x1i;
printf ("X1 = (%f,%f)\n", x1r, x1i);
printf ("X2 = (%f,%f)\n", x2r, x2i);
}
}
Technorati Tags: Complier, Programming, Example, Source+Code, Operator, Logical, Condition
Daily Lessons for programming in Visual Studio, using C code.
If the images are hard to read simply open them in new window. Then you will see them in actual size
if (!(x>20)) printf("x is smaller or bigger than 20");
The printed statemend isn't exactly right. It should read "x is smaller or equal to 20". Everything else, though, is crystal clear.
Corrected it! Thank you! I always appreciate when readers point to my mistakes to make this articles better!
Well, this isnt really and i dont have a horrible lot of programming experince but i think your code is sorta hard to read in certian snippets. Especially for new comers
if (d > 0) {
/* results are real numbers */
q = pow (d, 1./2);
x1 = (-b + q)/(2*a);
x2 = (-b - q)/(2*a);
printf ("X1=%f X2=%f\n", x1, x2);
} else if (d == 0) {
/* there’s only one result */
x1 = -b/(2*a);
printf ("X1=X2=%f\n", x1);
} else {
/* results are conjugated complex numbers */
q = pow(-d, 1./2);
x1r = -b/(2*a) ;
x2r = x1r;
x1i = q/(2*a);
x2i = -x1i;
printf ("X1 = (%f,%f)\n", x1r, x1i);
printf ("X2 = (%f,%f)\n", x2r, x2i);
}
}
might be better as
if (d > 0)
{
/* results are real numbers */
q = pow (d, 1./2);
x1 = (-b + q)/(2*a);
x2 = (-b - q)/(2*a);
printf ("X1=%f X2=%f\n", x1, x2);
}
else if (d == 0)
{
/* there’s only one result */
x1 = -b/(2*a);
printf ("X1=X2=%f\n", x1);
}
else
{
/* results are conjugated complex numbers */
q = pow(-d, 1./2);
x1r = -b/(2*a) ;
x2r = x1r;
x1i = q/(2*a);
x2i = -x1i;
printf ("X1 = (%f,%f)\n", x1r, x1i);
printf ("X2 = (%f,%f)\n", x2r, x2i);
}
}
i think the tabs can add some clarity really i guess this is more important when you get to some more nested type structures but still its easier to follow this way imho
oh your deal kill the whitespace that might account for some issues im seeing...
what is whitespace you reefer to? yeah i see code will be re formatted just when i figure how!
[CODE] in some forums you can use tags like these for that kind of stuff...seems like a "visialcplus blogspot whould have something like this
maybe not though
[/CODE]
hi...juz wan ask what is the mean of "amp"? what is the use of that words??please reply soon...thanks...