What is the result of logical or relational expression in C True False 0 or 1 0 if an expression is false and any positive number if an expression is true?

Introduction to C Programming Decision and Branching Concepts

Outline:
  • Boolean type
  • Relational Operators
  • Logical Operators
  • if statements

Boolean Type ( or lack thereof in C ) and stdbool.h

  • A true boolean data type could be used for storing logical values, and would only have two legal values - "true", and "false".
  • C does not have boolean data types, and normally uses integers for boolean testing.
    • When a programmer is setting values, set 0 for false and 1 for true
    • When the computer interprets them, 0 is false and nonzero is true
    • To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.
      • In the old days, this was done using #define:
        • #define true 1
        • #define false 0
      • Today it is better to use const int instead of #define:
        • const int true = 1;
        • const int false = 0;
      • C99 provides a new header, stdbool.h, that defines some new helpful types and constants:
        • The type "bool" is the same as a newly defined type "_Bool"
          • _Bool is an unsigned integer, that can only be assigned the values 0 or 1
          • Attempting to store anything else in a _Bool stores a 1. ( Recall that C interprets any non-zero as true, i.e. 1 )
          • Variables can now be declared of type "bool".
        • stdbool.h also defines constants "true" and "false", with value 1 and 0 respectively.
        • Use these new features using #include <stdbool.h> at the top of your program

Relational Operators

  • Relational operators are binary operators that evaluate the truthhood or falsehood of a relationship between two arguments, and produce a value of true ( 1 ) or false ( 0 ) as a result.
  • The following table shows the 6 relational operators and their meaning, by example:

Operator

Meaning

A > B

True ( 1 ) if A is greater than B, false ( 0 ) otherwise

A < B

True ( 1 ) if A is less than B, false ( 0 ) otherwise

A >= B

True ( 1 ) if A is greater than or equal to B, false ( 0 ) otherwise

A <= B

True ( 1 ) if A is less than or equal to B, false ( 0 ) otherwise

A == B

True ( 1 ) if A is exactly equal to B, false ( 0 ) otherwise

A != B

True ( 1 ) if A is not equal to B, false ( 0 ) otherwise

  • DANGER DANGER: Do not confuse the equality test operator, ==, with the assignment operator, =. It is a VERY common error to use the wrong number of equals signs, ( either too many or too few ), and the resulting code will usually compile and run but produce incorrect ( and often unpredictable ) results.
    • Example: "while( i = 5 )" is an infinite loop. i=5 is an assignment operator which returns the value being assigned (e.g. 5), which is an integer. But a while statement expect a boolean. But C doesn't have any real boolean type - booleans are just integers. Therefore, type checking does not save you. 5 is nonzero, so it always evaluates to true. The programmer probably meant to use "while( i = = 5 )", which will test whether or not i is equal to 5, and will only continue to loop as long as this is true.
  • <, <=, >, and >= have equal precedence, higher than = = and !=, which have equal precedence with each other. All these operators are evaluated left to right.
  • Note: As a general rule, floating point numbers ( e.g. floats or doubles ) should NOT be tested for exact equality, because roundoff makes it virtually impossible for two doubles to ever be exactly equal. Instead one checks to see if the two numbers are acceptable close to each other:
    • double x, y, tolerance = 1.0E-6;
    • BAD: if( x = = y )
    • GOOD: if( fabs( x - y ) < tolerance )
    • This is particularly important when testing the stopping condition of an iterative algorithm, because otherwise the roundoff error could cause the program to run forever. WRONG: while( estimatedError != 0.0 )

Logical Operators

  • Logical operators are used to combine and evaluate boolean expressions, generally in combination with the relational operators listed above.
  • The following table shows the 3 logical operators and their meaning, by example:

Operator

Meaning

A && B

A AND B - True ( 1 ) if A is true AND B is also true, false ( 0 ) otherwise

A | | B

A OR B - True ( 1 ) if A is true OR B is true, false ( 0 ) otherwise

! A

NOT A - True ( 1 ) if A is false ( 0 ), false ( 0 ) if A is true ( non-zero ).

  • Note: There should not be a space between the | characters in | |, but one is being added here for clarity.
  • DANGER DANGER: If you only use a single & or | instead of the double && or | |, then you get valid code that is beyond the scope of this course, but which will not do what you want.
  • ! has a very high precedence, higher than the relational operators listed above. && and | | have lower precedence than the relational operators, with && having higher precedence than | |.
  • Example: To determine whether x is between 5.0 and 10.0:
    • INCORRECT: 5.0 < x < 10.0
    • CORRECT: 5.0 < x && x < 10.0
    • Question - What is the value of the incorrect expression ( regardless of the value of x ), and why?
  • Example: Leap year occurs when the current year is divisible by 4, but NOT when the year is divisible by 100, UNLESS the year is also divisible by 400.
    int year;
    bool leap_year;
    
    leap_year =  ( ( ( ( year % 4 ) == 0 ) && ( ( year % 100 ) != 0  ) ) || ( ( year % 400 ) == 0 ) ); 
    
    // Or equivalently:
    
    leap_year = (( !( year % 4 ) && (year % 100)) || !( year % 400 ));
  • Short Circuiting of Logical Operations:
    • In a complex logical expression, the computer will stop evaluating the expression as soon as the result is known.
    • "A && B" will not evaluate B if A is already false.
    • "A | | B" will not evaluate B if A is already true.
    • Example: "if( x != 0.0 && 1.0 / x < 10.0 )" will never divide by zero.

if

There is a broad category of conditional statements - they all hinge on a conditional that evaluates to true or false. Details:
  • A condition is any expression that evaluates to true (non-zero) or false (0)
  • BB can be a single instruction or multiple instructions. The curly braces ({ }) are only strictly necessary when BB contains multiple instructions.
  • control flow are depicted in control-flow diagrams. Diamonds are decisions and rectangles are actions. Arrows show the sequence of actions that occur.
  • This CFG shows only the one part of the program - it is assumed that there is code before and after the control structure.
  • These can be composed in several ways, as we will see.
How it works:

If the condition is true, the BB will execute and then continue with code after the close curly brace. Otherwise, it will skip over BB and go directly to the code after the curly brace.


When to use this construct:

When you want something to happen in a special case. Otherwise, you do the normal thing.

What is the result of logical or relational expression in C answer?

The result of a logical operation is either 0 or 1. The type of the result is int . The logical-AND operator produces the value 1 if both operands have nonzero values. If either operand is equal to 0, the result is 0.

What does a logical operator return for a true condition 0 or 1?

The logical operators return TRUE or FALSE, which are defined as 1 and 0, respectively, depending on the relationship between the parameters. /is the logical not operator. && is the logical and operator. It returns TRUE if both of the arguments evaluate to TRUE.

What is the result of logical or relational operator?

Relational operators compare values and return either TRUE or FALSE. Logical operators perform logical operations on TRUE and FALSE. Values used with a logical operator are converted into booleans prior to being evaluated. For numerical values, zero will be interpreted as FALSE, and other values will be TRUE.

Which logical operation outputs true if and only if one or more of its operands is true?

The logical OR ( || ) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value.