bim211 – visual programming making decisions, loops, debugging, designing objects using classes 1

36
BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Upload: dorcas-miller

Post on 18-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

BIM211 – Visual Programming

Making Decisions, Loops, Debugging, Designing Objects Using Classes

1

Page 2: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Contents

• Making Decisions– if, else, switch

• Loops– for, while, do-while, foreach

• Debugging– by using Visual Studio features– try…catch…finally

• Designing Objects using Classes

2

Page 3: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Decision Making

3

Page 4: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

if Statement

if (expression) <statement to execute when expression is true>;

if (expression){ <statement 1>; <statement 2>;}

4

Page 5: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Exercise

• Write a program which checks whether an integer number written in a textbox is less than 100 or not.

5

Page 6: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

if..else

if (expression) <statement to execute when expression is true>;else <statement to execute when expression is false>;

• If there are more statements, use curly brackets.

6

Page 7: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Nested if..else Statements

if (expression1) { if (expression2) { <statements1> } else { <statements2> }} else { <statements3>}

7

Page 8: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

switchswitch (expression){ case value1: ... break; case value2: ... break; default: ... break;}

8

Page 9: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

switch Exampleswitch (strProfession){ case "teacher": MessageBox.Show("You educate our young"); break; case "programmer": MessageBox.Show("You are most likely a geek"); break; case "accountant": MessageBox.Show("You are a bean counter"); break; default: MessageBox.Show("Profession not found in switch statement"); break;}

9

In C, only integer values can be used as the expression but in

C#, strings can be used too.

Don’t forget to use breaks!

Page 10: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

switch Exercise

• Write a program which displays the names of some animals (horse, dog, bird, cat, snake, and centipede) in a combobox and displays the number of legs of the selected animal.

10

Page 11: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Solution for the switch Exerciseswitch (cboAnimals.Text){case “Bird”: MessageBox.Show(“The animal has 2 legs.”); break;case “Dog”: // Notice there is no code here to execute.case “Horse”: // Notice there is no code here to execute.case “Cat”: MessageBox.Show(“The animal has 4 legs.”); break;case “Snake”: MessageBox.Show(“The animal has no legs.”); break;case “Centipede”: MessageBox.Show(“The animal has 100 legs.”);

break;default: MessageBox.Show(“You did not select from the list!”); break;}

11

Page 12: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Looping

12

Page 13: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

for Loop

for (initializers; check_condition; modifying_expressions){ <statements>}• Example:for (i = 0; i < 10; i++) { MessageBox.Show("i = " + i.ToString());}

13

Page 14: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

while Loop

while (expression){ <statements>}

14

Page 15: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

do-while Loop

do{ <statements>} while (expression);

15

Page 16: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

foreach Loop

foreach (<type> <name> in <list>){ <statements>}

16

Page 17: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Displaying Months using for Loop

string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

for (int i = 0; i < months.Length; i++){ MessageBox.Show(months[i]);}

17

Page 18: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Displaying Months using foreach

string[] months = new string[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

foreach (string month in months){ MessageBox.Show(month);}

18

Page 19: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Exercise

• Display first ten prime numbers in a listbox.

19

Page 20: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Debugging

20

Page 21: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Debugging

• No one writes perfect code.• You’re most certainly familiar with those

problems that prevent code from executing properly; they’re called bugs.

• We can’t teach you how to debug every possible build or runtime error you might encounter.

• Debugging is a skill and an art.

21

Page 22: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

About Commenting…• Document the code’s purpose (the why, not the

how).• Clearly indicate the thinking and logic behind the

code.• Call attention to important turning points in the

code.• Reduce the need for readers to run a simulation

of the code execution in their heads.• Comment your code as you are typing it. If you

wait until the code is complete, you probably won’t go back and add comments.

22

Page 23: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Identifying the Two Basic Types of Errors

• Build Errors– Visual C# won’t compile a project that has a build

error in it.– Example: Calling a method with incorrect

parameters.

• Runtime Errors– Encountered when the project is being run.– Example: Division by zero.

23

Page 24: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Debugging Steps in Visual Studio

1. Locate the cursor to the line at which you want the program stops

2. Put a break point by pressing F93. Run the program by pressing F54. The program stops at each breakpoint and you can

see the values of the variables by moving mouse pointer on them

5. Press F10 to execute a single line, or F11 to enter into a method

6. Press F5 to continue, or Shift+F5 to stop debugging

24

Page 25: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Immediate Window

• You can execute some statements using the Immediate Window at debugging time.

• Just write the expression and press Enter.

25

Page 26: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Locals and Watch Windows

• You can see the local variables in Locals window at debugging time.

• If there are so many local variables in your code, watching a specific variable may be harder.

• In this case, you can use the Watch window.• Just write the expressions into the Watch

window!

26

Page 27: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

try..catch..finally

• While running a program through the IDE (i.e. during the development step, or Debug mode), you receive an error message if an error occurs.

• However, if your program runs in the final version (or Release mode), an error causes your program to terminate.

• For these cases, you may prefer using structured exception handling (or try..catch..finally)

27

Page 28: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Sections of the try Structure

• try: The try section is where you place code that might cause an exception.

• catch: Code within the catch section executes only when an exception occurs.

• finally: Code within the finally section occurs when the code within the try and/or catch sections completes. This section is where you place your cleanup code—code that you always want executed, regardless of whether an exception occurs.

28

Page 29: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

try Example

int number = int.Parse(tbNumber.Text);int result = 100 / number;tbResult.Text = result.ToString();

29

An error may occur while converting a

text into an integer!

An error may occur if the number is

zero!

Page 30: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

try Example – Solution 1

try{ int number = int.Parse(tbNumber.Text); int result = 100 / number; tbResult.Text = result.ToString();} catch { tbResult.Text = “An error occurred!”;}

30

Page 31: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

try Example – Solution 2try{ int number = int.Parse(tbNumber.Text); int result = 100 / number; tbResult.Text = result.ToString();} catch (ArgumentException ex1){ tbResult.Text = “An ArgumentException error occurred!”;} catch (DivideByZeroException ex2) { tbResult.Text = “The divisor can’t be zero”;} catch (Exception ex) { tbResult.Text = “An unspecified error occurred. The details is: ” +

ex.Message;}

31

Page 32: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

try Example – Solution 3int result = 0;try{ int number = int.Parse(tbNumber.Text); result = 100 / number;} catch (Exception ex) { MessageBox.Show(“An unspecified error occurred. The

details is: ” + ex.Message);} finally { tbResult.Text = result.ToString();}

32

Page 33: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

try Example on Database Operations

try{ … // Open database … // Make some operations on the database} catch { … // Error handling statements} finally { … // Close the database}

33

Page 34: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Designing Objects Using Classes

34

Page 35: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

Designing Objects Using Classes

• We are omitting this chapter since it is related to object oriented programming, which is the next semester's course.

• However I strongly recommend you to read this chapter at home.

35

Page 36: BIM211 – Visual Programming Making Decisions, Loops, Debugging, Designing Objects Using Classes 1

HW#3 and Project

• Your last homework will be a simple Paint program. The user will select the size and the color of the pen and draw some picture. The details will be given to you in next weeks.

• The project will be a database application. You will write a word memorizer program. The user will be able to add some words to the database and update and delete them. Your program will make a multiple-choice-test to the user. The details will be given in next weeks.

36