Math107Ch3

# Math107Ch3 - Introduction To Scientific Programming Chapter...

This preview shows pages 1–10. Sign up to view the full content.

Introduction To Scientific Programming Chapter 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
S.Horton/107/Ch. 3 Slide 2 Overview I. Program Control or “Flow” Tests A. Introduction to Boolean Variables and Expressions I. Program Control Structures A. Branches B. Loops I. Boolean Expressions Revisited I. Programming with Control Strucutres A. B. Java Packages
S.Horton/107/Ch. 3 Slide 3 What is Program Control or “Flow”? Program control is how the execution of a program’s instructions are handled. Programs can be written with three main control flow elements: 1. Sequence - just go to the next instruction 2. Branching or Selection – make a choice from two or more blocks to: either go to the next instruction or jump to some other instruction 3. Loop or Repetition - loop or repeat a block of code and then at the end of the loop: either go back and repeat the block of code or continue with the next instruction after the block

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
S.Horton/107/Ch. 3 Slide 4 Introduction - Java Control Statements By default, Java automatically executes the next instruction unless you use a control (branching or looping) statement. Basic control statements are: Branching if if-else if-else if-else if- … - else switch Loop while do-while for
S.Horton/107/Ch. 3 Slide 5 I. Boolean Variables and Expressions Branching allows for more than one choice when executing the next instruction or block of code. Which branch is taken depends on a test condition which evaluates to either true or false. In general, if the test is true then one block is executed, otherwise if it is false, another code block is executed. Variables or expressions that are either true or false are called boolean variables (or expressions). So, the value of a boolean variable (or expression) is always either true or false.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
S.Horton/107/Ch. 3 Slide 6 More On Boolean Expressions Often, boolean expressions are simply the comparison of two values. Example: Is A greater than B? Is A equal to B? Is A less than or equal to B? A and B can be any data type (or class), but they should be the same data type (or class).
S.Horton/107/Ch. 3 Slide 7 Java Comparison Operators Math Notation Name Java Notation Java Examples = equal to == balance == 0 answer = 'y' not equal to != income tax answer != 'y' > greater than > income > outgo greater than or equal to >= points >= 60 < less than < pressure < max less than or equal to <= income <= outgo

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
S.Horton/107/Ch. 3 Slide 8 II-A. Branching – The Java if Statement Simplest version is selection Do the next statement if test is true or skip if false Syntax: if (Boolean_Expression) Action; //execute only if true next action; //always executed Note the indentation for readability (not needed to compile or execute).
S.Horton/107/Ch. 3 Slide 9 if Example The body of the if statement is conditionally executed. Statements after the body of the if statement

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 41

Math107Ch3 - Introduction To Scientific Programming Chapter...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online