Presentation is loading. Please wait.

Presentation is loading. Please wait.

.Net Programming with C#

Similar presentations


Presentation on theme: ".Net Programming with C#"— Presentation transcript:

1 .Net Programming with C#
Haiming Chen Department of Computer Science Ningbo University Fall Semester , Course 106F22A01

2 Outline Variables and constants (types) Operators Expression
Flow of program Sequence Selection Loop

3 Arithmetic operation Operator Description Expression + Add
For strings, join opt1 + opt2 - Subtract opt1 - opt2 * Multiple opt1 * opt2 / Divide opt1 / opt2 % Mode opt1 % opt2 ++ Add 1 opt++ or ++opt -- Minus 1 opt– or --opt ~ Reverse each bit ~opt

4 Condition operation operator Description Expression
? : If the result of expression is true, then run operand1, otherwise run operand2 <Relationship expression or logic expression> ? operand1: operand2

5 Relationship operation
Operator Description Expression > Larger than opt1 > opt2 < Less than opt1 < opt2 >= No less than opt1 >= opt2 <= No larger than opt1 <= opt2 == Equal opt1 == opt2 != No equal opt1 != opt2 Result is True or False

6 Logic operation opt, opt1, opt2 are boolean Result is True or False
Operator Description Expression && and opt1 && opt2 || or opt1 || opt2 ! not ! opt opt, opt1, opt2 are boolean Result is True or False

7 Set operation operator format expression equivalent result( X = 10) =
var = exp X=x+2 X=x+12 12 += var = exp1 + exp2 X+= 2 X=X+2 -= var = exp1 - exp2 X-= 2 X=X-2 8 *= var = exp1 * exp2 X*= 2 X=X*2 20 /= var = exp1 / exp2 X/= 2 X=X/2 5 %= var = exp1 % exp2 X%= 2 X=X%2

8 Change priority of operation
Special operation Type operation description Expression Member access . Access data member or methods of a class or an object class.member object.member Note: data type can be seen as class name Type conversion () Convert type of variable (type_name) opt 1+2*3/5 (1+2)+2*3/5 Change priority of operation

9 Example int i=21, j=-1; bool blVar = i>1 && j<0;

10 Priority Priority Operator 1 ( ) 2 ++/-- 3 * / % 4 + - 5 < <=
> >= 6 == != 7 && 8 || 9 = += *= /= %= -=

11 Example 1 X==0 y X/2 X!=0

12 condition can only be boolean
Selective Structure condition can only be boolean if … else if (<condition>) { <statement 1> } else <statement 2> if (<condition>) { <statement 1> } else if <statement 2> else <statement 3>

13 expression can only be boolean, char, int, string
Selective Structure switch…case expression can only be boolean, char, int, string switch (<expression>) { case constant1: break; case constant2: case constant3: }

14 Continue without judging the condition
Loop structure while (<condition>) { <statements> } Break Go out from the loop do { <statements> } while (<condition>) Continue Continue without judging the condition

15 Loop structure for (initialize value of variable; condition; increase or decrease variable) { //statement } foreach (type var in array or string) { //statement }

16 Quiz Read a line from the console, and count the numbers of letters, digits, respectively. 1123abcd48 Number of letters: 4 Number of digits: 6 char.IsChar(var) // char.IsDigit(var) //


Download ppt ".Net Programming with C#"

Similar presentations


Ads by Google