1 / 15

C# Lesson 3

C# Lesson 3. Control Statements - Selection . Objectives. Learn the if statements. Learn the switch statement. Learn how break is used in switch statements. Understand proper use of the goto statement . The if Statement.

kamaria
Download Presentation

C# Lesson 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Lesson 3 Control Statements - Selection CIS 330

  2. 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

  3. 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

  4. 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); } CIS 330

  5. 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 CIS 330

  6. 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

  7. 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; } CIS 330

  8. C# Branching Statements CIS 330

  9. C# Lesson 4 Control Statements - Loops CIS 330

  10. 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 CIS 330

  11. 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

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

  13. 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 CIS 330

  14. 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

  15. 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.WriteLine("{0} ", person); } CIS 330

More Related