top of page
Search

Let us C with MP.T_Part. 2

Haven't read the first blog?


In continuation of the previous blog, let's dive deeper into the language.


TYPES OF INSTRUCTIONS

  1. Type Declaration Intructions - This intruction is used to declare the type of variables used in C.

  2. Arithmetic Instructions - This instruction is used to perform arithmetic operations on constants and variables.

  3. Control Instructions - This instruction is used to control the sequence of execution of the various statements.


Type Declaration Instructions

  • It is used to specifiy which type of variables is used

Egs-- int base; // this means base variable stores only integers.

float num1, num2; // num1 and num2 are two variables which store real numbers.

  • We can also initialize the variables while declaring the data type.

    Egs -- int i = 10, j = 20;


  • It is always important to declare a variable before using it

    Egs -- int a = b + 0.5 , b = 7; // this is wrong bc 'b' is used before declaring it.


  • The following statement would work

int a,b,c;

a = b = c = 10;

but this won't

int a = b = c = 10; // again we are trying to use 'b' and 'c' (to assign 'a') before defining it.


Arithmetic Instructions

It consists of a variable in the LHS and other variables, constants (also called operands) with operators on the RHS.

Egs --

The output -

There are different types of operators --

<Modulus % calculates the remainder when two numbers are divided>


Hierachy of Operations

so if I write

int i = 10 + 20 / 5 - 5;

It will do -- 10 + (20 / 5) -5


When operations of same priority are used, Law of Associavity

All operators in C have either Left to Right associavity or Right to Left associavity.

Egs --a) 10/5*7; / and * have same priority and both enjoy Left to Right associavity. First division is sone followed by multiplication.

b) a = c = 10; Here both assignment operators have same priority and = associates from Right to Left. Therefore, second = is performed earlier than first =.


ASSIGNMENT OPERATORS

Used in comparison or assigning two operands.


These help us compare and return true and false values.



Next type of operators are LOGICAL OPERATORS

AND is the mathematical equivalent of INTERSECTION

OR is the mathematical equivalent of UNION

NOT does the opposite of the statement mentioned (exchanged True and False)


Truth tables

Here 1 means True, and 0 means False.


AND -- only if both conditions are satisified the statement in executed.

OR -- If any one condition is satisfied, the statement is executed.

NOT -- Whatever condition is given, if opposite is true, then executes the statement.


We will look at the usage of all these operators in conditional statements in the next blog!

Now a meme to end this one...


 
 
 

Comments


bottom of page