130 likes | 225 Views
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 .
E N D
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 • 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;
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;}
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 . . . }
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; }
Compound Boolean Expressions Computer Programming 2
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.
Or Operator • Using Or if(number > 50| number < 25) • Now if any of the conditions are true, the statement evaluates to true.
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.
Short Circuiting • && (and) • if (number < 15 && number > 5) • || (or) • if(number > 50 || number < 25)
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.
Conclusion • This PowerPoint provided an overview of decision making statements in Visual Studio C#.