Ake Decisions Using Conditional Tatements

You will typically use conditional statements in your code to enforce business logic. Conditional statements (selection statements in the C# specification) are for making decisions in your code.

You have two main ways to implement conditional code, the if and switch statements. You will commonly use the if statement for a single comparison that has code that needs to execute when the comparison yields true and when the comparison yields false. The switch statement works best when multiple comparisons with one value are used for controlling the execution of code.

Both the if and switch statements are controlled by Boolean expressions. Boolean expressions yield either

MAKE DECISIONS USING CONDITIONAL STATEMENTS

a true or false value. With the if statement, if the Boolean expression evaluates to true, the first embedded section of code runs. After this is done, control is transferred to the end of the if statement. If the Boolean expression evaluates to false, the control then goes to the second embedded section of code. After this second embedded section runs, control goes to the end of the if statement.

The if and the switch statements can both be used to control conditional flow. It is up to you to determine which construct will best solve your programming problem.

MAKE DECISIONS USING CONDITIONAL STATEMENTS

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.

—Q Type { }, placing the opening and closing curly braces on separate lines, to set off the body of the class.

Between the curly braces, create the Main function that reads in the arguments from the command line.

-0 Use the Console.Write function to write a message to the console for collecting a character.

—H Declare a character variable and read the user input into the variable.

Note: Only the first character is read, not the line.

Was this article helpful?

0 0

Post a comment