BİL528 – Bilgisayar Programlama II Making Decisions, Loops, Debugging, Designing Objects Using Classes 1.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
Chapter 11 Debugging and Handling Exceptions
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
BIM313 – Advanced Programming Techniques Debugging 1.
Debugging Introduction to Computing Science and Programming I.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Chapter 6 Control Structures.
Introduction to Java Programming, 4E Y. Daniel Liang.
Finding and Debugging Errors
How to Debug VB .NET Code.
Guidelines for working with Microsoft Visual Studio.Net.
Visual Basic Debugging Tools Appendix D 6/27/20151Dr. Monther Aldwairi.
Guidelines for working with Microsoft Visual Studio 6.
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CHAPTER 6 Loop Structures.
1 Web Based Programming Section 6 James King 12 August 2003.
ASP.NET Programming with C# and SQL Server First Edition Chapter 6 Debugging and Error Handling.
CIS 270—Application Development II Chapter 13—Exception Handling.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
BIM313 – Advanced Programming Techniques Flow Control 1.
Java Programming: Guided Learning with Early Objects
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
VB.Net - Exceptions Copyright © Martin Schray
Object Oriented Software Development 8. Exceptions, testing and debugging.
Introduction to Exception Handling and Defensive Programming.
VB – Debugging Tools Appendix D. Why do we need debugging? Every program has errors, and the process of finding these errors is debugging Types of errors.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Debugging Visual Basic.NET Programs ► ► Use debugging tools ► ► Set breakpoints and correct mistakes. ► ► Use a Watch and Local window to examine variables.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
CPS120 Introduction to Computer Science Iteration (Looping)
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Error Handling Tonga Institute of Higher Education.
5.01 Understand Different Types of Programming Errors
JavaScript and Ajax (Control Structures) Week 4 Web site:
COMPUTER PROGRAMMING I SUMMER Understand Different Types of Programming Errors.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Discussion 4 eecs 183 Hannah Westra.
5.01 Understand Different Types of Programming Errors
Computer Programming I
5.01 Understand Different Types of Programming Errors
MSIS 655 Advanced Business Applications Programming
IF if (condition) { Process… }
Exception Handling Chapter 9 Edited by JJ.
Teacher name August phone: Enter text here.
Debugging and Handling Exceptions
Chapter 11: Exception Handling
2015 January February March April May June July August September
Presentation transcript:

BİL528 – Bilgisayar Programlama II 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

Decision Making 3

if Statement if (expression) ; if (expression) { ; } 4

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

if..else if (expression) ; else ; If there are more statements, use curly brackets. 6

Nested if..else Statements if (expression1) { if (expression2) { } else { } } else { } 7

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

switch Example switch (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!

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

Solution for the switch Exercise switch (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

Looping 12

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

while Loop while (expression) { } 14

do-while Loop do { } while (expression); 15

foreach Loop foreach ( in ) { } 16

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

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

Exercise Display first ten prime numbers in a listbox. 19

Debugging 20

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

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

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

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 F9 3.Run the program by pressing F5 4.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

Immediate Window You can execute some statements using the Immediate Window at debugging time. Just write the expression and press Enter. 25

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

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

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

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!

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

try Example – Solution 2 try { 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

try Example – Solution 3 int 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

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

Designing Objects Using Classes 34

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