Presentation is loading. Please wait.

Presentation is loading. Please wait.

DECISION MAKING STRUCTURES Computer Programming 2.

Similar presentations


Presentation on theme: "DECISION MAKING STRUCTURES Computer Programming 2."— 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 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 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#.


Download ppt "DECISION MAKING STRUCTURES Computer Programming 2."

Similar presentations


Ads by Google