15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

166<br />

Manipulating Strings<br />

Most likely, you won’t have any idea, but you don’t have to because you can<br />

calculate the square root of a number just by using that language’s built-in<br />

square root math function. So if you wanted to know the square root of 34<br />

and store it in an Answer variable, you could just use the sqrt math function,<br />

such as<br />

Answer = sqrt(34)<br />

In some languages, such as BASIC, it doesn’t matter if you type a math<br />

function in either uppercase or lowercase. In other languages, such as C,<br />

commands like SQRT and sqrt are considered two completely different<br />

functions, so you must know if your language requires you to type a math<br />

function in all uppercase or all lowercase.<br />

Table 3-4 lists some common built-in math functions found in many <strong>programming</strong><br />

languages.<br />

Table 3-4<br />

Common Built-In Math Functions<br />

Math Function What It Does Example<br />

abs (x) Finds the absolute value of x abs (–45) = 45<br />

cos (x) Finds the cosine of x cos (2) = – 0.41614684<br />

exp (x) Returns a number raised to the power of x exp (3) = 20.0855369<br />

log (x) Finds the logarithm of x log (4) = 1.38629436<br />

sqrt (x) Finds the square root of x sqrt (5) = 2.23606798<br />

By using math operators and math functions, you can create complex equations,<br />

such as<br />

x = 67 * cos (5) + sqrt (7)<br />

Rather than plug fixed values into a math function, it’s more flexible just to<br />

plug in variables instead, such as<br />

Angle = 5<br />

Height = 7<br />

X = 67 * cos (Angle) + sqrt (Height)<br />

Manipulating Strings<br />

Just as math operators can manipulate numbers, so can string operators<br />

manipulate strings. The simplest and most common string operator is the<br />

concatenation operator, which smashes two strings together to make a single<br />

string.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!