3. Demystify Programming Operators

3. Demystify Programming Operators

3. Demystify Programming Operators.

 
Ada-Lovelace.jpeg

Ada Lovelace, first person to publish an algorithm that was executed by the first modern computer. She started studying mathematics and science at the age of 4 and was regarded as the “Princess of Parallelograms”. Lovelace’s ideas about computing were so far ahead of her time that it took nearly a century after her death in 1852 for technology to catch up. A true visionary, she is often known as the first computer programmer.

So what are conditions and operators?

Operators 2.png

In C language you need operators to carry out arithmetic tasks.

Operators are symbols that perform basic tasks on variables and values in a program.

Operators in programming allow us to manipulate data
and outcomes.

By using Arithmetic and Assignment operators we can manipulate this data.

Operators 8.png

Operators are just symbols that tell the compiler what kind of mathematical or logical manipulation should be performed on our data.

In this tutorial, we will talk about three operators used in C programming: ArithmeticAssignment, and Relational operators.

Arithmetic Operators:

Arithmetic operators perform arithmetic operations on variables and their values. Let’s go through all the arithmetic operators.

Addition operator (+) is used to perform the addition operation on variables.

Let’s try an example to make sure we understand this operator.

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

The code above is completely editable, so give it a shot by changing the values in the editor and hit run to see your changed output.


Now it’s time to break down the code and make sure we understand all of it.

#include 
int main(void){ ~your code goes here~ }

We covered these lines in the Hello World tutorial.

int first_number = 2;
int second_number = 9;

As we explained in the ‘Variables’ tutorial, this part is called declaring and assigning our variables. We can do the declaration and assignment of variable in a single step as it is done above: int first_number = 2. let’s break this down more to make it clear. By writing int first_number; we declared a variable. You can simply change the above name to your desired name.

Note: Check the valid variable names list in the ‘Variables’ tutorial to make sure your desired name is considered a valid name.

Then, we assigned a value to our variable, = 2. You can change this number to any other number you want.

 

// printing the sum of first_number and second_number
printf("first_number + second_number = %d\n",
(first_number + second_number));

Comments ( // ) are used here to explain the code we wrote. whatever is written after //, would not be displayed in the console.

As we talked about in the ‘Hello World’ tutorial, printf function gives your computer the ability to output whatever text you desire to put into its parenthesis. In this case, we wanted to show arithmetic operations on our variables and values. Whatever is written in quotations “ “ will be printed in our console, and we use format specifier %d to specify which numbers we wish to appear for our result. Here, we’ve already assigned our variables and our values earlier.

We can use the same process for subtraction (-), multiplication (*), and division (/) operators. Notice, you can do multiple arithmetic operations on your assigned variables. For example, you can do (first_number + second_number) * (first_number).

We also have another operator called Modulo operator, also known as remainder operator, shown by (%). The modulo operation is used to determine the remainder of a division operation.

Now, let’s do a quick exercise on your own before we resume our discussion below.

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

As you can see in the code above, we used different arithmetic operations on our assigned variables; however, there is a problem here. Can you guess what the problem is?

For a / b, we got 3 instead of 3.5. The reason is we used an integer variable here. If you remember from the ‘variables’ tutorial, we have different types of variables and if we want to perform operations on numbers with decimals (floating point numbers), we need to use float variables.

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

As you can see in the code above, by using the float variable we got the true result of a / b. Notice, instead of %d we used %f specifier which is used for floating-point numbers.

Note: Modulo (%) operator cannot be used with floatingpoint numbers in C and C++.

Apart from arithmetic operators which are mentioned above, we also have Increment and decrement operators, denoted by (++) and (). These operators are used to increase or decrease the value of a variable by 1. let’s take ‘a=7’ as an example. a++ is the same as a + 1 which equals 8.

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

As you can see in the code above, we used an increment operator to add 1 value to our assigned variable, a. You might be wondering why we used the ++ before a?

++a gives the value after it is incremented; however, a++ gives the value before it is incremented. Try using a++ to see what you get for your output.

Decrement operators are done similarly. The only difference is, () will be used instead. Try using both operators and different values in the code above.


Assignment operators:

In C language, assignment operators are used to assigning values to variables. The assignment operator is denoted by the ( = ).

The list below includes some of the assignment operators.

For example, a += 2 means first add 2 to ‘a’ and then assign ‘a’ as the result of the addition.

For example, a += 2 means first add 2 to ‘a’ and then assign ‘a’ as the result of the addition.

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

You can perform different assignment operations mentioned in the list above in the compiler.

Before we continue our discussion, let’s do a quick exercise.

#include 
int main(void) { 
int a = 7
a *= 2;
printf("a = %d\n",a);
return 0;
}

 

Sign up for free and test your knowledge!

Relational Operators:

Previously, we talked about arithmetic and assignment operators. Now, we will talk about another type of operators. Relational operators are used to compare the relationship between two operands.

Note: An operand is a value or variable on which an operation is being carried on.

All relational operators will return either True (1) or False (0). In the table below, there is a list of relational operators.

arelationa.png

Now, let’s try one of these operators in a code.

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

You can easily change the operators above; for example, try != operator on your own.

As you can see in the code above, we declared variables and assigned values to them. We talked about this topic in the ‘variables’ tutorial.

if (a == b)
{
   printf("true");
}
else 
{
   printf("false");
   }

You might be wondering what these if/else statements mentioned above are. The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed. In this case, we said if a==b ( means 2 equals to 5 ), print ‘true’; else, print ‘false’.

Let’s do a quick exercise.

int main(void) {
int a = 100; 
int b = 200;

if (a <= b)  
{
  printf("True");
}
else 
{
  printf("false");
}
Sign up for free and test your knowledge!

Next steps.

Now that you have a firm grasp on how arithmetic and assignment operators work, you can start doing some more complex calculations. Try the following exercises to test your learning.

  1. Take input from the user for the length and width of a triangle and find the hypotenuse.

  2. Calculate the tax of an item, return the subtotal and total for the purchase.

  3. Using the formula y=mx+b, take 3 inputs for y, x and b to find the slope of the line (m)

Related Posts