350 likes | 486 Vues
This overview covers essential C# concepts including statements, expressions, operators, conditionals (if, switch), and looping structures (while, for, foreach). It delves into statement types, declaration, variable usage, and execution flow control such as jump statements (break, continue, return). With a focus on exception handling using try-catch-finally blocks and resource management through the using statement, this guide provides a comprehensive foundation for developing robust C# applications. Ideal for beginners and intermediate programmers looking to solidify their understanding of C# programming fundamentals.
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