LOOPING AND DECISION STRUCTURES Computer Programming II Summer

  • Slides: 18
Download presentation
LOOPING AND DECISION STRUCTURES Computer Programming II Summer 2015

LOOPING AND DECISION STRUCTURES Computer Programming II Summer 2015

Essential Standard ■ 1. 02 - Understand looping and branching mechanisms (4%) ■ This

Essential Standard ■ 1. 02 - Understand looping and branching mechanisms (4%) ■ This presentation is the second part of Power. Point that explains differences between C# and Visual Basic.

IF Statements ■ In C# we do not use THEN keyword as in VB.

IF Statements ■ In C# we do not use THEN keyword as in VB. The comparison is enclosed in ( ) after the if keyword. if (a < b) { Statements } ■ All statements after the if (to be run if the if is true) that would be executed are enclosed in braces ( { } ) unless there is only one line of code to be run. Braces can be omitted in this case.

IF. . Then. . Else Example if(a > b) { b=c; d= 7; }

IF. . Then. . Else Example if(a > b) { b=c; d= 7; } else { b=5; d=4; }

Else If ■ C# handles multiple else if statements a little differently as well.

Else If ■ C# handles multiple else if statements a little differently as well. There is a space between else and if unlike in VB. As in VB there can be multiple else ifs and a else (default) if all statements are false. if (a == b) c = 12; else if (a>b) c=14; else if (a < b) c = 16; else c = 10;

If Statements in Assignment Statements ■ Like VB, C# supports an if statement in

If Statements in Assignment Statements ■ Like VB, C# supports an if statement in an assignment statement. However the format is completely different: string str. Name = (a < b) ? "less than 10" : "greater than 10"; ■ ? is called the ternary operator. Example: int input = Convert. To. Int 32(Console. Read. Line()); string classify; // if-else construction. if (input > 0) classify = "positive"; else classify = "negative"; // ? : conditional operator. classify = (input > 0) ? "positive" : "negative";

And/Or & Short Circuiting ■ In VB we use the And/Or keywords in compound

And/Or & Short Circuiting ■ In VB we use the And/Or keywords in compound if statements. ■ C# – And = & – Or = | ■ To short circuit the statement use && (and) or || (or). Example: Syntax expr 1 && expr 2 example expr 1 || expr 2 example Description – expr 1 && expr 2 represents a logical AND operation that employs short-circuiting behavior. That is, expr 2 is not evaluated if expr 1 is logical 0 (false). Each expression must evaluate to a scalar logical result. – expr 1 || expr 2 represents a logical OR operation that employs short-circuiting behavior. That is, expr 2 is not evaluated if expr 1 is logical 1 (true). Each expression must evaluate to a scalar logical result.

Examples (And) ■ Using and – else if (number < 15 & number >

Examples (And) ■ Using and – else if (number < 15 & number > 5) ■ Using short circuit and – else if (number < 15 && number > 5)

Examples (Or) ■ Using or – else if(number > 50 | number < 25)

Examples (Or) ■ Using or – else if(number > 50 | number < 25) ■ Using short circuit or – else if(number > 50 || number < 25)

Select Case ■ The Select Case statement in VB does not exist in C#.

Select Case ■ The Select Case statement in VB does not exist in C#. Instead C# has the switch keyword. This operates similarly to Select Case, but has some key differences. – There are not any operators other than equals. No comparisons or ranges be used. – After each case and the code for that case, the break keyword is required. – Instead of Case Else the catch-all case is called default.

switch(int. Grades) { case 10: case 9: str. Grade="A"; break; case 8: str. Grade="B";

switch(int. Grades) { case 10: case 9: str. Grade="A"; break; case 8: str. Grade="B"; break; case 7: str. Grade = "C"; break; case 6: str. Grade="D"; break; case 5: case 4: case 3: case 2: case 1: str. Grade = "F"; break; } default: str. Grade = "F"; break; } Switch • Notice instead of using the To keyword we just assign multiple cases to a given statement— separate each with a colon. • The break; as the end of each case is required in C#.

Switch 2 ■ If all cases are false and we want an action to

Switch 2 ■ If all cases are false and we want an action to occur we use the default keyword: switch (case. Switch) { case 1: Console. Write. Line("Case 1"); break; case 2: Console. Write. Line("Case 2"); break; default: Console. Write. Line("Default case"); break; }

Loops ■ VB and C# each support the following loops: – Do – While

Loops ■ VB and C# each support the following loops: – Do – While – For each ■ In C# loops are written in the same syntax as an if statement: while (i< int. Num) remember no semicolon on this line { Code to run in the loop; semicolon at the end of each code statement unless using a if statement }

Loop Example do { int. Result = int. Result + i; (or int. Result

Loop Example do { int. Result = int. Result + i; (or int. Result +=i) //posttest loop i ++; } while (i <= 50) { int. Result = int. Result + i; i++; } //pretest loop

For Statements ■ For statements are written completely differently in C#: Initializer Condition for(i=0;

For Statements ■ For statements are written completely differently in C#: Initializer Condition for(i=0; i<=50; i++) Modifier – increment or decrement { int. Result = int. Result + i; } Condition ■ VB: For i as Integer = 0 to 50 Step 2 Modifier – increment or decrement – int. Result = int. Result + i uses Step keyword- omit to add Next i positive 1. Initializer

Step Keyword ■ In VB to count by a value other than 1 we

Step Keyword ■ In VB to count by a value other than 1 we use the Step keyword. This is not supported in C#. To count by a value other than 1 we change the end of the statement. ■ To count by 2’s upward for example: for(i=0; i<=50; i+=2) ■ To decrease by 2: for(i=100; i>=50; i-=2)

For Each ■ The for each loop is handled differently in C# as well.

For Each ■ The for each loop is handled differently in C# as well. foreach(int j in int. Array) { lst. Grades. Items. Add(j); } VB: For Each j In int. Array lst. Grades. Items. Add(j) Next j //no space

Conclusion ■ In this presentation we looked at the differences between Visual Basic and

Conclusion ■ In this presentation we looked at the differences between Visual Basic and C# for loops and IF statements. ■ Next, complete the sample program(s) as assigned by your teacher.