CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Advertisements

Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Comparing Data Comparing More than Numbers. Comparing Data When comparing data using boolean expressions, it's important to understand the nuances of.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3: Program Statements
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Topics Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement The for loop Nested Loops.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 More about Flow of Control. Topics Debugging Logical Operators (Chapter 5) Comparing Data (Chapter 5) The conditional operator The switch Statement.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 4 Repetition Statements (loops)
Chapter 3: Program Statements
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
EGR 2261 Unit 4 Control Structures I: Selection
Loop Structures.
Boolean Expressions and If
Boolean Expressions & the ‘if’ Statement
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Outline Altering flow of control Boolean expressions
The ‘while’ Statement September 27, 2006
Chapter 3: Program Statements
Logic of an if statement
CprE 185: Intro to Problem Solving (using C)
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Comparing Data & the ‘switch’ Statement
Outline Software Development Activities
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Conditionals and Loops
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
Boolean Expressions & the ‘if’ Statement
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

CprE 185: Intro to Problem Solving (using C) Instructor: Alexander Stoytchev http://www.cs.iastate.edu/~alex/classes/2008_Fall_185/

Debugging CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Administrative Stuff HW5 is due this Wed, Oct 15 @ 8pm Please double check your grades on Web CT WebCT is not your friend!!

Administrative Stuff Midterm 2 is coming up in two weeks  Same format as before: Lab exam during your regular lab time (Oct 28 or Oct 29) Lecture exam on Oct 29 (10-11am) Note: There will be NO night exam. The exam will be cumulative with emphasis on conditional statements (if, if-else, switch), loops (do, while, for), arrays (to be covered), and searching and sorting algorithms (to be covered).

Quick Review of the Last Lecture

Logic of a do Loop statement true condition evaluated false © 2004 Pearson Addison-Wesley. All rights reserved

Logic of a while Loop condition evaluated false true statement © 2004 Pearson Addison-Wesley. All rights reserved

Comparing while and do The while Loop The do Loop statement condition true false condition evaluated The while Loop true condition evaluated statement false The do Loop © 2004 Pearson Addison-Wesley. All rights reserved

Logic of a for loop initialization condition evaluated false statement true increment © 2004 Pearson Addison-Wesley. All rights reserved

Infinite Loops The body of a while loop eventually must make the condition false If not, it is called an infinite loop, which will execute until the user interrupts the program This is a common logical error You should always double check the logic of a program to ensure that your loops will terminate normally © 2004 Pearson Addison-Wesley. All rights reserved

Infinite Loops An example of an infinite loop: int count = 1; while (count <= 25) { printf (“%d\n”, count); count = count - 1; } This loop will continue executing until interrupted (Control-C) or until an underflow error occurs © 2004 Pearson Addison-Wesley. All rights reserved

Nested Loops Similar to nested if statements, loops can be nested as well That is, the body of a loop can contain another loop For each iteration of the outer loop, the inner loop iterates completely © 2004 Pearson Addison-Wesley. All rights reserved

Nested Loops How many times will the string "Here" be printed? count1 = 1; while (count1 <= 10) { count2 = 1; while (count2 <= 20) printf ("Here"); count2++; } count1++; 10 * 20 = 200 © 2004 Pearson Addison-Wesley. All rights reserved

Analogy for Nested Loops http://www.brandondufau.com/archives/odometer%201.jpg

Analogy for Nested Loops Inner Loop Outer Loop http://static.howstuffworks.com/gif/odometer2.jpg

Other Stuff that we could not cover last time

Break and Continue

Comparing Float Values You should rarely use the equality operator (==) when comparing two floating point values (float or double) Two floating point values are equal only if their underlying binary representations match exactly Computations often result in slight differences that may be irrelevant In many situations, you might consider two floating point numbers to be "close enough" even if they aren't exactly equal © 2004 Pearson Addison-Wesley. All rights reserved

Comparing Float Values To determine the equality of two floats, you may want to use the following technique: if (abs(f1 - f2) < TOLERANCE) printf ("Essentially equal"); If the difference between the two floating point values is less than the tolerance, they are considered to be equal The tolerance could be set to any appropriate level, such as 0.000001 © 2004 Pearson Addison-Wesley. All rights reserved

Comparing Characters As we've discussed, C character data is based on the ASCII character set ASCII establishes a particular numeric value for each character, and therefore an ordering We can use relational operators on character data based on this ordering For example, the character '+' is less than the character 'J' because it comes before it in the ASCII character set © 2004 Pearson Addison-Wesley. All rights reserved

ASCII Table

Extended ASCII Codes

Comparing Characters In ASCII (and Unicode) the digit characters (0-9) are contiguous and in order Likewise, the uppercase letters (A-Z) and lowercase letters (a-z) are contiguous and in order Characters Unicode Values 0 – 9 48 through 57 A – Z 65 through 90 a – z 97 through 122 © 2004 Pearson Addison-Wesley. All rights reserved

The Conditional Operator C has a conditional operator that uses condition to determine which of two expressions is evaluated Its syntax is: condition ? expression1 : expression2 If the condition is true, expression1 is evaluated; if it is false, expression2 is evaluated The value of the entire conditional operator is the value of the selected expression

The Conditional Operator The conditional operator is similar to an if-else statement, except that it is an expression that returns a value For example: larger = ((num1 > num2) ? num1 : num2); If num1 is greater than num2, then num1 is assigned to larger; otherwise, num2 is assigned to larger The conditional operator is ternary because it requires three operands

Boolean Expressions in C C does not have a boolean data type. Therefore, C compares the values of variables and expressions against 0 (zero) to determine if they are true or false. If the value is 0 then the result is implicitly assumed to be false. If the value is different from 0 then the result is implicitly assumed to be true. C++ and Java have boolean data types.

Relational Operators A condition often uses one of C's equality operators or relational operators == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator (==) and the assignment operator (=) © 2004 Pearson Addison-Wesley. All rights reserved

Logical Operators Boolean expressions can also use the following logical operators: ! Logical NOT && Logical AND || Logical OR They all take boolean operands and produce boolean results Logical NOT is a unary operator (it operates on one operand) Logical AND and logical OR are binary operators (each operates on two operands) © 2004 Pearson Addison-Wesley. All rights reserved

Logical NOT The logical NOT operation is also called logical negation or logical complement If some condition a is true, then !a is false; if a is false, then !a is true Logical expressions can be shown using a truth table a !a true false © 2004 Pearson Addison-Wesley. All rights reserved

Logical AND and Logical OR The logical AND expression a && b is true if both a and b are true, and false otherwise The logical OR expression a || b is true if a or b or both are true, and false otherwise © 2004 Pearson Addison-Wesley. All rights reserved

Logical Operators Expressions that use logical operators can form complex conditions if (total < MAX+5 && !found) printf ("Processing…"); All logical operators have lower precedence than the relational operators Logical NOT has higher precedence than logical AND and logical OR © 2004 Pearson Addison-Wesley. All rights reserved

Logical Operators A truth table shows all possible true-false combinations of the terms Since && and || each have two operands, there are four possible combinations of conditions a and b a b a && b a || b true false © 2004 Pearson Addison-Wesley. All rights reserved

Boolean Expressions Specific expressions can be evaluated using truth tables total < MAX found !found total < MAX && !found false true © 2004 Pearson Addison-Wesley. All rights reserved

Boolean Expressions in C C does not have a boolean data type. Therefore, C compares the values of variables and expressions against 0 (zero) to determine if they are true or false. If the value is 0 then the result is implicitly assumed to be false. If the value is different from 0 then the result is implicitly assumed to be true. C++ and Java have boolean data types.

Short-Circuited Operators The processing of logical AND and logical OR is “short-circuited” If the left operand is sufficient to determine the result, the right operand is not evaluated if (count != 0 && total/count > MAX) printf ("Testing…"); This type of processing must be used carefully The outcome may be compiler dependent!!! © 2004 Pearson Addison-Wesley. All rights reserved

Debugging (this is not in your textbook) (this is specific to Dev-C++ but it generalizes easily to other programming environments)

Add a Breakpoint

The current line turns red

In the Debug window click “Run to Cursor”

To debug step by step click “Next Step”

To debug step by step click “Next Step”

The output window shows the output of your program as it is being exuted step by step

This is the second iteration when i=1

Add a watch (for the variable i)

The value of i is displayed and updated continuously as the program is debugged

Debugging with Functions

Add a Breakpoint

The current line turns red

In the Debug window click “Run to Cursor”

Click “Next Step”

Add a watch (for the value of i)

The value of i is displayed and updated continuously as the program is debugged

Add a watch for the value of a

Because a is defined in a different function its value is not available outside that function

Add a watch for the value of p

The value of p is not available as it is a function argument for a different function

Click “Step Into”

We are debugging inside the function now

The values of a and p are now defined

Back to the main function

Questions?

THE END