4. Rational Decision Making

4. Rational Decision Making

4. Rational Decision Making.

 
Dr. Grace Hopper was a pioneer of computer programming who came up with one of the first linkers. She finished a program that let a user use English words instead of numbers for giving instructions to the computer.

Dr. Grace Hopper was a pioneer of computer programming who came up with one of the first linkers. She finished a program that let a user use English words instead of numbers for giving instructions to the computer.

Untitled.png

In the previous tutorial, we explored the basics of some programming operators which are Arithmetic, Assignment, and Comparison operators. In this tutorial, we will continue our discussion about Logical and Bitwise operators.

Logical Operators.

As we discussed with programming operators, we can use comparison operators to create an expression that produces a boolean value (true or false). If an expression is true, 1 is returned; if the expression is false, 0 is returned.

For example:

// expression

5 > 3

// produces a boolean value of true

We can use logical operators to combine two or more expressions to make one big expression.

alogical table.png

The logical operators are commonly used in decision-making or in what we call conditional statements. Let’s see an example to clear up the operators shown in the table above.

Suppose, a = 5 and b = 8. Then,

AND operator Example:

(a > 3) && (b > 5) 
// evaluates to true

(a > 3)  && (b < 5) 
// evaluates to false

OR operator Example:

(a > 3) || (b > 5) 
// evaluates to true

(a > 3) || (b < 5) 
//evaluates to true

(a < 3) || (b < 5) 
// evaluates to false

NOT operator Example:

!(a < 3) 
// evaluates to true

!(a > 3) 
// evaluates to false

Now, let’s write some code to display these operators in our compiler. Hit the run button to see the output in the console below.

First, we start with the Logical AND operator. This operator returns true when all conditions are evaluated to true and returns false when one or more than one condition is evaluated to false.

Access the full Thinking Cap learning experience by signing up to Propel for FREE!

The code above is completely editable, so try different numbers inside the if statement and hit run to see your changed output.

It’s time to break down the code above to see what each line means.

#include 

int main(void){your code goes here}

We covered these lines in Hello Worldtutorial. Check it out!

int a = 3;

As we explained in the ‘Variables’ tutorial, this part is called declaring and assigning our variables. In this case, we declared ‘a’ as our variable and then we assigned 3 as its value. Check previous tutorials to make sure you know every step of declaration and assignment of variables.

if ( a==3 && a!=7) {
  printf("your desired output")
}

As we explained the main concept of if statements in the previous tutorial, whatever is mentioned inside the braces after if, will get evaluated to either True or False. If the expression inside the braces gets evaluated to true, we will execute everything in the curly braces {}. So, in this case, the printf(“the statement is correct”) code will be executed. And if it gets evaluated to false, everything after curly braces will be executed, which would be printf(“the statement is not correct”).

In this example, we are using the Logical AND operator and this will only get evaluated to true when the two expressions (a==3 and a!=7) are correct.

Note: a==3 means ‘a’ equals 3, and a!=7 means ‘a’ does not equal 7.

Practise: what will be the output if you put a<=5 instead of a!=7? Change the input in the compiler above and hit the run button to see if you got the right answer!

The next operator that we will be talking about is the Logical OR operator. This operator returns true when one or more than one condition is evaluated to true and returns false when all conditions are false.

Access the full Thinking Cap learning experience by signing up to Propel for FREE!

Hit the run button to see what you will get as your output.

Remember we talked about all the lines in the compiler above earlier in this tutorial? Well, they look sort of similar, right? the only difference that you would notice is using || instead of &&.

As you can see, we are using the OR operator here. So, if any of the expressions mentioned in the if statement (a==3, a>=4) is evaluated to true, the code inside the curly braces would be executed (print(“the statement is correct”)).

Finally, we get to the NOT operator. This operator returns true when a condition is false and returns false when the condition is true. Let’s see an example to clear this up:

Access the full Thinking Cap learning experience by signing up to Propel for FREE!

As you can see in the code above, ‘a’ equal 3. So, the expression inside the if statement is evaluated to true. The NOT operator executes true expressions as false, that is why we got the statement is not correct in our console.

Let’s do a quick exercise before we continue our discussion.

aaaLogica .png
Sign up for free and test your knowledge!

Bitwise Operators

The Bitwise operator is used to perform operations on individual bits. They are only with char and int data types. The first Bitwise operator we will be talking about is AND (&) operator.

And Operator:

  • It takes two bits to perform AND operation.

  • It is a binary operator which means it takes two numbers to do the operation.

  • It returns 1 if only if both operands are 1.

abitwise.png

Let’s see an example to fully understand the Bitwise AND operator:

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

 //Bitwise AND Operation of 12 and 25

      00001100

&    00011001

     _________

     00001000  = 8 (In decimal)

OR Operator:

  • It takes two bits to perform OR operation.

  • It is a binary operator which means it takes two numbers to do the operation.

  • It returns 1 if one of the operands is 1.

aorbitwise.png

Let’s see an example to make sense of the Bitwise OR operator:

12 = 00001100 (In Binary)

25 = 00011001 (In Binary)

  //Bitwise OR Operation of 12 and 25

     00001100

| 00011001

    _________

    00011101  = 29 (In decimal)

NOT Operator

  • It requires only one operand to perform the operation (unary operator)

  • It changes every 1 to 0 and 0 to 1.

Let’s see an example to get the point of the Bitwise NOT operator:

35 = 00100011 (In Binary) 

// Using bitwise complement operator

~ 00100011 

 __________

  11011100

Related Posts