320 likes | 339 Views
Learn to use functions and control structures to organize C# code efficiently, make decisions with if and switch statements, and work with loops. Dive into creating code blocks, defining functions, calling functions, and understanding variable scope. Discover how to work effectively with the Request object and common collections.
E N D
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures
Objectives In this chapter, you will: • Learn how to use functions to organize your C# code • Work with the Request object • Use if statements, if…else statements, and switch statements to make decisions • Use while statements, do…while statements, and for statements to execute code repeatedly ASP.NET Programming with C# and SQL Server, First Edition
Working with Functions • Most programming languages allow you to group programming statements into logical units • Decision-making and flow-control statements allow you to determine the order in which statements execute in a program • Function: a procedure, used to organize a related group of C# statements as a single unit ASP.NET Programming with C# and SQL Server, First Edition
Creating Code Declaration Blocks • Code declaration block: contains ASP.NET functions and global variables • <script>element: used to create a code declaration block • Must specify the scripting language and where to run the code • Syntax: <script language =“C#” runat=“server”> … </script> ASP.NET Programming with C# and SQL Server, First Edition
Defining Functions • Must define a function before using it • Function definition: the lines that make up a function • Syntax: returnDataType functionName(parameters) { statements; } ASP.NET Programming with C# and SQL Server, First Edition
Defining Functions (cont’d.) • Return data type: indicates the type of data that the function will provide when it completes • void keyword: specifies that a function or method does not return a value • Parameter: a variable used within a function • Placed within the parentheses of a function definition • Must declare the parameter’s data type • Multiple parameters are separated by commas • Parameters are used like local variables within the body of the function ASP.NET Programming with C# and SQL Server, First Edition
Calling Functions • A function definition does not execute the function • Call: to invoke, or execute, the function • Function call: the code that calls the function • Syntax: functionName(parameters); • Arguments: values or variables to be assigned to the function parameters • Also called actual parameters • Passing arguments: sending arguments to the parameters ASP.NET Programming with C# and SQL Server, First Edition
Returning Values • If a function returns a value, the return value can be assigned to a variable • Syntax: variable =functionName(parameters); • Return statement: a statement that returns a value to the statement that called the function • Syntax: return variable; ASP.NET Programming with C# and SQL Server, First Edition
Understanding Variable Scope • Variable scope: where the variable can be used • Determined by where the variable was declared • Scope can be global or local • Global variable: a variable declared within a code declaration block but outside a function • Available to all parts of the page • Local variable: a variable declared inside a function or code render block • Available only within the function or code render block in which it is declared ASP.NET Programming with C# and SQL Server, First Edition
Understanding Variable Scope (cont’d.) • Local variables cease to exist when the function or code render block ends • If a program contains both a global and a local variable with the same names, the local variable takes precedence when its function is called • Considered poor programming practice to use the same name for both local and global variables ASP.NET Programming with C# and SQL Server, First Edition
Working with the Request Object • Request object: a built-in core object that represents the current URL request from a client • Request object uses collections • Collection: a data structure similar to an array that stores object properties • To access an object collection: object.collection(“property”) • Form collection: contains variables representing form elements from a Web page that was submitted with a method of “post” • Example: Request.Form[“name”] ASP.NET Programming with C# and SQL Server, First Edition
Working with the Request Object (cont’d.) Table 3-1:Common collections of the Request object ASP.NET Programming with C# and SQL Server, First Edition
Working with the Request Object (cont’d.) • QueryString collection: contains variables representing form elements from a Web page that was submitted with a method of “get” • Example: Request.QueryString[“name”] • A query string on a URL will also assign the name=value pairs to the Request.QueryStringcollection • Count property: returns the number of variables in a collection • Example: Request.Form.Count ASP.NET Programming with C# and SQL Server, First Edition
Making Decisions • Decision making (or flow control): the process of determining the order in which statements execute in a program or determining which sets of statements execute • if statement: most common type of decision-making statement ASP.NET Programming with C# and SQL Server, First Edition
if Statements • if statement: used to execute specific programming code based on the result of a conditional expression • If the conditional expression is true, the statements will be executed • Syntax: if (conditional expression) statement; • Command block: a set of statements contained within a set of braces • Can be used anywhere a single statement is used ASP.NET Programming with C# and SQL Server, First Edition
if…else Statements • if…else statement: executes one set of statements if the conditional expression is true or a different set of statements if the conditional expression is false • Only one set of statements will be executed • Syntax: if (conditional expression) statement; else statement; ASP.NET Programming with C# and SQL Server, First Edition
if…else Statements (cont’d.) • With command blocks: if (conditional expression) { statements; } else { statements; } ASP.NET Programming with C# and SQL Server, First Edition
Nested if and if…else statements • Nested decision-making structure: when one decision-making structure is contained within another decision-making structure • Allows the evaluation of more than one conditional expression • Example: if (condition-1) if (condition-2) statement; ASP.NET Programming with C# and SQL Server, First Edition
switch Statement • switch statement: executes a specific set of statements depending on the value of an expression • Compares the value of an expression with the value in a case label • case label: represents a specific value and contains code that executes if the switch’s expression matches in value • Can be used instead of a set of if or if…else statements ASP.NET Programming with C# and SQL Server, First Edition
switch Statement (cont’d.) • Syntax: switch (expression) { case label: statement(s); break; case label: statement(s); break; default: statement(s); } ASP.NET Programming with C# and SQL Server, First Edition
switch Statement (cont’d.) • default label: contains statements that execute when no match is found in the case labels • Must include a break statement to stop processing after the desired statements have been executed • Without a break statement, the rest of the code in the following case labels will also be executed ASP.NET Programming with C# and SQL Server, First Edition
Repeating Code • Loop statement: a control structure that repeatedly executes a statement or series of statements while a condition holds • Three types of loop statements: • while • do…while • for ASP.NET Programming with C# and SQL Server, First Edition
while Statements • while statement: repeats a set of statements as long as a given conditional expression evaluates to true • When condition becomes false, the loop ends • Syntax: while (conditional expression) { statement(s); } • Iteration: a repetition of the looping statement ASP.NET Programming with C# and SQL Server, First Edition
while Statements (cont’d.) • Counter: a variable that increments or decrements with each iteration of a loop statement • To ensure that the while statement will eventually end, you must have code that will change the value of the conditional expression • Infinite loop: a loop statement that never ends because its conditional expression is never false • A while statement may not execute at all if the expression is initially false ASP.NET Programming with C# and SQL Server, First Edition
do…while Statements • do…while statement: executes its statements once, and then repeats the execution as long as a conditional expression evaluates to true • Syntax: do { statement(s); } while (conditional expression); • In the do…while statement, the statements execute before the conditional expression is evaluated • A do…while loop always executes at least once ASP.NET Programming with C# and SQL Server, First Edition
for Statements • for statement: used for repeating a set of statements as long as a given conditional expression evaluates to true • Utilizes a counter whose value changes with each iteration • Syntax: for (counter declaration and initialization; condition; update statement) { statement(s); } ASP.NET Programming with C# and SQL Server, First Edition
for Statements (cont’d.) • Counter variable in a for statement is updated automatically with each iteration of the loop • for statement is usually used when you know (or can programmatically determine) exactly how many iterations are required ASP.NET Programming with C# and SQL Server, First Edition
Using continue Statements to Restart Execution • continue statement: stops the current iteration of the loop, but allows the loop to continue with a new iteration • Allows you to skip some statements in the loop body in a single iteration without stopping the looping ASP.NET Programming with C# and SQL Server, First Edition
Summary • Use <script>elements to create code declaration blocks to contain ASP.NET functions and global variables • Functions organize a group of related C# statements as a single unit • Global variables are declared with a code declaration block but outside of a function; are available to all parts of the page • Local variables are declared inside of a function or code render block; are available only within the function or block in which it was declared ASP.NET Programming with C# and SQL Server, First Edition
Summary (cont’d.) • Request object represents the current URL request from the client • Collections are data structures that store object properties • Flow control is the process of determining the order in which statements execute • if statement executes specific code if a conditional expression evaluates to true • if…else statement executes the ifclause if the conditional expression is true or executes the elseclause if the expression is false ASP.NET Programming with C# and SQL Server, First Edition
Summary (cont’d.) • Decision-making structures can be nested within each other • switch statement controls program flow by executing specific statements depending on the value of an expression • Loop statement repeatedly executes a set of statements while a condition is true or until a condition becomes true • while statement repeats a set of statements as long as the condition is true ASP.NET Programming with C# and SQL Server, First Edition
Summary (cont’d.) • do…while statement repeats a set of statements as long as the condition is true • for statement repeats a set of statements as long as the condition is true • continue statement is used to terminate a single loop iteration early but continue the loop with the next iteration ASP.NET Programming with C# and SQL Server, First Edition