Objectives Learn the if statements Learn the switch
Objectives • Learn the if statements. • Learn the switch statement. • Learn how break is used in switch statements. • Understand proper use of the goto statement CIS 330 1
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 CIS 330 2
The if Statement (cont) if (my. Int != 0) { Console. Write. Line("Your number {0} is not equal to zero. ", my. Int); } else { Console. Write. Line("Your number {0} is equal to zero. ", my. Int); } // Multiple Case Decision if (my. Int < 0 || my. Int == 0) { Console. Write. Line("Your number {0} is less than or equal to zero. ", my. Int); } else if (my. Int > 0 && my. Int <= 10) { Console. Write. Line("Your number {0} is in the range from 1 to 10. ", my. Int); } CIS 330 3
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 subexpression evaluates to false • Conditional AND (&&) operator will evaluate the second sub-expression only when the first sub-expression evaluates to true CIS 330 4
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 • CIS 330 5
The Switch Statement (cont) switch (my. Int) { case 1: Console. Write. Line("Your break; case 2: Console. Write. Line("Your break; case 3: Console. Write. Line("Your break; default: Console. Write. Line("Your 1 and 3. ", my. Int); break; } CIS 330 number is {0}. ", my. Int); number {0} is not between 6
C# Branching Statements Branching statement Description break Leaves 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. Goto (Not Recommended) Leaves the switch block and jumps directly to a label of the form "<labelname>: " return Leaves the current method. Methods are described in more detail in Lesson 05: Methods. throw Throws an exception, as discussed in Lesson 15: Introduction to Exception Handling. CIS 330 7
C# Lesson 4 Control Statements - Loops CIS 330 8
Objectives • • • Learn the while loop. Learn the do loop. Learn the foreach loop. Complete your knowledge of the break statement. • Teach you how to use the continue statement CIS 330 9
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 (<boolean expression>) { <statements> } CIS 330 10
The while Loop (cont) int my. Int = 0; while (my. Int < 10) { Console. Write("{0} ", my. Int); my. Int++; } CIS 330 11
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. Write. Line("My Address Bookn"); Console. Write. Line("A Console. Write. Line("D Console. Write. Line("M Console. Write. Line("V Console. Write. Line("Q - Add New Address"); Delete Address"); Modify Address"); View Addresses"); Quitn"); … Console. Write("Press Enter key to continue. . . "); Console. Read. Line(); Console. Write. Line(); } while (my. Choice != "Q" && my. Choice != "q"); // Keep going until the user wants to quit CIS 330 12
The for loop • Loop includes initialization and condition modification • Syntax: for (<initializer list>; <boolean expression>; <iterator list>) { <statements> } for (int i=0; i < 20; i++) { if (i == 10) break; } if (i % 2 == 0) continue; Console. Write("{0} ", i); CIS 330 13
The foreach Loop • Loop used to iterate through the items in a list • Syntax: foreach (<type> <item name> in <list>) { <statements> } string[] names = {"Cheryl", "Joe", "Matt", "Robert"}; foreach (string person in names) { Console. Write. Line("{0} ", person); } CIS 330 14
- Slides: 14