Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 3301 C# Lesson 3 Control Statements - Selection.

Similar presentations


Presentation on theme: "CIS 3301 C# Lesson 3 Control Statements - Selection."— Presentation transcript:

1 CIS 3301 C# Lesson 3 Control Statements - Selection

2 CIS 3302 Objectives Learn the if statements. Learn the switch statement. Learn how break is used in switch statements. Understand proper use of the goto statement

3 CIS 3303 The if Statement An if statement allows you to take different paths of logic, depending on a given condition Condition: An expression that evaluates to true or false Basic Syntax: if (boolean expression) { statements } When the condition evaluates to a boolean true, a block of code for that true condition will execute else block executes if condition false

4 CIS 3304 The if Statement (cont) if (myInt != 0) { Console.WriteLine("Your number {0} is not equal to zero.", myInt); } else { Console.WriteLine("Your number {0} is equal to zero.", myInt); } // Multiple Case Decision if (myInt < 0 || myInt == 0) { Console.WriteLine("Your number {0} is less than or equal to zero.", myInt); } else if (myInt > 0 && myInt <= 10) { Console.WriteLine("Your number {0} is in the range from 1 to 10.", myInt); }

5 CIS 3305 The if Statement (cont) In C#, the condition must evaluate to a boolean value of either true or false Conditional OR ( || ) will evaluate the second sub-expression only if the first sub- expression evaluates to false Conditional AND ( && ) operator will evaluate the second sub-expression only when the first sub-expression evaluates to true

6 CIS 3306 The Switch Statement The switch statement executes a set of logic depending on the value of a given parameter Types of the values a switch statement operates on can only be booleans, enums, integral types, and strings

7 CIS 3307 The Switch Statement (cont) switch (myInt) { case 1: Console.WriteLine("Your number is {0}.", myInt); break; case 2: Console.WriteLine("Your number is {0}.", myInt); break; case 3: Console.WriteLine("Your number is {0}.", myInt); break; default: Console.WriteLine("Your number {0} is not between 1 and 3.", myInt); break; }

8 CIS 3308 C# Branching Statements Branching statementDescription breakLeaves the switch block continue Leaves the switch block, skips remaining logic in enclosing loop, and goes back to loop condition to determine if loop should be executed again from the beginning. Works only if switch statement is in a loop as described in Lesson 04: Control Statements - Loops.Lesson 04: Control Statements - Loops Goto (Not Recommended) Leaves the switch block and jumps directly to a label of the form " :" return Leaves the current method. Methods are described in more detail in Lesson 05: Methods.Lesson 05: Methods throw Throws an exception, as discussed in Lesson 15: Introduction to Exception Handling.Lesson 15: Introduction to Exception Handling

9 CIS 3309 C# Lesson 4 Control Statements - Loops

10 CIS 33010 Objectives Learn the while loop. Learn the do loop. Learn the for loop. Learn the foreach loop. Complete your knowledge of the break statement. Teach you how to use the continue statement

11 CIS 33011 The while Loop While loop will check a condition and then continues to execute a block of code as long as the condition evaluates to a boolean value of true Syntax: while ( ) { }

12 CIS 33012 The while Loop (cont) int myInt = 0; while (myInt < 10) { Console.Write("{0} ", myInt); myInt++; }

13 CIS 33013 The do Loop Similar to the while loop, except that it checks its condition at the end of the loop do { // Print A Menu Console.WriteLine("My Address Book\n"); Console.WriteLine("A - Add New Address"); Console.WriteLine("D - Delete Address"); Console.WriteLine("M - Modify Address"); Console.WriteLine("V - View Addresses"); Console.WriteLine("Q - Quit\n"); … Console.Write("Press Enter key to continue..."); Console.ReadLine(); Console.WriteLine(); } while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit

14 CIS 33014 The for loop Loop includes initialization and condition modification Syntax: for ( ; ; ) { } for (int i=0; i < 20; i++) { if (i == 10) break; if (i % 2 == 0) continue; Console.Write("{0} ", i); }

15 CIS 33015 The foreach Loop Loop used to iterate through the items in a list Syntax: foreach ( in ) { } string[] names = {"Cheryl", "Joe", "Matt", "Robert"}; foreach (string person in names) { Console.WriteLine("{0} ", person); }


Download ppt "CIS 3301 C# Lesson 3 Control Statements - Selection."

Similar presentations


Ads by Google