IT111 Module 1 part 2 Handout PDF
Document Details
Uploaded by HumorousFallingAction4956
Malayan Colleges Laguna
Adomar L. Ilao
Tags
Summary
This handout covers concepts of object-oriented programming in C# along with examples of conditionals, loops, and message box application. It's targeted at undergraduate-level computer science students, and is from Malayan Colleges Laguna.
Full Transcript
IT111L Concepts of Object Oriented Programming Module 1.4-1.5 Mr. Adomar L. Ilao Making Decisions Short-Circuit Evaluation Short-circuiting logical operators && and || OR (||) expressions – if the first evaluates as true, no need to evaluate the second operand...
IT111L Concepts of Object Oriented Programming Module 1.4-1.5 Mr. Adomar L. Ilao Making Decisions Short-Circuit Evaluation Short-circuiting logical operators && and || OR (||) expressions – if the first evaluates as true, no need to evaluate the second operand AND (&&) expressions – if the first evaluates as false, no need to evaluate second operand C# also includes the & and | operators logical does not perform short-circuit evaluation Two-way Selection Statement (continued) if (expression) { statement; } else { statement; } Two-way Selection Statement (continued) if (expression) { if(condition) { statement_t;} else {statement_f;} } else { if(condition) { statement_t;} else {statement_f;}} Switch Selection Statements Multiple selection structure Also called case statement Works for tests of equality only Single variable or expression tested Must evaluate to an integral or string value Requires the break for any case No fall-through available Switch Statements General Form switch (expression) { case value1: statement(s); Selector break;... case valueN: statement(s); Value must be a break; of the same type as selector [default: statement(s); break;] } Optional Repeating Instructions Using the while Statement Simplest and most frequently used loop while (conditional expression) statement(s); Expression – sometimes called loop condition Returns a Boolean result of true or false No semicolon after the conditional expression Null body→ empty bodied loop→ infinite loop Enclose multiple statements for body in { } while Statement Pretest If the conditional expression evaluates to true, statement(s) performed If the conditional expression evaluates to false, statement(s) skipped Pretest loop Sample of a While Statement MessageBox Application Windows Applications (continued) Right-click on the Reference Folder found in Solution Explorer Add a reference to a project Windows Applications (continued) Click on System.Windows.Forms. Click on OK. Class libraries of.NET Windows MessageBox Class: Type using System.Windows.Forms; MessageBox.Show( ) Method MessageBox.Show("Do you want another number ?", "State Controlled Loop", MessageBoxButtons.YesNo, MessageBoxIcon.Question) 1st argument 2nd argument 4th argument 3rd argument State-controlled loop of random numbers MessageBox class MessageBox.Show("Do you want another number ?", "State Controlled Loop", MessageBoxButtons.YesNo, MessageBoxIcon.Question) MessageBox class (continued) MessageBox.Show("Do you want another number ?", "State Controlled Loop", MessageBoxButtons.YesNo, MessageBoxIcon.Question) For Loop Pretest form of loop (like the while) Considered specialized form of while statement Usually associated with counter-controlled types Packages initialization, test, and update all on one line General form is: for (statement; conditional expression; statement) statement; Interpreted as: for (initialize; test; update) statement; Do…While Statements Posttest General form do { statement; } while ( conditional expression); Do…while loop Sample of a Do While Statement. Unconditional Transfer of Control Break Used with switch statement Place in the body of a loop to provide immediate exit Be careful (Single Entry/Single Exit) Continue When reached, a new iteration of the nearest enclosing while, do…while, for, or foreach statement is started Foreach Statement Used to iterate or move through a collection – Array – General form foreach (type identifier in expression) statement; Expression is the collection (array) Type is the kind of values found in the array – Restriction on foreach—cannot change values Access to the elements is read-only