Presentation is loading. Please wait.

Presentation is loading. Please wait.

Test Review CIS 199 Exam 2 by.

Similar presentations


Presentation on theme: "Test Review CIS 199 Exam 2 by."— Presentation transcript:

1 Test Review CIS 199 Exam 2 by

2 Presentation Outline Looping: while, do-while, for statements Arrays
Break vs continue statements Arrays Methods Pre-conditions vs post-conditions Void vs value returning Parameters vs arguments Pass by value vs pass by reference Sequential Search Algorithm

3 Looping These are used to perform repetitive tasks
There are 3 primary types: While statement Do-while statement For statement These statements are made up of 4 parts: The control variable e.g int i The initial value e.g i = 0 The increment (or decrement) by which the variable is modified each time through the loop e.g. i = i + 1, i++, i+=2, ++i The loop continuation condition e.g. i < 10

4 While statement: will go through the loop only if the continuation condition is true
int i = 0; //variable and initialization while(i < 10) //continuation condition { Console.Write($“{i} ”); i++; //variable increment } Result:

5 Do-While: will go through the loop at least once even if the continuation condition is false
int i = 0; //variable and initialization do { Console.Write($”{i} ”); i++; //increment } while(i < 10); //continuation condition Result:

6 For Statement: condensed version of the while statement
for(int i = 0; i < 10; i++) { Console.Write($”{i }”); } Result:

7 Questions Using the for statement, write a looping statement for counting from 0 to 100 in multiples of 5 i.e 0, 5, 10,…, 100 Using the while statement, print the following statement 1000 times “Programming is fun” Which situations might the do-while statement be useful?

8 Looping Statements: break vs continue
Break statement: When executed in a while, for or do-while statement causes immediate exit from that statement Continue statement: When executed in a while, for or do-while statement skips the remaining statements in the loop body and proceeds to the next iteration

9 Break vs Continue for(int i = 1; i < 11; i++) { if(i == 5) break; }
Console.Write($”{i} ”); Result: for(int i = 1; i < 11; i++) { if(i == 5) continue; } Console.Write($”{i} ”); Result:

10 Question What’s the difference between a finite and an infinite repetition? Finite loops have continuation conditions which eventually become false thus terminating the loop. int start = 0; int end = 10; while(start < end) { //do something start = start + 1; } Infinite loops have continuation conditions which never evaluate to false thus causing the loop to ‘run forever’. int start = 0; int end = 10; do{ //do something start = start - 1; } while(start < end); This could also be achieved using a sentinel value

11 Arrays Array an important data structure in all programming languages
An array is a group of variables (called elements) containing values that all have the same data type Declaring an array can be done as follows: dataType[] arrayName = new dataType[size]; e.g. int[] numbers = new int[10]; Numbers is an array that can hold 10 integer elements e.g. string[] names = new string[5]; Names is an array that can hold 5 string elements Initializing an array can be done as follows: int[] numbers = {1,2,3,4,5,6,7,8,9,10}; string[] names = {“Jean”, “Linda”, “Mariam”, “Rudy”, “Allen”};

12 Visualizing Arrays 1 2 3 4 Jean Linda Mariam Rudy Allen
1 2 3 4 Jean Linda Mariam Rudy Allen Fun Facts about arrays: Highest index = size – 1 They are homogenous: only accept elements of the same data type Elements can be accessed from the array using the index number Elements can be added/replaced/deleted using the index number C# provides some array properties and methods that will be useful while using arrays in your program One of the most important properties is the Length property which gives the length (or size) of an array e.g. names.Length = 5

13 Question Using array declaration, initialization and the length property, print the elements of the names array using the following format. namesElement has index indexNumber in the array e.g “Jean has index 0 in the array” *Hint, a looping statement can also be used here

14 Return Types C# is an object-oriented programming language
This means it uses classes and objects to imitate/describe real world problems A class can be described as a blueprint or a template A class is usually made up of attributes (or properties) and methods An object is an instance of a class

15 Methods Every method has a return type
Indicates what kind of value the method will return to any other method that calls it If a method does not return a value, its return type is void public dataType methodName(param1, param2,…, paramN) { //body of method }

16 Void Methods //void parameterless method public void PrintName() { Console.WriteLine(“The name is Bond. James Bond”); } //void Method with Parameters public void PrintName(string userName) Console.WriteLine($”The name is {userName}”);

17 Value Returning Methods
Values are returned from a value returning method using the return keyword //value returning methods without parameters public int addNumbers() { int x = 5; int y = 10; return x + y; } //value returning methods with parameters public int addNumbers(int x, int y)

18 Parameters vs Arguments
Parameters are variables used when defining a method When a method is called, the arguments are the values inputted into the method public int findSquare(int number) //int number is a parameter { return number * number; } int x = 5; Console.WriteLine($”The square is: {findSquare(x)}”); //x is an argument

19 Methods: Pass by Value vs Pass by Reference
Passes a copy of the value Does not pass the object itself thus the original object’s properties unaffected Pass by Reference: Passes the actual object itself Thus, the original object’s properties are altered out and ref are keywords which pass variable data to methods by reference In order to pass by reference, the keyword ref is used and it causes a pass-by-reference on a variable WHILE the keyword out is used to reference a variable that the method will update

20 Example: Pass by Value vs Pass by Reference

21 Example Method That Require a Single Argument:

22 Sequential Search Algorithm
static int seqsearch(int[] dataset, int target){   int found=0;   int position = -1;   for(int i=0; i< dataset.Length; i++)     if(target == dataset[i]){ position = i; break; }   return position; }

23 Sequential Search Algorithm
static void Main(string[] args) { string search = "jessica"; bool found = false; string[] names = { "jean", "james", "adele", "marcus", "caesar" }; for (int i = 0; i < names.Length; i++) if(search == names[i]) Console.WriteLine($"{search} found at index {i}"); found = true; break; } if (found) Console.WriteLine("Search term found"); else Console.WriteLine("Search term not found"); Console.Read();

24 Questions

25 Computer Resource Center
Ekstrom Library Hours: Mon - Thurs: 8am – 8pm Fri: 8am – 4pm Sun: 12 noon – 2pm


Download ppt "Test Review CIS 199 Exam 2 by."

Similar presentations


Ads by Google