Decision Making, Conditions, and Relational Operators

Share this post:

Condition

  • A condition is any statement or expression than can either be true or false, but not both. As expected, for this statement to be true or false, there is a premise that must be fulfilled for the condition to be true, otherwise, it is false because the condition has not been fulfilled.
  • In C language, the condition allows a program to make a decision. This means that the program has two alternative outputs already pre-programmed, and the condition allows the user to receive only one of the two outputs, but not both. For example, consider the scenario where the pass mark for a test is set at 70%. The condition will then be: Is the mark obtained in a test above 70% or less? If the mark is above 70%, the student is assigned a pass, and if it is lower, then the student is assigned a fail. As is clear, this student cannot be assigned both a pass and a fail by the program.
  • Any statement that instructs the computer to perform an action (such as receive input, deliver output, or calculate a value) and then make a decision is called an executable statement. This allows a condition to be redefined as any executable statement that can be true or false based on a set premise.
  • In the condition, there are 2 types of executable statements – the if statement and the else statement. Only one of these statements provides the output.
  • To form a condition, there is a need to use a unique set of operators called relational operators.

Relational Operator

  • A relational operator shows the relationship between two values. For this reason, it is also called a comparison operator. It is placed between these values using this syntax:
value1 relational_operator value2
  • What differentiates the relational operator from the binary arithmetic operator is that the relational operator does not perform any calculation, it just reveals how the value to the left of the operator is related to the value to its right. The binary arithmetic operator is used to perform a calculation to create a new value as the output.
  • There are two types of relational operators:
    • Numerical Inequality Operator: This is used to show the non-equal comparison between two values e.g 1>0. It serves to reveal if one value is larger or smaller than the other. The inequality operator can belong to one of two categories:
      • Strict Inequality: This operator excludes any possibility of the compared values having any equivalency. For example, 1>0 shows that 1 is greater than 0, and can never be equal to 0. The two main notations for the inequality operators are:
        • < denotes less than. It shows that the value to its left is less than the value to its right. The symbol << is used to denote much less than in engineering sciences.
        • > denotes greater than. It shows that the value to its left is greater than the value to its right. The symbol >> is used to denote much greater than in engineering sciences.
      • Non-Strict Inequality: This operator includes the possibility of the compared values having an equivalency. For example, a≥b shows that a is greater than or equal to b, and can never be less than b. The two main notations for the inequality operators are:
        • denotes less than or equal to. It shows that the value to its left is less than or equal to the value to its right. The symbol is written in a straight-line form as <= with the inequality symbol always being the first character.
        • denotes greater than or equal to. It shows that the value to its left is greater than or equal to the value to its right. The symbol is written in the straight-line form as >=.
    • Numerical Equality Operator: This operator is used to show an equation or an inequation. This operator shows whether two values are equal or not, not if one value is larger or smaller than the other. For example, a=1 means that a has only one value and that value is 1; while a≠1 means that a can be any value that is not1. There are only two equality operators:
      • The EQUAL TO operator is denoted by the = symbol in mathematical operations, and == in the C language.
      • The NOT EQUAL TO operator is denoted by the symbol in mathematical operations and != in the C language.
  • The numerical inequality operator has a higher level of precedence as compared to the numerical equality operator. Regarding associativity, both groups of operators group from left-to-right.
  • Relational operators have a lower level of precedence compared to binary arithmetic operators.
  • The condition can be expressed as any expression whose output can generate zero i.e output == 0 to denote false (i.e condition has not been met/fulfilled) or generate a non-zero value i.e output != 0 to denote true (i.e condition has been fulfilled).

If Statement

  • It is mandatory that any condition include an if statement.
  • The if statement is an executable statement that uses the if keyword to initialize a conditional function.
  • The conditional function delivers a predetermined output when the condition is proved to be true e.g displaying a text on the screen. In the Calculator program, an example of an if statement is:
if (number1 != number2)
{
printf("First Number is NOT equal to the Second Number. \n");
}
  • The syntax of the if statement is:
if (value1 relational_operator value2) //This is the if condition/conditional
{
if statement;
}

  • The use of curly braces is not mandatory, but they improve program readability.
  • Placing the semicolon after the right bracket/parenthesis voids the if statement by allowing it to execute regardless of whether the condition is met or not. In fact, the semicolon terminates the if statement as an empty statement. It is for this reason that using braces is encouraged as a semicolon before a brace can easily notify the programmer of a lexical error.
  • The if statement considers only one condition, and this capability can be extended to consider two conditions or multiple conditions. The if statement can be extended to consider two conditions if it is converted into an if/else (also written as if-else or if…else) statement. Likewise, the if statement can be extended to consider multiple conditions if it is converted into a switch statement.
  • The if statement, if/else statement, and switch statement are categorized as selection statements because they allow the program to select an action to perform from the set of preprogrammed actions.

Leave a Reply

Discover more from Kagirison

Subscribe now to keep reading and get access to the full archive.

Continue reading