CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics
Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting Control structures Conditions Branching loops
First Program 3 comments A class name is an identifier Series of letters, digits and ( _ ), cannot begin with a digit, and does not contain spaces.
variables 4 Every variable has a name, a type, a size and a value. data type variable name int total = 0; // Initialization int count, temp, result; Multiple variables can be created in one declaration
5
Standard Input 6 input is received from Terminal window. Input entered while program is executing. The Console s ReadLine method waits for the user to type a string of characters at the keyboard and press the Enter key. Console.ReadLine()returns the text the user entered. Use appropriate methods to converts this sequence of characters into a certain data of type
Example 7
Arithmetic Expressions 8 An expression is a combination of operators and operands Arithmetic expressions special methods applied to numerical data objects. They compute numeric results and make use of the arithmetic operators: Addition + Subtraction - Multiplication * Division / Remainder %
9 Expressions The remainder operator is most commonly used with integer operands but can also be used with floats, doubles, and decimals.
Operator precedence 10
Assignment-related Operators 11 Increment and decrement operators: ++, -- Assignment operators: +=, -=, *=, /= these three expressions have the same effect count = count + 1; count += 1; count ++; these two expressions have the same effect count = count - 10; count -= 10;
12
Constants 13 A constant variable is an identifier that is similar to a variable except that it holds one value for its entire existence Why constants: give names to otherwise unclear literal values facilitate changes to the code prevent inadvertent errors In C#: const double PI = 3.14159265;
String 14 Represents text as a series of Unicode characters. The String class provides many methods for safely creating, manipulating, and comparing strings.
15 String concatenation
How Do Data Conversions Happen? 16 Explicitly: Casting widening / narrowing conversions Examples: double MyResult; MyResult = 12.0 / 5.0; //OK int myint = (int) MyResult; // truncation MyResult = (double)myint/3.0;
17 Control Structures
Relational operators 18
Logical Operators 19
Conditional Statements 20 A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic decisions Java's conditional statements: the if and if-else statements the conditional operator the switch statement
condition evaluated true false Statement 1 Statement 2 21 Logic of an if-else statement Several statements can be grouped together into a block statement A block is delimited by braces ( { } )
The if Statement 22 The if statement has the following syntax: if is a C# reserved word The condition must be a boolean expression. e.g., a boolean variable, a == b, a <= b. It must evaluate to either true or false. if ( condition ) statement1; else statement2; If the condition is false, this statement is executed. If the condition is true, this statement is executed.
Conditional Operator 23 Also called "ternary operator" Allows embedded conditional in expression Essentially "shorthand if-else" operator Example: if (n1 > n2) max = n1; else max = n2; Can be written: max = (n1 > n2)? n1 : n2; "?" and ":" form this "ternary" operator
switch 24 multipleselection statement a constant integral expression of type: byte, short, int or char. case labels break statement optional default case
25 loops Chapters 5 & 6
26 while loops while ( condition ) statement;
Example 27
28 do loops do { statement; } while ( condition );
Example 29
30 for loops for ( initialization ; condition ; increment ) statement;
Example 31
break and Continue 32 break; Forces loop to exit immediately. Execution continues with the first statement after the control statement. continue; Skips rest of loop body In while and do while statements, the program evaluates the loop-continuation test immediately after the continue statement executes. In a for statement, the increment expression executes, then the program evaluates the loop-continuation test.
33 That s all Chapter 3: 3.6 3.9 Chapter 5 Chapter 6
34 Case Studies
Q1: 35
Q2: 36
Q3: 37
Q4: 38