250 likes | 377 Views
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;
E N D
Overview • C# Data Types • Shortcut Statements • Conditional Statements; • IF • SWITCH • Loop Structure
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;
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
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
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.
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.
IF/ELSE Statement - Example if (GPA > 3.5) { ODK_Membership = “Eligible”; } else { ODK_Membership = “Not eligible”; }
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 ||
Compound Expressions – Example: && if (ClassRank >=75 && GPA > 3.0) { Scholarship = “Eligible”; } else { Scholarship = “Ineligible”; }
Compound Expressions – Example: || if (ProductStatus ==“discontinued” || QtyOrdered = 0) { OfferDiscount = “No”; } Find the error?
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.
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.
NOT Operator • The NOT operator is used to negate the result of any Boolean expression. • It is written as an exclamation point (!)
NOT Operator - Example if ( !(age>=21) ) { bouncer = “No Entry”; } else { bouncer = “Come on in!”; }
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.
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!!”
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; }
WHILE Loop • The while loop allows you to execute a body of statements continuously while a condition is true. while (conditional expression) { //statement; }
WHILE Loop - Example int monthCounter = 0; while (monthCounter < 12) { Savings = Savings * (1 + BankRate); monthCounter++; } lblTotal.Text = Convert.ToString(Savings);
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);
DO-WHILE Loop - Example int monthCounter = 0; do { Savings = Savings * (1 + BankRate); monthCounter++; } while (Savings < 1200);
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
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