A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

Introduction to C Programming
Control Structures Selections Repetitions/iterations
Decisions If statements in C.
More on Algorithms and Problem Solving
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 2: Algorithm Discovery and Design
Program Design and Development
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
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.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
The University of Texas – Pan American
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Decision Structures and Boolean Logic
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CPS120: Introduction to Computer Science Decision Making in Programs.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
CPS120: Introduction to Computer Science Decision Making in Programs.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Controlling Program Flow with Decision Structures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
1 Sections 7.2 – 7.7 Nested Control Statements Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Computer Programming
CprE 185: Intro to Problem Solving (using C)
Section 7.1 Logical Operators
Selections Java.
Lecture 3- Decision Structures
Engineering Problem Solving with C++, Etter/Ingber
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Topics The if Statement The if-else Statement Comparing Strings
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Programming Fundamentals
Expressions and Control Flow in JavaScript
JavaScript: Control Statements I
Java Programming: Guided Learning with Early Objects
Control Structures – Selection
Lecture 2: Logical Problems with Choices
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3: Program Statements
MSIS 655 Advanced Business Applications Programming
Structured Program
3 Control Statements:.
Boolean Expressions to Make Comparisons
Using Decision Structures
Outline Software Development Activities
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Structural Program Development: If, If-Else
Presentation transcript:

A8 – Control Structures if, if-else, switch

Control of flow in Java Any sort of complex program must have some ability to control flow.

Control of Flow Without this control, programs become limited to one basic job each time the program is run.

Getting started The most basic of these control structures is the if statement,

The other most basic. The other most basic of these control structures is the if-else,

The other other most basic Huh? and then the switch statement.

Structured Programming ……………..in the old days In the early days of programming (1960's), the approach to writing software was relatively primitive and ineffective. Much of the code was written with goto statements that transferred program control to another line in the code.

Spaghetti code Tracing this type of code was an exercise in jumping from one spot to another, leaving behind a trail of lines similar to spaghetti.

Spaghetti code The term "spaghetti code" comes from trying to trace code linked together with goto statements. The complexity this added to code led to the development of structured programming.

5 rules of Structured Programming a.No goto statements are to be used in writing code. None, nope, don’t do it….

5 rules of Structured Programming All programs can be written in terms of three control structures: – sequence, selection, –and iteration.

5 rules of Structured Programming.get in, get out Each control structure has one entrance point and one exit point. We will sometimes allow for multiple exit points from a control structure using the break statement.

Break command The break command allows us to leave a loop at any time

5 rules of Structured Programming Control structures may be stacked (sequenced) one after the other.

5 rules of Structured Programming Control structures may be nested inside other control structures.

sequence, selection, and iteration. There are only three necessary control structures sequence, selection, and iteration

sequence Sequence refers to the line-by-line execution as used in your programs so far.

1.sequence The program enters the sequence, does each step, and exits the sequence. This allows for sequences to do only a limited job during each execution.

selection Selection is the control structure that allows choice among different paths. Java provides different levels of selection: One-way selection with an if structure Two-way selection with an if - else structure Multiple selection with a switch structure

different levels of selection: One-way selection with an if structure Two-way selection with an if - else structure Multiple selection with a switch structure

Iteration Iteration refers to looping.. Java provides three loop structures.

Looping while loops do - while loops for loops

Algorithm Development An algorithm is a solution to a problem.. Computer scientists are in the problem-solving business.

Algorithms Algorithms will range from the easier: "finding the average of two numbers" To more difficult "visiting all the subdirectories on a hard disk, searching for a file."

Pseudocode Pseudocode refers to a rough-draft outline of an answer, written in English- like terms.

Pseudocode generally use phrases and words that are close to programming languages, but avoid using any specific language

Don’t skip this stage Once the pseudocode has been developed, translation into code occurs more easily than if we had skipped this pseudocode stage.

Stepwise refinement Stepwise refinement is the process of gradually developing a more detailed description of an algorithm.

"finding the average of two numbers" Get two numbers Add them together Divide by 2 Print out the answer

Stepwise refinement Problem solving in computer science involves overall development of the sections of a program,

Stepwise refinement expand each section with more detail,

Stepwise refinement..final steps later work out the individual steps of an algorithm using pseudocode, and then finally writing a code solution.

Relational Operators A relational operator is a binary operator that compares two values. 5 = = 3 + 2

Relational Operators < < less than > greater than <= less than or equal to >= greater than or equal to == equal to != not equal to

relational expression A relational operator is used to compare two values, resulting in a relational expression. For example: number > 16 grade == 'F' passing >= 60

boolean value The result of a relational expression is a boolean value of either true or false.

Comparing character data When character data is compared, use the ASCII code values are used to determine the answer. 'A' < 'B' evaluates as true, (65 < 66) 'd' < 'a' evaluates as false, (100 < 97) 't' < 'X' evaluates as false, (116 < 88)

Upper case, lowercase upper case letters come first in the ASCII collating sequence; the lower case letters follow after and consequently have larger ('A' = 65, 'a' = 97).

The three logical operators in the AP subset are AND, OR, and NOT. AND && OR || (two vertical bars-no space) NOT !

Logical operators compare logical operators allow us to combine conditions. For example, if a dog is gray and weighs less than 15 pounds it is the perfect lap dog

&& (and) operator The && (and) operator requires both operands (values) to be true for the result to be true. (true && true) -> true (true && false) -> false (false && true) -> false (false && false) -> false

&& (and) operator The following are Java examples of using the && (and) operator. ((2 3.0)) -> true ((1 == 0) && (2 != 3)) -> false

Short Circuit evaluation The && operator performs short-circuit evaluation in Java. If the first operand in && statement is false, the operator immediately returns false without evaluating the second half.

The || (or) operator The || (or) operator requires only one operand (value) to be true for the result to be true. (true || true) -> true (true || false) -> true (false || true) -> true (false || false) -> false

Precedence The order of operations is called precedence in Java.

Precedence and Associativity of Operators OperatorAssociativity ! unary right to left * / % left to right + - left to right >= left to right == != left to right && (and) left to right || (or) left to right = += -= *= /= right to left

logical operators logical operators have low precedence in Java, parentheses are not needed to maintain the correct order of solving problems. However, they can be used to make complex expressions more readable.

parentheses are not needed ((2 + 3 < 10) && (75 % 12 != 12)) // easier to read

parentheses are not needed (2 + 3 < 10 && 75 % 12 != 12) // harder to read Hhowever, parentheses can be used to make complex expressions more readable.