Presentation is loading. Please wait.

Presentation is loading. Please wait.

Expressions, Conditionals and Looping CS 3260 Version 1.0 CS 3260 Version 1.0.

Similar presentations


Presentation on theme: "Expressions, Conditionals and Looping CS 3260 Version 1.0 CS 3260 Version 1.0."— Presentation transcript:

1 Expressions, Conditionals and Looping CS 3260 Version 1.0 CS 3260 Version 1.0

2 Overview Statements Expressions Operators Selection (Conditionals) Iteration (Loop) Statements Expressions Operators Selection (Conditionals) Iteration (Loop)

3 C# Statement Types CategoryC# keywords Selection statementsif, else, switch, case Iteration statementsdo, for, foreach, in, while Jump statementsbreak, continue, default, goto, return, yield Exception handling statementsthrow, try-catch, try-finally, try-catch-finally checked and uncheckedchecked, unchecked fixed statementfixed lock statementlock

4 Statements Declaration –Variable –Method Blocks { … } Expression Selection (Conditional) –if/if-else/switch/?: Iteration (Loop) –while/do-while/for/foreach Declaration –Variable –Method Blocks { … } Expression Selection (Conditional) –if/if-else/switch/?: Iteration (Loop) –while/do-while/for/foreach

5 Statements Statement Lists { … } ; - Empty : ; Declaration - = ; Expression Selection (Conditional) Iteration (Loop) Statement Lists { … } ; - Empty : ; Declaration - = ; Expression Selection (Conditional) Iteration (Loop)

6 Jump Statements goto - jump to label break – switch/loops continue - loops return – no-value/value throw – Exception object yield – continue/break goto - jump to label break – switch/loops continue - loops return – no-value/value throw – Exception object yield – continue/break

7 Exception Handling Statements checked/unchecked Block try-catch-finally checked/unchecked try-catch-finally checked/unchecked

8 try-catch-finally try: try-block catch(...) { //catch-block-1 }... catch(...) { //catch-block-n } try: try-block finally: finally-block try: try-block catch(...) { //catch-block-1 }... catch(...) { //catch-block-n } try: try-block finally: finally-block

9 using Statements using using ( resource-acquisition ) embedded-statement using using ( resource-acquisition ) embedded-statement

10 Other Statements lock yield fixed lock yield fixed

11 lock Statement lock ( expr ) embedded-statement

12 yield Statement yield return expr ; yield break; yield return expr ; yield break;

13 StatementExample Local variable declaration static void () { int a; int b = 2, c = 3; a = 1; Console.WriteLine(a + b + c); } Local constant declaration static void () { const float pi = 3.1415927f; const int r = 25; Console.WriteLine(pi * r * r); } Expression statement static void () { int i; i = 123; // Expression statement Console.WriteLine(i);// Expression statement i++;// Expression statement Console.WriteLine(i);// Expression statement } if statement static void (string[] args) { if (args.Length == 0) { Console.WriteLine("No arguments"); } else { Console.WriteLine("One or more arguments"); } }

14 switch statement static void (string[] args) { int n = args.Length; switch (n) { case 0: Console.WriteLine("No arguments"); break; case 1: Console.WriteLine("One argument"); break; default: Console.WriteLine("{0} arguments", n); break; } } while statement static void (string[] args) { int i = 0; while (i < args.Length) { Console.WriteLine(args[i]); i++; } } do statement static void () { string s; do { s = Console.ReadLine(); if (s != null) Console.WriteLine(s); } while (s != null); } for statement static void (string[] args) { for (int i = 0; i < args.Length; i++) { Console.WriteLine(args[i]); } } foreach statement static void (string[] args) { foreach (string s in args) { Console.WriteLine(s); } }

15 break statement static void () { while (true) { string s = Console.ReadLine(); if (s == null) break; Console.WriteLine(s); } } continue statement static void (string[] args) { for (int i = 0; i < args.Length; i++) { if (args[i].StartsWith("/")) continue; Console.WriteLine(args[i]); } }

16 goto statement static void (string[] args) { int i = 0; goto check; loop: Console.WriteLine(args[i++]); check: if (i < args.Length) goto loop; } return statement static int Add(int a, int b) { return a + b; } static void () { Console.WriteLine(Add(1, 2)); return; } yield statement static IEnumerable Range(int from, int to) { for (int i = from; i < to; i++) { yield return i; } yield break; } static void Main() { foreach (int x in Range(-10,10)) { Console.WriteLine(x); } }

17 throw and try statements static double Divide(double x, double y) { if (y == 0) throw new DivideByZeroException(); return x / y; } static void (string[] args) { try { if (args.Length != 2) { throw new Exception("Two numbers required"); } double x = double.Parse(args[0]); double y = double.Parse(args[1]); Console.WriteLine(Divide(x, y)); } catch (Exception e) { Console.WriteLine(e.Message); } finally { Console.WriteLine(“Good bye!”); } }

18 checked and unchecked statements static void () { int i = int.MaxValue; checked { Console.WriteLine(i + 1);// Exception } unchecked { Console.WriteLine(i + 1);// Overflow } }

19 lock statement class Account { decimal balance; public void Withdraw(decimal amount) { lock (this) { if (amount > balance) { throw new Exception("Insufficient funds"); } balance -= amount; } } } using statement static void () { using (TextWriter w = File.CreateText("test.txt")) { w.WriteLine("Line one"); w.WriteLine("Line two"); w.WriteLine("Line three"); } }

20 Expressions Primary void Arithmetic Assignment Primary void Arithmetic Assignment

21 Operators Precedence Associativy Operators Precedence Associativy

22 C# operators (categories in order of precedence) CategoryOperator symbolOperator nameExampleUser-overloadable Primary( )Groupingwhile(x)No.Member accessx.yNo ->Pointer to struct (unsafe)x->yNo ( )Function callx( )No []Array/indexa[x]Via indexer ++Post-incrementx++Yes --Post-decrementx--Yes newCreate instancenew Foo( )No stackallocUnsafe stack allocationstackalloc(10)No typeofGet type from identifiertypeof(int)No checkedIntegral overflow check onchecked(x)No uncheckedIntegral overflow check offunchecked(x)No UnarysizeofGet size of structsizeof(int)No +Positive value of+xYes -Negative value of-xYes !Not!xYes ~Bitwise complement~xYes ++Pre-increment++xYes --Post-decrement--xYes ( )Cast(int)xNo *Value at address (unsafe)*xNo &Address of value (unsafe)&xNo Multiplicative*Multiplyx * yYes /Dividex / yYes %Remainderx % yYes Additive+Addx + yYes -Subtractx - yYes

23 Shift<<Shift leftx >> 1Yes >>Shift rightx << 1Yes Relational<Less thanx < yYes >Greater thanx > yYes <=Less than or equal tox <= yYes >=Greater than or equal tox >= yYes isType is or is subclass ofx is yNo asType conversionx as yNo Equality==Equalsx == yYes !=Not equalsx != yYes Logical And&Andx & yYes Logical Xor^Exclusive Orx ^ yYes Logical Or|Orx | yYes Conditional And&&Conditional Andx && yVia & Conditional Or||Conditional Orx || yVia |

24 Null Coalescing ??Null Coalescingx ?? yNo Conditional?:ConditionalisTrue ? thenThisValue : elseThisValueNo Assignment=Assignx = yNo *=Multiply self byx *= 2Via * /=Divide self byx /= 2Via / +=Add to selfx += 2Via + -=Subtract from selfx -= 2Via - <<=Shift self left byx <<= 2Via << >>=Shift self right byx >>= 2Via >> &=And self byx &= 2Via & ^=Exclusive-Or self byx ^= 2Via ^ |=Or self byx |= 2Via | Lambda=>Lambdax => x + 1No

25 Suffixes real-type-suffix: one of F f D d M m

26 Selection - Conditional if ( ) ; if( ) else ; switch(<integer expression) { } = ( : if ( ) ; if( ) else ; switch(<integer expression) { } = ( :

27 if - Selection if ( expr ) then-stmt else else-stmt

28 Iteration - Loop while( ) ; do while(<bool expression); for( ; ; ;) ; foreach( in ) ; while( ) ; do while(<bool expression); for( ; ; ;) ; foreach( in ) ;

29 while - Loop while-statement: while ( boolean-expression ) embedded-statement

30 do loop do-body while ( expr ) ;

31 for - Loop for ( for-initializer ; for-condition ; for-iterator ) embedded-statement

32 foreach Loop foreach( in ) foreach( in )

33 throw - return throw expr ; return expr ; or return ; throw expr ; return expr ; or return ;

34 Executable Statements Methods Properties Events Indexers User-defined operators Instance constructors Static constructors Destructors Methods Properties Events Indexers User-defined operators Instance constructors Static constructors Destructors


Download ppt "Expressions, Conditionals and Looping CS 3260 Version 1.0 CS 3260 Version 1.0."

Similar presentations


Ads by Google