A few reference types are already baked into the language and include String, as well as wrapper classes for all primitive types, like Integer for int and Boolean for boolean. The number of reference types is unlimited, as they are user-defined. Non-primitive types, also known as reference types, hold references to objects stored somewhere in memory. These built-in types describe variables that store single values of a predefined format and size. There are eight primitive types in Java: byte, short, int, long, float, double, boolean and char. Data types, or types for short, are divided into two categories: primitive and non-primitive. In Java, every variable has a data type and stores a value of that type. Variables are memory containers used to store information. Achieving this behavior with CASE would be difficult.Introduction to Data Types & Type Conversion If we pass the value 3 to the DAYS_OF_XMAS procedure, we get the following output. PRO DAYS_OF_XMAS, day IF ( N_ELEMENTS(day) EQ 0) THEN DAY = 12 IF ((day LT 1) OR (day GT 12)) THEN day = 12 day_name = PRINT, 'On The ', day_name, $ ' Day Of Christmas My True Love Gave To Me:' SWITCH day of 12: PRINT, ' Twelve Drummers Drumming' 11: PRINT, ' Eleven Pipers Piping' 10: PRINT, ' Ten Lords A-Leaping' 9: PRINT, ' Nine Ladies Dancing' 8: PRINT, ' Eight Maids A-Milking' 7: PRINT, ' Seven Swans A-Swimming' 6: PRINT, ' Six Geese A-Laying' 5: PRINT, ' Five Gold Rings' 4: PRINT, ' Four Calling Birds' 3: PRINT, ' Three French Hens' 2: BEGIN PRINT, ' Two Turtledoves' PRINT, ' And a Partridge in a Pear Tree!' BREAK END 1: PRINT, ' A Partridge in a Pear Tree!' ENDSWITCH END The first day of Christmas requires special handling, so we use a BREAK statement at the end of the statement for case 2 to prevent execution of the statement associated with case 1. Therefore, the fall-through behavior of SWITCH fits this problem nicely. If we enter 3, for example, we want to print the presents for days 3, 2, and 1. It starts on the specified day, and prints the presents for all previous days. The DAYS_OF_XMAS procedure accepts an integer argument specifying which of the 12 days of Christmas to start on. The following example illustrates an application that uses SWITCH more effectively. There may be other cases when the fall-through behavior of SWITCH suits your application. We could write this example using SWITCH: SWITCH name OF 'Larry': BEGIN PRINT, 'Stooge 1' BREAK END 'Moe': BEGIN PRINT, 'Stooge 2' BREAK END 'Curly': BEGIN PRINT, 'Stooge 3' BREAK END ELSE: PRINT, 'Not a Stooge' ENDSWITCHĬlearly, this code can be more succinctly expressed using a CASE statement. For example, our first example of the CASE statement looked like this: CASE name OF 'Larry': PRINT, 'Stooge 1' 'Moe': PRINT, 'Stooge 2' 'Curly': PRINT, 'Stooge 3' ELSE: PRINT, 'Not a Stooge' ENDCASE The decision on whether to use CASE or SWITCH comes down deciding which of these behaviors fits your code logic better. Instead, execution continues immediately following the SWITCH. Failure to match is not an error within a SWITCH statement. If there are no matches within a CASE statement and there is no ELSE clause, IDL issues an error and execution halts.(For more information on using the BREAK statement, see BREAK.) For example, we can add a BREAK statement to the SWITCH example in the above table to make the SWITCH example behave the same as the CASE example: x= 2 SWITCH x OF 1: PRINT, 'one' 2: BEGIN PRINT, 'two' BREAK END 3: PRINT, 'three' 4: PRINT, 'four' ENDSWITCH X= 2 SWITCH x OF 1: PRINT, 'one' 2: PRINT, 'two' 3: PRINT, 'three' 4: PRINT, 'four' ENDSWITCHīecause of this difference, the BREAK statement is often used within SWITCH statements, but less frequently within CASE. X= 2 CASE x OF 1: PRINT, 'one' 2: PRINT, 'two' 3: PRINT, 'three' 4: PRINT, 'four' ENDCASE The following table illustrates this difference: By contrast, execution within a SWITCH statement falls through to the next statement. The CASE and SWITCH statements are similar in function, but differ in the following ways:Įxecution exits the CASE statement at the end of the matching statement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |