Sponsored Links

 Tweet Follow @webcartoonmaker

Web Cartoon Maker: a Fun Way to Learn C++ Contents Previous Next

### Operators Operators are special symbols that are used to represent simple computations like addition and multiplication. Most of the operators in C++ do exactly what you would expect them to do, because they are common mathematical symbols. For example, the operator for adding two integers is +. The following are all legal C++ expressions whose m eaning is more or less obvious:

1+1 iHour – 1 iHour * 60 + iMinute iMinute / 60

Another less obvious but interesting operator is % . It is called modulus operator . The modulus operator works on integers (and integer expressions) and yields the remainder when the first operand is divided by the second. You can use it the same way as other operators. For example 7 % 3 will yield 1.

Expressions can contain both variables names and integer values. In each case the name of the variable is replaced with its value before the computation is performed. Addition, subtraction and multiplication all do what you expect, but you might be surprised by division. For example, the following program:

void Scene1 ()

{

int iHour, iMinute;

iHour = 11;

iMinute = 59;

Text TotalMinutes ( iHour * 60 + iMinute );

Text HourFraction ( iMinute / 60 );

TotalMinutes.SetVisible ( true );

SetTime ( 1.0 );

TotalMinutes.SetVisible ( false );

HourFraction.SetVisible ( true );

SetTime ( 2.0 );

}

will show you two numbers in your cartoon: 719 and 0 . Stop, but why zero???!!! It should be 0.983333 , should not it? The reason for the discrepancy is that C++ is performing integer division. When both operands are integers the result is also integer and it is rounded down. It is here that the modulus (remainder) operator comes in. 59 % 60 is 59. You’re basically back to first grade arithmetic (before you were introduced to the decimal points, i.e. “59 divided by 60 is 0 with a remainder of 59).

Note: Historically, integer arithmetic was used for thousands of years before the decimal point was introduced. Simple things like 3/2 = 1 ½ work fine – but for larger or much smaller numbers, things get messy. If you want to perform a floating-point division then at least one operand must be a floating-point. For example you could use 60.0 or a double variable. The following program will work just fine:

void Scene1 ()

{

int iHour, iMinute;

iHour = 11;

iMinute = 59;

Text TotalMinutes ( iHour * 60 + iMinute );

Text HourFraction ( iMinute / 60.0 );

TotalMinutes.SetVisible ( true );

SetTime ( 1.0 );

TotalMinutes.SetVisible ( false );

HourFraction.SetVisible ( true );

SetTime ( 2.0 );

}  In C++ these mathematical operators work the same way for int and double values and variables. If both operands are int then the result is also int . If at least one operand is double then the result is double as well.

Contents Previous Next

News

New Tales Animator Video by Alan Sturgess Alan Sturgess shared an excellent video he made using Tales Animator! You can still download Tales Animator here. Unfortunately it is only available for Wi

...

Simple Online Character Designer There is a prototype of simple online character designer available HERE. It is only a prototype, it does not contain many pieces yet but it can already generat

...

Book is updated Now our book "Web Cartoon Maker: A Fun Way to Learn C++" is fully in synch with WCM 1.5! It is available for download and online reading HERE.

...

Web Cartoon Maker 1.5 is here! Web Cartoon Maker 1.5 is finally here! You can download it HERE! Here is what was updated in version 1.5: Web Cartoon Maker Desktop Edition is now fully standal

...

Poll