350 likes | 473 Views
Expressions, Conditionals and Looping. CS 3260 Version 1.0. Overview. Statements Expressions Operators Selection (Conditionals) Iteration (Loop). C# Statement Types. Statements. Declaration Variable Method Blocks { … } Expression Selection (Conditional) if/if-else/switch/?:
E N D
Expressions, Conditionals and Looping CS 3260 Version 1.0
Overview • Statements • Expressions • Operators • Selection (Conditionals) • Iteration (Loop)
Statements • Declaration • Variable • Method • Blocks { … } • Expression • Selection (Conditional) • if/if-else/switch/?: • Iteration (Loop) • while/do-while/for/foreach
Statements • Statement Lists { … } • ; - Empty • <label>: <statement>; • Declaration - <type> <ident> = <init>; • Expression • Selection (Conditional) • Iteration (Loop)
Jump Statements • goto <label> - jump to label • break – switch/loops • continue - loops • return – no-value/value • throw – Exception object • yield – continue/break
Exception Handling Statementschecked/unchecked Block • try-catch-finally • checked/unchecked
try-catch-finally • try: try-blockcatch(...) { //catch-block-1 }...catch(...) { //catch-block-n } • try: try-block finally: finally-block
using Statements • using <namespace> • using ( resource-acquisition ) embedded-statement
Other Statements • lock • yield • fixed
lock Statement • lock ( expr ) embedded-statement
yield Statement • yield return expr ; • yield break;
Expressions • Primary • void • Arithmetic • Assignment
Operators • Operators • Precedence • Associativy
Suffixes real-type-suffix: one ofF f D d M m
Selection - Conditional • if (<boolean expression>) <statement>; • if(<boolean expression>) <statement> else <statement>; • switch(<integer expression) { <statements> } • <datatype> <ident> = (<expression)?<true_value>:<false_value>
if - Selection • if ( expr ) then-stmt else else-stmt
Iteration - Loop • while(<bool expression>) <statement>; • do <statement> while(<bool expression); • for(<init statement>; <boolexpr>; <change statement>;) <statement>; • foreach(<type> <ident> <type> in <ident>) <statement>;
while - Loop • while-statement:while(boolean-expression ) embedded-statement
do loop • do-body while ( expr ) ;
for - Loop • for ( for-initializer ; for-condition ; for-iterator ) embedded-statement
foreach Loop • foreach(<datatype> <ident> in <enumerable_collection>) • <statement(s)>
throw - return • throw expr ; • return expr ; or return ;
Executable Statements • Methods • Properties • Events • Indexers • User-defined operators • Instance constructors • Static constructors • Destructors