This shows you the differences between two versions of the page.
cs:c_language:boolean_expressions [2019/02/26 14:35] |
cs:c_language:boolean_expressions [2020/11/26 23:18] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Boolean Expressions ====== | ||
+ | **Concepts: | ||
+ | Boolean expressions (and '' | ||
+ | **Text:**\\ | ||
+ | Some examples regarding boolean expressions, | ||
+ | |||
+ | Examples have been explained with comments in the code. | ||
+ | |||
+ | **Solution: | ||
+ | |||
+ | <file C boolean_expressions.c> | ||
+ | /* Examples regarding boolean expressions */ | ||
+ | |||
+ | #include < | ||
+ | |||
+ | |||
+ | int main() { | ||
+ | | ||
+ | int res; | ||
+ | int x = 2, y = 3; | ||
+ | |||
+ | /* In C, a number different than 0 is a TRUE value */ | ||
+ | if (-10) { | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | /* A false condition in C is the number 0 */ | ||
+ | if (0) { | ||
+ | printf(" | ||
+ | }else{ | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | | ||
+ | res = 3+2*5; /* res=13 */ | ||
+ | if (res) { /* 13 is a True value */ | ||
+ | printf(" | ||
+ | } | ||
+ | |||
+ | /* res that is equal to 13 is greater than the 7. | ||
+ | The comparison is therefore a TRUE value, but | ||
+ | NOT TRUE = FALSE */ | ||
+ | if ( !(res> | ||
+ | printf(" | ||
+ | }else{ | ||
+ | printf(" | ||
+ | } | ||
+ | | ||
+ | /* 2 > 5 is FALSE and 3 < 4 is TRUE, i.e, | ||
+ | FALSE OR TRUE = TRUE, therefore the | ||
+ | | ||
+ | than 0, in this case it contains the | ||
+ | value 1 */ | ||
+ | res = 2 > 5 || 3 < 4; /* 0 OR 1 */ | ||
+ | printf(" | ||
+ | |||
+ | /* x=2; y=3; | ||
+ | 2 >= 3 is FLASE, 3==3 is TRUE, 2!=3 is FALSE | ||
+ | | ||
+ | | ||
+ | | ||
+ | res = (x >= y && x+1==y) || !(x!=y); | ||
+ | printf(" | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | **Output: | ||
+ | The output of this program is: | ||
+ | <code bash> | ||
+ | A: True | ||
+ | B: False | ||
+ | C: True | ||
+ | D: False | ||
+ | E: 1 | ||
+ | F: 0 | ||
+ | </ |