GTAOnline popping here

HomeHomeCalendarGalleryFAQSearchUsergroupsRegisterLog in

Share | 

 Lesson 4 - Operators and Math

View previous topic View next topic Go down 

Points Points : 6144
Posts : 381
Reputation : 25
Join date : 2011-01-25
Location : Hacking Kingdom

PostSubject: Lesson 4 - Operators and Math   Tue Jan 25, 2011 12:10 pm

Lesson 4: Operators and Math


Operators! A VERY VERY..... boring subject. Although, you get to learn how to do MATH!!!!! WOOHOO! Everyone loves math! Right? Maybe not, but it is very crucial to learning programming. We will start with operators and then move on to different ways to do math in C++.

Operators in C++ are mostly common keyboard symbols. The word 'operator' means any function or keyword whose name is a symbol or series of symbols. They do many different things. We have already used a few of them.

Operator '=' ::: This is this assignment operator (this is NOT the same as the equality operator). It is used to assign a value to a variable.

Operator '==' ::: This is the equality operator (NOT the same as the assignment operator). We use this operator in statements. The statement it is in will either be equal to 'true' or 'false'.

Ex 1
int a = 1;
a == 2;
When you look at this statement, it should seem to be false. If it isn't false, then you didn't learn much from Lesson 2. :P

Operator '<=' and '>=' ::: These are very obvious. You used them in math class. These are less than or equal to and greater than or equal to.

Operator '++' and '--' ::: The '++' operator is the operator for increment upward. It will increase the value of a variable by one. The '--' operator decreases a variable by one.
int x = 6;
'x' will be equal to 7.

Operator '!=' ::: If '!' means NOT and you have an equal sign after it, then '!=' would mean NOT EQUAL TO.

Operator '<' and '>' ::: Less-than and greater-than.

Your three logical operators are '!', '&&', and '||'. The exclamation mark mean NOT. The two 'and' signs mean AND. The two line things mean OR. (The line things are NOT 'L')
NOTE: I will teach you more about these three operators in the next lesson.

Conditional Operator '?' ::: The conditional operator is like a minitature if statement. (you will learn about if statements in the next lesson) It is used like so:
#include <iostream>
using namespace std;

int main ()
  int a,b,c;

  c = (a>b) ? a : b;

  cout << c;

  return 0;
As you can see, it has a condition in front of it and then it has two values behind it separated by a colon. The first value is the value for 'true' and the second value is for 'false'. So this statement is saying: 'if (a > b) then c=a else c=b'
NOTE: Again, you will learn about if statements in the next lesson.

Comma Operator ',' ::: This operator allows you to do multiple things in one statement. Like so:
int a = 1, b = 4;
I declared two integers in one line. Here is a more advanced example:
for(int x = 0, x < 5, x++)
This is a for loop. This particular one displays text 5 times.

+ - * / %
The first one is plus. The second is minus. The third is multiply. The fourth one is divide. They do just what they mean. The last one is called the modulo. The modulo tells you the remainder after the division of two numbers.
int a = 1;
int b = 2;
int c = a + b;
cout << c;
The above cout statement will display the number 3. If you got a different answer, then you didn't see the warning at the top of the page. When doing calculations outside of a declaration:
int a = 1;
int b = 2;
int c;
c = a / b;
MODULO: Below is a program that tells the user if a year was a leap year.
#include <iostream>

using namespace std;

int main()
    int year;
    int result;

    cout << "Please enter a year number: ";
    cin >> year;

    result = year % 4;

    cout << result << endl;

    return 0;
If the output is equal to '0' then that year was a leap year. Meaning that the year number divided evenly by four. So it had NO remainder.

That is the end of Lesson 4. Sorry for the lack of goodness in it. It's just not an exciting lesson.

View the video here: [You must be registered and logged in to see this link.]
Back to top Go down
View user profile

Lesson 4 - Operators and Math

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
ChEaTdEViCe ::  :: Programming :: C++ Programinng Basics-
Forum create on Forumotion | © phpBB | Free forum support | Contact | Report an abuse |