1 / 13

Decision Making Structures

Decision Making Structures. Computer Programming 2. If Statement. Syntax: if (Boolean Expression) statement ; Boolean Expression can be: Equivalent == Greater Than > Greater Than or Equal to >= Less Than < Less Than or Equal to <= Not Equivalent to !=. If - Else Statement .

hue
Download Presentation

Decision Making 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. Decision Making Structures Computer Programming 2

  2. If Statement • Syntax: if (Boolean Expression)statement; • Boolean Expression can be: • Equivalent == • Greater Than > • Greater Than or Equal to >= • Less Than < • Less Than or Equal to <= • Not Equivalent to !=

  3. If - Else Statement • Syntax – Single Statement: if (Boolean Expression)statement; else statement; • Syntax – Multiple Statement: if (Boolean Expression) {statement; statement; Must have {} if more than 1 statement } else statement;

  4. If – Else If Statement • To check more than three conditions. • Syntax – Multiple Statement: if (Boolean Expression) {statement; } else if (Boolean Expression) { statement; } else if (Boolean Expression) { statement;}

  5. Switch • Another way to make a decision is a switch statement. • This statement is used in lieu of multiple else if statements. • Syntax: switch (variable/expression) { case value: statements; break; //must have or all statements run . . . }

  6. Example switch(intGrades) { case 10: case 9: strGrade="A"; break; case 8: strGrade="B"; break; case 7: strGrade = "C"; break; case 6: strGrade="D"; break; case 5: case 4: case 3: case 2: case 1: strGrade = "F"; break; }

  7. Compound Boolean Expressions Computer Programming 2

  8. And Operator • When using And or & with IF all statements MUST be true for the IF (or else if) statement to evaluate to true. So using our C# example again: if (number < 15 &number > 5) • If the number is not less than 15 AND greater than 5 this statement is false.

  9. Or Operator • Using Or if(number > 50| number < 25) • Now if any of the conditions are true, the statement evaluates to true.

  10. Short Circuiting • C# provides a way to “short circuit” long compound IF statements. • The computer looks at the first statement and uses that information to decide if it needs to look at the rest of the statement. • && • If IsNumeric(number) && IsNumeric(num1) Then • So if number is not numeric the statement is false and the computer does not waste time looking at num1. • || • If IsNumeric(number) || IsNumeric(num1) Then • If number is numeric there is no need to check num1 because the statement is true.

  11. Short Circuiting • && (and) • if (number < 15 && number > 5) • || (or) • if(number > 50 || number < 25)

  12. The Ternary Operator • There is a shortcut for IF statements. This is called the ternary (three) operator. The ternary operator is a question mark (?). • Syntax: varName= (comparison) ? Value if true : value if false; • Example: int a = 0; int b= 10; string strName= (a < b) ? "less than 10" : "greater than 10"; //Assign a value to the string strName based on the comparison given. This is exactly how if statements are done in Excel.

  13. Conclusion • This PowerPoint provided an overview of decision making statements in Visual Studio C#.

More Related