1 / 25

Unit 2.1 Code Structures

Unit 2.1 Code Structures. Overview. C# Data Types Shortcut Statements Conditional Statements; IF SWITCH Loop Structure. C# Data Types. Shortcut Statements. Many shortcuts are used within C# Such As: ++, --, +=, -=, *=, /= Examples: x++;  x = x + 1; y--;  y = y – 1;

elliot
Download Presentation

Unit 2.1 Code Structures

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. Unit 2.1Code Structures

  2. Overview • C# Data Types • Shortcut Statements • Conditional Statements; • IF • SWITCH • Loop Structure

  3. C# Data Types

  4. Shortcut Statements • Many shortcuts are used within C# • Such As: ++, --, +=, -=, *=, /= • Examples: x++;  x = x + 1; y--;  y = y – 1; z += 5;  z = z + 5; q *= 7;  q = q * 7;

  5. Conditional Statements Two types • IF statement: • Tests a condition • If true, executes enclosed code • SWITCH statement: • tests one variable against many values • If value matches, executes code til break

  6. Expression must be in parenthesis. IF Statement if (expression) { Statement; } The IF statement tests an expression for true or false. If true, the enclosed statements will run, otherwise, they will be skipped. Indentation, although not required, is standard practice

  7. IF Statement - Example if (CreditHours >= 192) { lblGraduation.Text = “You are good to go”; } You only want to consider a student for graduation if their credits total more than 192 hours.

  8. Indentation, although not required, is standard practice if (expression) { Statement; } else { Statement; } Expression must be in parenthesis. Must put statement within brackets. Do not forget the semicolon. IF/ELSE Statement • Some decisions have two conditions to evaluate. • An ELSE clause may be added to an IF statement when an action must be performed for either outcome.

  9. IF/ELSE Statement - Example if (GPA > 3.5) { ODK_Membership = “Eligible”; } else { ODK_Membership = “Not eligible”; }

  10. Compound Expressions • Sometimes you will need to meet multiple qualifications before the outcome will equal true or false. • For example, College Scholarships are available: • if your high school class rank is higher than 75% • and if your GPA is higher than 3.0 • To solve this problem use the AND/OR operators • AND  && • OR  ||

  11. Compound Expressions – Example: && if (ClassRank >=75 && GPA > 3.0) { Scholarship = “Eligible”; } else { Scholarship = “Ineligible”; }

  12. Compound Expressions – Example: || if (ProductStatus ==“discontinued” || QtyOrdered = 0) { OfferDiscount = “No”; } Find the error?

  13. Nested IF Statements • Nested IF statements are used when you want to evaluate an expression ONLY after a previous expression has been met. • It is like a compound statement that uses AND.

  14. Nested IF Statement - Example if (ProductStatus ==“active”) { if (QtyOrdered > 100) { DiscountPercent = 10; } else { DiscountPercent = 5; } } Here, only if the product status is “active” will the following (nested) IF statement be evaluated.

  15. NOT Operator • The NOT operator is used to negate the result of any Boolean expression. • It is written as an exclamation point (!)

  16. NOT Operator - Example if ( !(age>=21) ) { bouncer = “No Entry”; } else { bouncer = “Come on in!”; }

  17. Each case must end in break. Brackets surround all cases. SWITCH Statement • The SWITCH statement tests a single variable against a series of exact matches. • The form you must use is as follows: switch (variable_being_evaluated) { case value: statement; break; case value2: statement2; break; default: statement3; break; } Switch expression must be in parenthesis. Indentation, although not required, is standard practice.

  18. SWITCH Statement – Scenarios if (CollegeFootball==“Ohio State University”) team = “Buckeyes”; else if (CollegeFootball==“University of Cincinnati”) team = “Bearcats”; else if (CollegeFootball==“Ohio University”) team = “Bobcats”; else if (CollegeFootball==“Miami University”) team = “Who cares”; else team = “Duh!!”

  19. SWITCH Statement - Example switch (CollegeFootball) { case “Ohio State University”: team = “Buckeyes”; break; case “University of Cincinnati”: team = “Bearcats”; break; case “Ohio University”: team = “Bobcats”; break; case “Miami University”: team = “Who Cares!”; break; default: team = “Unlisted”; break; }

  20. WHILE Loop • The while loop allows you to execute a body of statements continuously while a condition is true. while (conditional expression) { //statement; }

  21. WHILE Loop - Example int monthCounter = 0; while (monthCounter < 12) { Savings = Savings * (1 + BankRate); monthCounter++; } lblTotal.Text = Convert.ToString(Savings);

  22. DO-WHILE Loop • When you need a set of statements to execute at least one time and then execute in a counter, use a do-while loop. • The following form must be used. do { statement; } while (expression);

  23. DO-WHILE Loop - Example int monthCounter = 0; do { Savings = Savings * (1 + BankRate); monthCounter++; } while (Savings < 1200);

  24. FOR Loop • The FOR loop is a counted loop. • Indicate: • The starting value for the loop control variable • The test condition that controls loop entry • The expression that alters the loop control variable

  25. For Loop - Example • To accrue savings with interest over a 12 month period, a FOR loop could be used as shown: for (Int32 month = 0; month < 12; month++) { Savings = Savings * (1 + BankRate); } Declare Variable Test Condition Expression

More Related