Page 1 of 1

Tutorial(for beginners):Operators in C

PostPosted: Wed Aug 01, 2012 5:18 am
by tzoli
Hi.I didn't see any tutorial like this but a lot of begginer has problem with these.
For the purposes of this tutorial, a, b, and c represent valid values (literals, values from variables, or return value).
Arithmetic operators:
Basic assignment: a=b (Note:In the if statements it means: If a could be equal with b)
There is the Addition and the Subtraction but they are easy so everybody should know.
Multiplication: a * b
Division: a / b(if anyone don't know the operator of the multiplication or the division)
Modulo (integer remainder): a % b (ex. 11 % 5 = 1)
Operators that we use in the if statements:
Equal to: a == b(This is the correct way(and not this: a = b) in the if statement)
Not equal to: a != b
Greater than: a > b
Less than : a < b
Greater than or equal to: a >= b
Less than and equal to: a <= b
Logical operators :
Logical negation (NOT): !a
Logical AND: a = b && b = c
Logical OR a = b || b = c
Compound assignment operators:
Addition assignment a += b means -> a = a + b
Subtraction assignment a -= b means -> a = a - b
Multiplication assignment a *= b means -> a=a*b
Division assignment a /= b means -> a= a / b
Modulo assignment a %= b means -> a=a / b

Code: Select all
[u]References:[/u]
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

Re: Tutorial(for beginners):Operators in C

PostPosted: Wed Sep 05, 2012 4:17 am
by supatails
This is really helpful and I reference it a lot, but just a quick question: is there any way to write "a through b"? For example, 2 through 16? Or do I have to write out
Code: Select all
2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15 || 16

Re: Tutorial(for beginners):Operators in C

PostPosted: Wed Sep 05, 2012 4:21 am
by skydereign
supatails wrote:This is really helpful and I reference it a lot, but just a quick question: is there any way to write "a through b"? For example, 2 through 16? Or do I have to write out
Code: Select all
2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 || 11 || 12 || 13 || 14 || 15 || 16

You would never do that. That is just saying 2 or 3 or 4 and so on. That is always true. Perhaps you meant this?
Code: Select all
(var==2 || var==3 || var==4)

In which case you can use > and < to create ranges.
Code: Select all
(var>=2 && var<=16)

Re: Tutorial(for beginners):Operators in C

PostPosted: Wed Sep 05, 2012 3:56 pm
by supatails
Oh! XD That's really obvious! Thanks.