360 likes | 468 Views
Expressions, Conditionals and Looping. CS 3260 Version 1.0. Overview. Flow of Control Statements Expressions & Operators Selection (Conditionals) Iteration (Loop) Method Calls (Invocation). C# Statement Types. All expressions require a boolean exp ression except?. if( exp )
E N D
Expressions, Conditionals and Looping CS 3260 Version 1.0
Overview • Flow of Control • Statements • Expressions & Operators • Selection (Conditionals) • Iteration (Loop) • Method Calls (Invocation)
All expressions require a booleanexpression except? • if(exp) • if(exp)-else • (exp)?(exp):(exp) • while(exp) • do-while(exp); • switch(exp) • for(<init>; <exp>; <change>) • All of the above. • None of the above.
Statements • Declaration • Variables/Fields/Data • Methods/Functions • Blocks { … } • Expression(s) • Selection (Conditional) • if/if-else/switch/?: • Iteration (Loop) • while/do-while/for/foreach
Statements • Statement Lists { … } • ; Empty • <label>: <statement>; • Declaration - <type> <ident> = <init>; • Expression – Types? • Selection (Conditional) • Iteration (Loop)
Jump Statements • goto <label> - jump to label • break – switch/loops • continue - loops • return – no-value (void)/value • throw – Exception object • yield – return/break
Exception Handling, checked/unchecked, unsafe Blocks • try-catch-finally • checked/unchecked • unsafe
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 • Logical • Others
Operators • Operators • Unary • Binary • Ternary • Precedence • Associativity
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 / if-else / ?: Selection • if( expr ) <then-statement> • if ( expr ) <then-statement> else <else-statement> • ( expr ) ? ( expr ) : ( expr )
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 ( expr )-statement:while(boolean-expression ) embedded-statement
do loop • do <statement> while ( expr ) ;
for - Loop • for ( for-initializer ; for-condition ; for-iterator ) embedded-statement
foreach Loop • foreach(<datatype> <ident> in <enumerable_collection>) • <statement(s)>
A foreach loop may not work with a? • Single dimensional array • Multiple dimensional array • C# Collections • C# Objects • All of the above. • None of the above.
throw - return • throw expr ; • return expr ; or return ;
Executable Statements • Methods • Properties & Automatic Properties • Events • Indexers • User-defined operators • Instance constructors • Static constructors • Destructors