+ All Categories
Home > Documents > ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter...

ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter...

Date post: 21-May-2018
Category:
Upload: phamduong
View: 213 times
Download: 1 times
Share this document with a friend
11
14/5/2018 IT Project Handout Hamad Matar Programming Errors & Control Structures Programming Errors The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program. The semantics of a program statement define what a statement means (its purpose or role in a program). A program that is syntactically correct is not necessarily logically (semantically) correct. A program will always do what we tell it to do, not what we meant to tell it to do!!! 1) Syntax Errors: Definition: Errors that result from any violation of programming rules and poor understanding of programming language. 1 Syntax errors : could be grammatical mistakes in a program Some common errors : Typographical errors (in names, etc.) Misplac ed end- of- comment Omitting or misplacing braces, parentheses Misplac ed keyword s
Transcript
Page 1: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

Programming Errors & Control Structures

Programming Errors

The syntax rules of a language define how we can put together symbols,

reserved words, and identifiers to make a valid program.

• The semantics of a program statement define what a statement means (its

purpose or role in a program).

• A program that is syntactically correct is not necessarily logically

(semantically) correct.

• A program will always do what we tell it to do, not what we meant to tell it

to do!!!

1) Syntax Errors:

Definition: Errors that result from any violation of programming rules and poor understanding of programming language.

1

Syntax errors: could be grammatical mistakes in a programSome common errors :

Typographical errors (in names, etc.)

Misplaced end-of-

comment

Omitting or misplacing braces, parentheses

Misplaced keywords

Page 2: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

The compiler detects syntax errors. A single syntax error can result in a long list of errors. Correction of one or

two errors in the program may remove the entire list of errors. An application with syntax errors will not run because it will not compile.

Examples of Syntax Errors:

2) Logical errors Or Semantic errors:

Definition: Errors that are related to the logical aspect of the program.

These errors occur due to incorrect translation of algorithm into the program, poor understanding of the problem, and a lack of clarity of hierarchy of operators.

Accurate and careful commenting, proper indentation, and descriptive identifiers (variable names) can help in finding and preventing logic errors.

Logical errors are not detected by the compiler and can cause incorrect results.

Logical errors in Java programming can be extremely difficult to find because they don’t reflect any sort of coding problem or an error in the use of Java language.

Example of Logical Errors:

2

The programmer forgot a semicolon.

The programmer forgot to capitalize the letter “S” in the word “System”.

Page 3: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

3) Run-Time Errors:

Definition: Errors that are generated when the program is in a running state.

These errors will cause your program to behave unexpectedly or may even kill your program. They are often referred to as Exceptions.

These are not detected by the compiler during the compilation process.

Example of Run-Time Errors:

4) Latent Errors:

Definition: “Hidden” errors that only occur when a particular set of data is used.

3

Division by zero

Page 4: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

Such errors can only be detected by using all possible combinations of data.

E xample of Latent Errors:

** Brief details about errors:

4

An error will only occur when c and d are equal because that will make the remainder equals to zero (division by zero error).

A p

rogr

am c

an h

ave

thre

e ty

pes

of e

rror

s

The compiler will find syntax errors and other basic problems (compile-time errors)

A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors)

A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors)

Page 5: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

Control Structures

• A conditional statement lets us choose which statement will be executed next.

• Therefore they are sometimes called selection statements• Conditional statements give us the power to make basic decisions• Examples of The Java conditional statements are the:

if statement Nested if statement

1) If statement:

The if statement is a conditional control structure, also called a decision structure which executes a set of statements when a condition is true.

The if statement evaluates the test expression inside the parenthesis.

If the test expression is evaluated to true (nonzero), statements inside the body of the if   statement are executed.

If the test expression is evaluated to false (0), statements inside the body of the if statement are not executed.

5

Page 6: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

If statements take the following form:if (<condition>) {

<statements>

}

Example of if statement:if(num< 100 &&num>= 1) {

System.out.println("Its a two digit number");

2) Nested If statement:

The if – else statement can contain another if-else or if statement.

The nested if...else statement allows you to check for multiple test

expressions and execute different codes for more than two conditions.

Example of Nested if...else statement:

6

Page 7: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

Exercises:

MCQ) Answer the following questions by underlining the correct answer:

Q1) Omitting a semicolon at the end of a statement is called a

A. Run time errorB. Execution time errorC. Syntax errorD. Logical error

Q2) ----------------- are the ‘hidden’ errors that occur only when a particular set of data is used.

A. Run time errorsB. Latent errorsC. Syntax errorsD. Logical errors

Q3) Runtime errors are the errors that are generated when the program is in running state.

A. True

B. False

Q4) The condition of an if statement must be a Boolean expression.

A. True

B. False

7

Page 8: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

Q5) What is wrong with this program?

A. There has to be a semicolon placed in the first line after 14.B. The Boolean type of length has to be changed to string.C. A quotation has to be placed in the fifth line.D. Nothing is wrong in the program.

Q6) If statements take the following form:

A. if (<condition>) {

<statements>

B. if (<statements>) {

<condition>

C. if (<condition>) {

<increment>

D. None of the above

Q7) If the test expression is evaluated to false (0), the statement inside the body of the if statement is executed.

A. True

B. False

8

Page 9: ohmi357265060.files.wordpress.com€¦  · Web viewThe programmer forgot to capitalize the letter “S” in the word ... occur when a particular set of data is ... a conditional

14/5/2018 IT Project Handout Hamad Matar

Critical Thinking:

Q8) Explain the difference between the if statement and the nested if-else statement?

Q9) If you are a programmer, how would you avoid making any type of error in your program and will that consume a lot of time from you or not? Explain your answer.

Q10) Create a Stages application that prompts the user for an age. For an age over 18, adult is displayed. For an age less than or equal to 18, teenager is displayed, when the age is less than or equal to 5kid is displayed, and when the age is equal to 14, child is displayed.

9


Recommended