Presentation is loading. Please wait.

Presentation is loading. Please wait.

Welcome to Computer Programming II! Computer Programming II Summer 2015.

Similar presentations


Presentation on theme: "Welcome to Computer Programming II! Computer Programming II Summer 2015."— Presentation transcript:

1 Welcome to Computer Programming II! Computer Programming II Summer 2015

2 Purpose Computer Programming II has been completely redesigned to better align with real world uses of the C# language outside of gaming. The course will use C#. This language is very similar and based on the C language. This PowerPoint will begin to cover differences between Visual Basic and C- based languages.

3 Essential Standard 1.00 1.01- Understand variable declaration and syntax  This PowerPoint 1.02- Loop and If Statements 1.03- Random Numbers 1.04- Software Design Life Cycle & Types of Errors

4 Moving to C# from Visual Basic This PowerPoint reviews variables, decision making statements, arrays, and methods (sub procedures). Major differences between C# and VB are also covered. C# is very different from VB in one KEY way. All statements MUST end with a semicolon (;). If you forget this, your code will not work. Exceptions: IF statements and loops The upside to this is two-fold: 1. Multiple coding statements may be on the same line 2. A long code segment can be spilt up without having to use an underscore like in VB.

5 Spacing C# Examples int intAge; string strName; Two different statements- same line Long line of code: string query = @"SELECT whatever FROM tableName WHERE column = 1"; Both are perfectly valid statements in C#.

6 Semicolon Exceptions Two exceptions to the semicolon rule: if(x==b)  no semicolon here It is a “clause” - only part of the statement { y=7;  semicolon as normal } while(x< i)  no semicolon here either – again it is a “clause” { intGradeTotal=intGrade;  semicolon as normal }

7 Declaring a Variable In VB we use the Dim keyword to declare a variable: Dim intAge as Integer C# handles this differently: int intAge; In C# use the following format: DataType variableName; All types are spelled out except integer (int), character (char), and boolean (bool)

8 C# Examples string strName; char charGrade; int intAge; double dblTaxRate;

9 Data Types Data TypeStorageValue Range Integer (int)4 Bytes-2,147,483,648 to 2,147,483,647 Double8 Bytes1.7E +/- 308 (15 digits) Decimal16 Bytes1.7E +/- 308 (15 digits) Char1 Byte–128 to 127 by default Boolean (bool)1 ByteTrue or False StringVaries0 to approximately 2 billion Unicode characters

10 Default Value It is a syntax error in C# to declare a variable then use it before giving it a value. In VB a default value of 0 for a number variable or null for a string is assumed. Example: int intAge = 0; intTotal= intAge + 5;  valid because intAge is given the value of 0 before being used below int intAge; intTotal = intAge + 5;  syntax error because no value was given before it was used

11 Static and Const In Visual Basic if we wanted a variable to hold its value we used the static keyword in place of Dim. Static works differently in C#. Local variables cannot be declared as static in C#. Const works the same as it does in VB. Using Const a variable’s value cannot be changed once assigned. Example: const double TAXRate = 0.675; (C#) Example: Const TAXRate As Double = 0.675 (VB)

12 Coding Blocks In VB we use the End keyword to end coding blocks like IF statements or loops. If a = b Then b=c End If In C#, we use braces so this code block would be written: if(a==b) { b=c; }

13 Equality Check (==) You might have noticed something on the last slide- a == in the if statement. In VB the assignment operator and equality are the same (=). This is NOT true in C#. The equals sign is solely used for assignment. To check for equality you must use ==. Using = in an if statement to check for equality is a logic error. The code will run properly, but not produce the intended result. (It will always be false.)

14 Increment Counters In VB to add one to a counter we use varName += 1, for two we use +=2 and so on. (i+=1) This shorthand works in C# as well, but C# provides an additional way to add or subtract one from a counter. i++; i--; ++ adds 1 to the value of i while -- subtracts one. Remember the semicolon is required. To count by 2, or another number, we use the same shorthand as in VB (i+=2).

15 Try/Catch Try/Catch was used for error checking (exception handling) extensively in VB and is also used in C#, but the format is different. Example: try { Code to try } catch { Code to run if try code fails }

16 try/catch/finally try { Code to try } catch { Code to run if try code fails } finally { Code to run regardless if the try was successful or not. }

17 Arrays In VB we use ( ) in array statements. In C# [ ] are used instead. Dim intArray = New Integer(4) {1, 2, 3, 4, 5} (VB) int[] intArray = new int[5] {1,2,3,4,5}; (C#) In VB we put in 1 less than the number of elements we want in the array. This is NOT the case in C#. If we want five elements we use a 5 as shown above. Arrays are still index-based starting at 0.

18 Conclusion of Part I In this PowerPoint we looked at some differences between C# and Visual Basic. In the next part we will take a look at methods in C#. In the next lesson we will continue our look at differences by looking at loops and if statements.


Download ppt "Welcome to Computer Programming II! Computer Programming II Summer 2015."

Similar presentations


Ads by Google