Basics

IF . . .

THEN . . .

A constant specified in case a label is equal to the value of the switch expression

Control is transferred to the statement list following the matched case label.

No constant matches the value of the switch expression, and a default label is present

Control is transferred to the statement list following the default label.

No constant matches the value of the switch expression, and no default label is present

Control is transferred to the end point of the switch statement.

File Edil Format Help using System; class MakeStatements static void Main(strinaD srgs) {

Console.Write("Enter ONE character to be evaluated:

char cUserlnput - (char] Console.Read(), -jit (Char.lsDigit(cUserlnputj)1

else

Console.WriteLineC'The character is a number!"),

Console.WriteLinefTTie character is not a number.");

—Q Create an if-then statement to determine whether the input was a number.

I Write the appropriate message to the console.

, Save the file as the class name.

Console.WriteLineC'The character is a number!"),

Console.WriteLinefTTie character is not a number.");

C:\WINNT\System32\cmd

Microsoft Windows 2000 [Version 5.00.2195] <C> Copyright 1985 2000 Microsoft Corp.

C:\Documents and Settings\Administrator>cd C:\CSharp

C:SCsharp>csc MakeStatements.es

Microsoft <R> Uisual Ctt Compiler Uersion 7.00.9148 [CI 615]

Copyright <C> Microsoft Corp 2000. fill rights reserve)

C:\Csharp>MakeStatements.exe

Enter ONE character to be evaluated: 1

The character is a number?

C:\Csharp>Ma!keStatements .exe

Enter ONE character to be evaluated: a

The character is not a number.

C:\WINNT\System32\cmd

Microsoft Windows 2000 [Version 5.00.2195] <C> Copyright 1985 2000 Microsoft Corp.

C:\Documents and Settings\Administrator>cd C:\CSharp

C:SCsharp>csc MakeStatements.es

Microsoft <R> Uisual Ctt Compiler Uersion 7.00.9148 [CI 615]

Copyright <C> Microsoft Corp 2000. fill rights reserve)

C:\Csharp>MakeStatements.exe

Enter ONE character to be evaluated: 1

The character is a number?

C:\Csharp>Ma!keStatements .exe

Enter ONE character to be evaluated: a

The character is not a number.

Compile and run the program.

■ The program displays the message about the character the user input.

Note: See page 35 for more information on saving a file.

You can create arrays when working with a set of variables of the same data type that are related to each other. For example, you may use an array to hold a list of states. Because the state names will all be string data types, you would define a string array of 52 members.

Arrays are a variable type, so you will need to declare and initialize them just like you need to declare and initialize a string variable type. When declaring an array, you will determine the data type needed for storing members of the array and you will determine the number of members in the array.

With arrays, the default lower bound of the array is 0. So when you access the first member of the State List array, strStateList[ ],you would reference this member with strStateList[0]. If this State List array was defined to contain 52 members, then the last member would be referenced as strStateList[51].

Arrays allow you to optimize lines of code. You can do so by iterating through all the members of the array with a standard For or For Each construct. If you structure your code this way, you do not have to add any lines of code if new members are added to the array.

WORK WITH ARRAYS

WORK WITH ARRAYS

D Open your text editor.

< Type the name of the class you want to create and press Enter.

S Between the curly braces, create the Main function.

D Open your text editor.

—0 Type using System; to import the System namespace and press Enter.

< Type the name of the class you want to create and press Enter.

S Between the curly braces, create the Main function.

—0 Declare an array variable for holding the goals and a string variable for the question to the user.

Q Store goals in an array for displaying a message to the console.

^O Store responses in another array.

Was this article helpful?

0 0

Post a comment