Testing strings and numbers

From Progzoo
Jump to: navigation, search

A simple if statement tests a condition, the following statement will be run only if the condition is true.

Testing for string equality

Notice that we use the method equals - you cannot use the == operator here.

Print the name of the country if the region is "North America"

[Font] [Default] [Show] [Resize] [History] [Profile]

Testing for number equality

When testing numbers you should use the == operator.

Print the name of the countries with a population of exactly 2000000

[Font] [Default] [Show] [Resize] [History] [Profile]

Testing for pieces of a string at either end

You can check to see if one string starts with a particular value (using startsWith) ends with a value (using endsWith).

Print only those countries where the name ends with land

[Font] [Default] [Show] [Resize] [History] [Profile]

Testing for pieces of a string in the middle

You can check to see if one string contains another using the contains method.

Print only those countries where the name includes ee

[Font] [Default] [Show] [Resize] [History] [Profile]

Testing for inequality

Print only those countries where pop is less than 20000

[Font] [Default] [Show] [Resize] [History] [Profile]