Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS199 Test Review 2 REACH.

Similar presentations


Presentation on theme: "CIS199 Test Review 2 REACH."— Presentation transcript:

1 CIS199 Test Review 2 REACH

2 What will be discussed Selection/Decision statements
Looping statements Top-Down vs OOP Reference data types vs value data types Coding problems Conclusion

3 Selection/Decision Statements
Types If statement If-Else statement Nested if statement If-else-if statement Switch statement

4 Switch vs If-else-If The switch statement can be used to replace the if-else-if statement if: Matching has to be done on a specific: Value Type Enumeration Other data Recall that the test condition in the switch statement cannot be a: Decimal number Floating point number Switch statement cannot be used to test for a range of values. Each value must be tested individually

5 Loops Usually consists of: Definite/Finite Loops
Initial value (i = 0) Test condition (i < FINAL_VALUE) Body (console.WriteLine(“The number is “, i)) Modification (i++) Definite/Finite Loops Test for a predefined condition Once condition no longer remains true, exit the loop Indefinite/Infinite Loops Most times happens by mistake Occurs when a stop condition is not defined thereby leading the program to run endlessly

6 Types of Looping statements
While statement: test for condition then enters the loop Do while statement: enters the loop before testing for condition. Loop will run at least once even if the condition is not true For statement: condensed version of the while statement Foreach statement: used for going through an array or list. Cannot select what items in the list/array to manipulate

7 Break vs Continue Break statement Continue statement
When a break statement is encountered within a loop, the loop is stopped and the statement right after the loop is executed next. Continue statement When a continue statement is encountered within a loop, the loop skips that step of the iteration and moves on to the next iteration

8 Top Down Design This is a method for breaking down an algorithm into methods The overall task that the program is to perform is broken down into a series of sub-tasks Each sub-task is examined to determine whether it can be broken down further into more subtasks. This is repeated until no more subtasks can be identified Once all the subtasks are identified, they are written in code It is called top-down design because the programmer begins by looking at the topmost level of tasks that must be performed and then breaks down those tasks into lower levels of subtasks

9 Example of Top Down Design

10 Methods A method can be used to break a complex program into smaller manageable pieces. A void method simply executes a group of statements and then terminates A value-returning method returns a values to the statement that called it Preconditions: Conditions that MUST be true BEFORE a method execution Postconditions: Conditions that MUST be true AFTER a method execution An Argument is any piece of data that is passed into a method when the method is called WHILE a Parameter is a variable that receives an argument that is passed into a method

11 Example Parameters are used in the method signature
Arguments are used in the method call

12 Pass by Value vs Pass by Reference
When an argument is passed by value, only a copy of the argument’s value is passed into the parameter variable. If the contents of the parameter variable are changed inside the method, it has no effect on the argument calling part of the program. It guarantees that the value of the variable will not be changed by the method its passing through When an argument is passed by reference to a method, he method can change the value of the argument in the calling part of the program using a reference parameter: ref Using an output parameter method: out

13 Ref vs Out Ref: anything done to the reference parameter is actually done to the argument it references Useful for establishing 2-way communication between methods Out: anything done to the output parameter is actually done to the argument it references. They are different from ref in the following ways: An argument does not have to be set to a value before it passed into an output parameter e.g an uninitialized variable A method with an output parameter must set the output parameter to some value before it finishes executing. E.g TryParse Method

14 Reference vs Value Data Types
Value Types: When declared, memory is set aside for the variable e.g. int, float, decimal, string Reference Types: the variable does not hold data but rather it holds a reference which links the variable to the object e.g Random r = new Random()

15 Code Fragments Write a loop that displays every fifth number from 1 through 100 (5, 10, 15, etc.). Write a method named ShowRetailPrice that accepts two parameters, a double wholesaleCost and a double markupPercent and does not return a value (void method). The method should calculate the retail price (increase the cost by the given percentage) and display it using a MessageBox. Write a method named ConvertInchesToCM that accepts a double parameter inches and returns a double value. The method should convert the specified number of inches into centimeters. Each inch is equivalent to 2.54 centimeters. Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named Sum that returns a double and accepts three double parameters a, b, and c. The method should calculate the sum of the three values and return it. Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named SumArray that returns an int and accepts one parameter, an array of integers named arr. The method should calculate the sum of all the numbers in the array arr and return it. Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named Max that returns a double and accepts two double parameters a, and b. The method should return the value of the larger parameter, either a or b. Write a C# code fragment (just the relevant statements, not an entire program) that defines a public method named Max that returns a double and accepts three double parameters a, b, and c. The method should return the value of the largest parameter, either a, b, or c. Write C# code fragments (just the relevant statements, not an entire program) to accomplish the following: a) Declare an array of ints named temps. b) Allocate memory for the array to hold 10 elements. c) Write a for loop to initialize all of the array elements to the value 72 . Write a C# code fragment that prints each element of an array of doubles named arr to the console, each on a separate line. You must use a foreach loop. Write a C# code fragment that creates an array of int values using an initializer list with the following values: 10, 14, 16, 21, 27 . Write a loop that displays the values from first to last, and then write another loop that displays them from last to first.

16 Questions? For more information or if you need any help:
REACH Computer Resource Center: Ekstrom Library, Ground Floor. Hours: Mon –Thurs: 8am – 8pm Friday: 8am – 4pm Sunday: 12 noon – 2pm Our Website with Test Review Info: You can also visit the link above to get these slides


Download ppt "CIS199 Test Review 2 REACH."

Similar presentations


Ads by Google