Showing posts with label Foundations. Show all posts
Showing posts with label Foundations. Show all posts

Tuesday, September 29, 2015

String Functions

Strings are sequences of characters. They are useful for storing data and also for accepting user input, whether it be from a terminal command line or a web form.

I find these particular string functions indispensable in my own web programming. I'm sure there are many other frequently-used string functions.

ActionC++PHPJavaScript
Split a string into an array of stringsexplodesplit
Compare stringscomparestrcmp< or >
Find a character within a stringfindstrchrindexOf
Get length of stringsizestrlenlength
Find a string within a stringstrstrstrstrindexOf
Convert a string to hexadecimalstd::hextoString(16)
Get a substringsubstrsubstrsubstring
Convert string to floating pointatoffloatvalparseFloat
Convert string to integeratoiintvalparseInt
Upper case first charactertoupperucfirsttoUpperCase
Trim white spacetrimtrim
Replacereplacestr_replacereplace
Regular expression replacecpreg_replacereplace
Determine if string is a numberis_numeric!isNan

There are also a few useful functions that relate to single characters only, but they are usually used in the context of string processing.

ActionC++PHPJavaScript
Get the ASCII value of a characterchrfromCharCode
Determine if character is a digitisdigitctype_digitparseInt and slice
Determine if a character is upper caseisupperctype_uppercompare with toUpperCase


Sunday, September 27, 2015

Decisions Using the Switch Statement

You can also make decisions using the switch statment. It looks like this:

switch (exponent) {
    case 0 :
        // do nothing
        break;
    case 1 :
        input = input * 10;
        break;
    case 2 :
        input = input * 100;
        break;
    case 3 :
        input = input * 1000;
        break;
    default :
        // do nothing
        break;
}

The above code implements simple exponentiation for languages that do not support it natively. Note that each case is an integer. The general rule is that the different cases must be types that can be converted to integer at compile time. The computer science term for such types is "enumerable." Integers, characters, and enumerations would be valid here. (An enumeration is just a way of giving names to different integers, like constant literals.) Characters are valid enumerable types because they can be considered a restricted set of integers. For example, the letter 'A' evaluates to the integer 65. See the ASCII character set for more information.

Thursday, September 24, 2015

Decisions and Branching

We must have a way for the program to make a decision, and then execute different code based on the decision. If the number is less than 10, we do one thing, otherwise we do something else. If the object is a rectangle, we obtain the area by multiplying its width by its height; but if the object is a circle, we obtain its area by multiplying its diameter by π.

Indeed, I would venture a guess that the majority of the time, popular computer programs are making decisions. (This is less so for scientific programs that do extensive numerical computations.)

Foundational Programming Concepts

I teach three programming classes. There are several concepts that I must teach in all my classes because they are so foundational. Once you have mastered these concepts in any programming language, it is easy to transfer the knowledge to other languages.

Wednesday, September 23, 2015

Boolean Logic

Boolean logic is a way of describing relationships between ideas that can have the truth values TRUE and FALSE. We describe these relationships in a diagram called a truth table:

xyx OR yx AND y
FalseFalseFalseFalse
FalseTrueTrueFalse
TrueFalseTrueFalse
TrueTrueTrueTrue

Here is an example of boolean logic. Suppose we have a set of colored shapes. We might ask two kinds of questions. If we choose one of the colored shapes, we might ask: is it blue and a circle? If we choose a blue circle, it is blue and also a circle. If we choose a yellow circle, it is not blue and a circle, because it is yellow (not blue). If we choose a blue square, it is not blue and a circle, because it is a square (not a circle). So whatever shape we choose must satisfy both the requirements, that it be both blue and also a circle.