Branches and Program Design

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.
Control Structures Selections Repetitions/iterations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Chapter 3 Program Design And Branching Structures.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 14.1Introduction 14.2Algorithms 14.3Pseudocode 14.4Control Structures 14.5The if Selection.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Top-down Program Design, Relational and Logical Operators
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
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.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
Chapter 4: Control Structures I
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
Chapter 4: Control Structures I
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
JavaScript: Control Statements I
SELECTION STATEMENTS (1)
Chapter 4: Control Structures I
MSIS 655 Advanced Business Applications Programming
Chapter 4 Selection.
CS2011 Introduction to Programming I Selections (II)
Chapter 4: Control Structures I (Selection)
Week 3 – Program Control Structure
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Presentation transcript:

Branches and Program Design Chapter 3 Branches and Program Design Chapter 3 - Branches and Program Design

Program Design Process (1) 1. Clearly state the problem that you are trying to solve. 2. Define the inputs required by the program and the outputs to be produced by the program. 3. Decompose the program into classes and their associated methods. 4. Design the algorithm that you intend to implement for each method. 5. Turn the algorithm(s) into Java statements. 6. Test the Java program. Chapter 3 - Branches and Program Design

Program Design Process (2) Chapter 3 - Branches and Program Design

Chapter 3 - Branches and Program Design Pseudocode Pseudocode is a hybrid mixture of Java and English. It is structured like Java, with a separate line for each distinct idea or segment of code, but the descriptions on each line are in English. Each line of the pseudocode should describe its idea in plain, easily understandable English. Example: Prompt user to enter temperature in degrees Fahrenheit Read temperature in degrees Fahrenheit (tempF) tempK in kelvins ¬ (5./9.) * (tempF - 32) + 273.15 Write temperature in kelvins Chapter 3 - Branches and Program Design

Chapter 3 - Branches and Program Design Relational Operators Relational operators are operators with two numerical operands that yield a boolean (true/false) result. Example: 23.6 > 20.0 produces the result true Chapter 3 - Branches and Program Design

Chapter 3 - Branches and Program Design Logical Operators Logical operators are operators with one or two boolean operands that yield a boolean (true/false) result. Example: If test1 is true and test2 is false, then the expression test1 || test2 is true. Chapter 3 - Branches and Program Design

Hierarchy of Operations Operations within expressions are evaluated in the following order: Chapter 3 - Branches and Program Design

Chapter 3 - Branches and Program Design The if Structure The if structure specifies that a statement will be executed if and only if a certain boolean expression is true. Multiple statements can be executed by including them in a compound statement (between { and } ). Examples: if ( grade > 70 ) System.out.println("Passed"); if ( discr > 0. ) { x1 = ( -b + Math.sqrt(discr) ) / (2.*a); x2 = ( -b - Math.sqrt(discr) ) / (2.*a); } Chapter 3 - Branches and Program Design

Chapter 3 - Branches and Program Design The if/else Structure The if/else structure specifies that certain statements will be executed if a boolean expression is true, and different statements will be executed if the expression is false. Example: if ( discr < 0. ) { System.out.println(”There are complex roots.”); } else { System.out.println(”There are real roots.”); Chapter 3 - Branches and Program Design

The Conditional Operator The conditional operator is essentially a compact if/else structure. The form of this operator is: boolean_expr ? expr1 : expr2 If the boolean expression is true, the result of this operator is expression 1. Otherwise, the result of this operator is expression 2. Example–the following statements print out “Passed”: grade = 82; System.out.println(grade > 70 ? ”Passed" : ”Failed”); Chapter 3 - Branches and Program Design

The switch Structure (1) The switch structure permits a programmer to select a particular code block to execute based on the value of a single integer or character expression. Chapter 3 - Branches and Program Design

The switch Structure (2) If the switch expression matches a particular case selector, the statements following that case selector will be executed. If the switch expression does not match any case selector, then the statements following the default case selector will be executed. Note that all statements from the chosen case selector until the end of the structure will be executed, unless a break statement is included to stop execution! The break state-ments are necessary for this structure to work properly. Chapter 3 - Branches and Program Design