4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

Introduction to Computer Programming - part 3

enter image description here



INTRODUCTION TO PROGRAMMING

I am collating my study notes as part of my education into Computer Programming. In an attempt to increase and improce my own learning, and to help others also (learn by teaching) I will be sharing my notes, as neatly presented as possible, for others to follow along with if they so wish. This is Lesson 3's notes.


EARLIER LESSONS

Lesson 1 Notes
Lesson 2 Notes


enter image description here



INTRODUCTION TO COMPUTER PROGRAMMING – STUDY NOTES

LESSON 3


OPERATORS

LINK: Operators

Operators allow you to operate on an element of the program.

  • Assignment

=
This means to apply the value of. For example x = y means to apply the value of x to y. It is not the same meaning as standard mathematics where it means equal to.

  • Arithmetic Operators

+Addition
-Subtraction
*Multiplication
/Division
%Modulo

  • Modulo = remainder of a division operation.

Example 143 / 3 = 47.66 (standard division) but if you take only the whole numbers, then the modulo of that operation is 143 / 3 = 47, WITH a remainder of 2. In other words, 47 * 3 = 141 and the difference between 143 and 141 is 2 (This is the modulo, or the remainder). There is a need for such an operator within programming languages.

  • Compound Operators

+= : x+=y is the same as saying x=x+y
-+ : x-=y is the same as saying x=x-y
*+ : x*=y is the same as saying x=x*y
/= : x/=y is the same as saying x=x/y
%= : x%=y is the same as saying x=x%y

This allows for a slightly shorter way of writing out these operators.

  • Unary Operators

++ : X++ is the same as saying x=x+1 (used a lot in loops)
-- : X-- is the same as saying x=x-1 (used a lot in loops)
! : Means NOT.
Example : x=true, then !x would then be false (ie - NOT true)

  • Prefix Operator vs Suffix Operator

LINK: Increment and Decrement Operators

When the ++ or -- is set as a prefix as opposed to a suffix like earlier described, then the value of the variable that it relates to is handled differently. The variable is increased FIRST then assigned to the new variable.

Prefix Example:
x = 3
y = ++x
In this example x is assigned (=) the value of 3, and then because of the PREFIX in the second line, x is increased by 1 to 4 (++x) BEFORE y is assigned that value. Now both x and y have the value of 4.

The suffix example would create a different answer.

Suffix Example
x = 3
y = x++
In this example x is assigned (=) the value of 3, and then because of the SUFFIX in the second line, x is increased by 1 to 4 (x++) AFTER y is assigned that value. So y is assigned the value of x which is 3, THEN x is increased by 1 to 4.

This seemingly minor but significant difference can lead to logical errors in your program if you apply the wrong use of suffix/prefix.

Suffix = value is assigned first before it is increased.
Prefix = value is increased first before it is assigned.

  • Relational/Comparison Operator

Used to compare 2 values.
Will always evaluate to a Boolean value (TRUE or FALSE).

== : equal to
!= : not equal to
> : greater than
< : less than
>= : greater than or equal to
<= : less than or equal to

- Logical Operator

Used to tell the computer to do an operation when certain conditions or mix of conditions are met. Such as condition 1 AND condition 2; condition 1 OR condition 2. Also the NOT operator.

! : NOT :
&& : AND : True only when both conditions are true.
|| : OR : True if one or the other is true.

  • Operator Precedence

There is an order to which operators are evaluated. There is an order of importance.
All the languages use the same precedence.


LINK: C++ Example
LINK: JavaScript Example

enter image description here


FURTHER DATA

cpp.sh - C++ shell website
Scratchpad - Shift + F4 on Firefox - Javascript shell
Dev C++ - A free, portable, fast and simple C/C++ IDE
Code::Blocks - A free C, C++ and Fortran IDE
Ideone - an online compiler and debugging tool which allows youto compile source code and execute it online in more than 60 programming languages.


My Posts

Introduction From a Newbie Programmer.

Introduction to Computer Programming - part 1 (Lesson 1)

Introduction to Computer Programming - part 2 (Lesson 2)


Images from unsplash.com.

I welcome new followers, and thank you for your upvotes and comments.

enter image description here


27 upvotes $0.21

Replies (3)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: