Chapter 3 Program Design And Branching Structures.

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.
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.
Introduction to arrays
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
1 Chapter 2 Basic Elements of Fortran Programming.
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
ITEC113 Algorithms and Programming Techniques
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 4 Programming and Software EXCEL and MathCAD.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Program Design and Development
Chapter 2: Input, Processing, and Output
Top-down Program Design, Relational and Logical Operators
Program Design, Relational and Logical Operators Selim Aksoy Bilkent University Department of Computer Engineering
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 3 Programming and Software.
Chapter 6 Control Statements Continued
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Chapter 3 Planning Your Solution
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Chapter 3 Making Decisions
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
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)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Stewart Venit ~ Elizabeth Drake Developing a Program.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Branches and Program Design
The Software Development Process
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
1 Program Planning and Design Important stages before actual program is written.
MATLAB 及其工程应用 MATLAB and It’s Engineering Application 主讲教师: 胡章芳
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Chapter 6 Control Statements Continued
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
The Hashemite University Computer Engineering Department
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
How Computers Solve Problems Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
1-1 Logic and Syntax A computer program is a solution to a problem.
Chapter 2: Input, Processing, and Output
Chapter 2- Visual Basic Schneider
Chapter 2 Assignment and Interactive Input
The Selection Structure
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
An Introduction to Programming with C++ Fifth Edition
Computer Programming.
Problem Solving Techniques
Programming Right from the Start with Visual Basic .NET 1/e
Chapter 2- Visual Basic Schneider
A programming language
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
Programming Concepts and Database
Chapter 3: Selection Structures: Making Decisions
Developing a Program.
Chapter 2: Input, Processing, and Output
Chapter 3: Selection Structures: Making Decisions
DATA TYPES AND OPERATIONS
Control Structures.
Presentation transcript:

Chapter 3 Program Design And Branching Structures

Design approaches On-the-fly OK with simple programs Simple programs can be debugged easily Top-down design Necessary for large/complex programs Task  subtasks Test each subtask individually, then combine all Debugging is easier

Top-down-design steps 1. Clearly state the problem 2. Define required inputs and produced outputs 3. Design algorithm to be used 4. Turn algorithm into Fortran statements 5. Test program

1.Clearly state the problem Write a program which calculates the total energy of a falling object. Not clear enough Write a program which prompts the user to enter the parameters of a falling object (height, mass, and velocity), calculates the total energy of the object (potential + kinetic) and prints on the screen for the user the total energy of the object. Clearer

2. Inputs & Outputs In the previous example: Inputs: mass height velocity Outputs: Total energy of the falling object

3. Algorithm Prompt (ask) user for inputs Compute the total energy using the equations: Gravity = 9.8 Potential energy = mass * gravity * height Kinetic energy = 0.5 * mass * velocity 2 Total energy = Potential energy + Kinetic energy

4. Algorithm  Fortran statements PROGRAM Energy implicit none REAL,parameter :: gravity=9.8 REAL :: height, mass, velocity REAL :: Potential_Energy, Kinetic_Energy, Total_Energy write (*,*) "Please enter the height, mass, and velocity of the object" read (*,*) height, mass, velocity Potential_Energy = mass * gravity * height Kinetic_Energy = 0.5 * mass * velocity**2 Total_Energy = Potential_Energy + Kinetic_Energy write (*,*) "The toal energy of the object = ", Total_Energy, "Joul." END PROGRAM

5. Testing Test in the LAB For big programs: ALPHA release First complete version Tested by the programmer and close friends All possible ways of using the program are tested BETA release Serious bugs in ALFA release is removed Tested by users who need the program Program is put under many different conditions Released for general use Released for everyone to use

Standard Forms of Algorithms: Pseudocode and Flowcharts An algorithm is composed of constructs. Constructs can be described using: Pseudocode Flowcharts Advantages: Standard form: Easy to understand by others Making changes in the program is easier Debugging is easier

Pseudocode: Describe algorithm using a mix of Fortran language and English language Example: Prompt user to enter height, mass, and velocity Read height, mass, and velocity Gravity  9.8 Potential energy  mass * gravity * height Kinetic energy  0.5 * mass * velocity 2 Total energy  Potential energy + Kinetic energy Write Total energy on the screen Standard Forms of Algorithms: Pseudocode and Flowcharts

Flowcharts: Describe algorithm graphically Standard shapes are used for each type of construct Standard Forms of Algorithms: Pseudocode and Flowcharts

LOGICAL constants take one of 2 values: true / false Example: Logical, parameter:: correct =.TRUE. Logical, parameter:: wrong =.FALSE. LOGICAL constants are rarely used Logical Variables and Constants

LOGICAL variables are declared like other variables Example: LOGICAL :: var1, var2, var3 LOGICAL :: var4 LOGICAL variables are more used than LOGICAL constants Logical Variables and Constants

Invalid syntax correct =.TRUE incorrect = FALSE. ERRORS …

Logical statement form: Logical_variable_name = logical experession Example: PROGRAM PASS IMPLICIT NONE CHARACTER (len=4) :: PASSWORD LOGICAL :: CHECK WRITE (*,*) “ What is the password? “ READ (*,*) PASSWORD CHECK = ( PASSWORD == ‘EASY’ ) WRITE (*,*) CHECK END PROGRAM Logical Statements

Relational operators: compare two operands and produce logical results (T/F) A1 op A2 A1 & A2: can be either numerical or character op:==/= >< >=<= Examples: 3 < 4.TRUE. 3 <= 4.TRUE. 4 <= 3.FALSE. ‘A’ < ‘B’.TRUE. 4 < ‘A’ ????? ILLEGAL (ERROR) Logical Statements.. Relational Operators

Example: PROGRAM PASS IMPLICIT NONE INTEGER :: x, y LOGICAL :: compare WRITE (*,*) “Enter numbers (x, y) to check if “ WRITE (*,*) “x > y “ WRITE (*,*) “ “ READ (*,*) x, y CHECK = (x > y) WRITE (*,*) “ The statement x > y is “, CHECK END PROGRAM Logical Statements.. Relational Operators

Combinational operators: compare two operands and produce logical results (T/F) A1 op A2 A1 & A2: logical operands (.TRUE. /.FALSE.) op:.AND..OR..EQV..NEQV..NOT Truth table for binary combinational logic operators: L1.FALSE..FALSE..TRUE..TRUE. L2.FALSE..TRUE..FALSE..TRUE. L1.AND. L2.FALSE..FALSE..FALSE..TRUE. L1.OR. L2.FALSE..TRUE..TRUE..TRUE. L1.EQV. L2.TRUE..FALSE..FALSE..TRUE. L1.NEQV. L2.FALSE..TRUE..TRUE..FALSE. L1.TRUE..FALSE..NOT. L1.FALSE..TRUE. Logical Statements.. Combinational Operators

Exercise: L1 =.TRUE. L2 =.TRUE. L3 =.FALSE. Logical expression.NOT. L1.FALSE. L1.OR. L3.TRUE. L2.NEQV. L3.TRUE. Logical Statements.. Combinational Operators

When evaluating an expression, follow these rules: 1. Arithmetic operations (e.g. (3 + 4 * ( 2 / 5)) 2. Relational logic operations (e.g. ( 3 > 4 ) ) 3. Combinational logic operations, evaluate in this order:.NOT. (left to right).AND. (left to right).OR. (left to right).EQV. and.NEQV. (left to right) Parenthesis can change order of evaluation Logical Statements.. Evaluation order

Exercise: L1 =.TRUE. L2 =.TRUE. L3 =.FALSE. Logical expression.NOT. ( 3 > 4 ).TRUE. L3.OR. ( (2 * 5) < 12 ).TRUE. L2.NEQV. ( L3.AND. ( 3 /= 4)).TRUE. Logical Statements.. Evaluation order