Lesson 3. Controlling program flow. Loops. Methods. Arrays. Programming in C# Lesson 3. Controlling program flow. Loops. Methods. Arrays.
Three types of program flow Straight line Chosen depending on a given condition Repeated according to a given condition
Making decisions
Making decisions in UML diagram
Two main constructions to control program flow IF/ELSE SWITCH
IF/ELSE construction if (condition) statement or block we do if the condition is true else statement or block we do if the condition is false
Conditions and Relational Operators == > < <= >= != ! Only two types of values: true or false
Combining Logical Operators && ||
Combining Logical Operators sample:
Combining Logical Operators inversed sample:
SWITCH construction switch (value) { } case ‘option 1’: statement or block we do if value equals ‘option 1’ break; case ‘option 2’: statement or block we do if value equals ‘option 2’ case ‘option 3’: statement or block we do if value equals ‘option 3’ default: statement or block we do in all other cases }
Loops
Loops in UML diagram
Four main types of loops DO-WHILE WHILE FOR FOREACH
DO-WHILE Loop do statement or block while (condition);
DO-WHILE Loop Sample
WHILE Loop while (condition) statement or block
WHILE Loop Sample
FOR Loop for (setup; finish test; update) things we want to do a given number of times
FOR Loop Sample
Breaking Out of Loops break continue
Methods Reusing a piece of code Break down a large task into a number of smaller ones
Simple method sample
Method Parameters
Method Parameters
Method Signature Type of Parameters Number of Parameters Return Type
Parameter Passing By Value
Parameter Passing By Reference
Passing Parameters as “out” Reference
What else should we know about method parameters? Default parameters Variable number of parameters Method overload
Arrays
Array declaration
Array in Memory
Array Elements
Array Elements Initialization
Managing Array Sizes
Two Dimensional Arrays
Two Dimensional Array Declaration
Arrays and Lookup Tables