Chapter 3 Applications & Logical Constructs. 2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding.

Slides:



Advertisements
Similar presentations
Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.
Advertisements

CONTROL STRUCTURES: SEQUENTIAL, SELECTIVE, AND REPETITIVE
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
27 March, 2000 CS1001 Lecture 7 Nested IFs Character Input IF and Input Validation Project Introduction.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Control Structure.
23 March, 2000 CS1001 Lecture 6 Logical Expressions Logical Operators Selective Execution Web page --
C++ for Engineers and Scientists Third Edition
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Fundamentals of Python: From First Programs Through Data Structures
Introduction to FORTRAN-90 University of Liverpool course.
Fundamentals of Python: First Programs
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.
CPS120: Introduction to Computer Science Decision Making in Programs.
Problem Solving with Decisions
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
Fortran 77 By Tanatorn Somboonvorakit, ID:
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Branches and Program Design
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
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.
Algorithm Design.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
Chapter 4 Selection: the if-else and switch-case instructions.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Chapter#3 Part1 Structured Program Development in C++
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
Week 4 Program Control Structure
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
1 VB-04-Control Structures 16 March 2016 Visual Basic Control Structures - Selection.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Program design Program Design Process has 2 phases:
CNG 140 C Programming (Lecture set 3)
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Algorithms and Flowcharts
Decisions Given hours worked and pay rate, calculate total pay
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
The Selection Structure
Engineering Problem Solving with C++, Etter/Ingber
Decisions Given hours worked and pay rate, calculate total pay
Control Structures: Selection Statement
Chapter 4 Selection.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Decision I (if Statement)
Structured Program Development in C++
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
REPETITION Why Repetition?
Presentation transcript:

Chapter 3 Applications & Logical Constructs

2 Application 1 Temperature Conversion: Write a program that will convert a Celsius temperature to the corresponding Fahrenheit temperature. SOLUTION: Assume that the temperature converting formula is given by F = (9/5) C + 32 INPUT: A temperature in degrees Celsius OUTPUT: A temperature in degrees Fahrenheit Programs T_conv_1.f and T_conv_2.f BASIC STEPS FOR ALGORITHM STEP1 --> Enter C STEP2 --> Calculate F STEP3 --> Display F

3 Application 2 Area of a Triangle: Write a program to read values for the three sides a, b, c of a triangle and then calculate its perimeter and its area. These should be displayed together with the values of a, b, c using appropriate labels. (for the area, you might use Hero’s formula for the area of a triangle: where s is one-half the perimeter.) SOLUTION: See the source code area_tri.f for the solution

4 Selective Execution Three basic methods of control are used in designing algorithms and programs: 1) Sequence: Each step are performed exactly once and steps are performed in a strictly sequential manner. 2) Selection: One of a number of alternative actions is selected and executed 3) Repetition: One or more steps are performed repeatedly.

5 Logical Expressions Logical expressions may be either simple or compound. Simple logical expressions are logical constants or variables or relational expressions of the form. expression 1 relational-operator expression 2 where both expression 1 and expression 2 are numeric or character (or logical) expressions, and the relational-operator may be any of the following:

6 Simple Logical Expressions

7 Examples: x < 6.7 number == 1999 ASCII uses codes in the range 0 through 255 (i.e. A=65, B=66, …Z=90). (See complete table of ASCII characters in Appendix D of Ellis’s book) ”A” < ”G” is true logical expression ”cat” < ”dog” true ”June” < ”July” true

8 Compound Logical Expressions They are formed by combining logical expressions by using the logical operators.NOT. (negation).AND. (conjunction).OR. (disjunction).EQV. (equivalence).NEQV. (nonequivalence) all possible values are displayed in truth tables (next page)

9 Compound Logical Expressions

10 IF Constructs  A simple IF construct is as follows if (logical-expression) then statement-sequence end if EXAMPLE: if (X >= 0) then Y=X * X Z=sqrt(X) end if

11 Logical IF Statement  Fortran also provides a simpler IF construct called Logical IF statement if (logical-expression) statement if (1.5 <= X.AND. X<= 2.5) print *, X

12 General form of the IF construct if (logical-expression) then statement-sequence 1 else statement-sequence 2 end if If the logical expression is true, statement-sequence 1 is executed and statement-sequence 2 is bypassed.

13 Applications  (Problem #1) Solve the quadratic equation using an IF construct. SOLUTION: Read : The coefficients A, B, and C Output : The roots of the equation Check the source code Quad_eq_1.f !

14 Applications  (Problem #2) Rank two numbers Two real data values, X and Y are given. Write a program that finds the ranking between two entered numbers. Use If construct for the solution in your program. Check the source code rank_two.f !

15 IF-ELSE IF Constructs  It is possible to use the IF construct to design selection structures that contain more than two alternatives. if (logical-expression 1 ) then statement-sequence 1 else if (logical-expression 2 ) then statement-sequence 2 else if (logical-expression 3 ) then statement-sequence 3. else statement-sequence n end if

16 IF-ELSE IF Constructs When an if-else if construct is executed, the logical expressions are evaluated to determine the first expression that is true; the associated sequence of statements is executed, and execution then continues with the next statement following the next construct. If none of the logical expressions is true, the statement sequence associated with the else statement is executed, and execution then continues with the statement following the construct. This if construct thus implements an n-way selection structure in which exactly one of statement-sequence 1, statement-sequence 2, statement-sequence 3,…, statement-sequence n is executed.

17 Named Constructs if and if-else if constructs may be named by attaching a label at the beginning and end of the construct name: if (logical-expression) then end if name For example: update: if (x > largest) then largest=x position=N end if update

18 Named Constructs Naming prevents the possible mixing within the nested if’s. The name used in an if or if-else if construct may also be attached following the keyboard then in any else if part of the construct and following the keyboard else in an else part. For example: EmpType: if (EmployeeType == ”S”) then ! Salaried employee print *,”Enter employee’s annual salary” read *, Salary pay = salary / 52 else EmpType ! Hourly employee print *,”Enter hours worked and hourly rate:” read *, HoursWorked, HourlyRate Overtime: if (HoursWorked > 40.0) then

19 Named Constructs pay = 40.0 * HourlyRate & + Multiplier * HourlyRate*(HoursWorked-40.0) else Overtime pay=HoursWorked*HourlyRate end if Overtime end if EmpType

20 The CASE Construct The case construct is not as general as the if-else if construct. But it is useful for implementing some selection structures. The form of a case construct is as follows: select case (selector) case (label-list 1 ) statement-sequence 1 case (label-list 2 ) statement-sequence 2. case (label-list n ) statement-sequence n end select

21 The CASE Construct Where the selector is an integer, character, or logical expression; each of the (label-list 1 ) is a list of one or more possible values of the selector, enclosed in parenthesis. The values in this list may have any of the forms value value 1 : value 2 value 1 : : value 2 to denote a single value, the range of values from value 1 through value 2, the set of all values greater than or equal to value 1, or the set of all values less than or equal to value 2, respectively. When a case construct is executed, the selector is evaluated; if this value is in label-list 1, statement-sequence 1 is executed, and execution continues with the statement following the end select statement.

22 Named CASE Construct If the value is not in any of the lists of values, the sequence of statements associated with default is executed. Named case constructs A name may be attached to a case construct: name: select case (selector). end select name

23 Named CASE Construct For example: Class: select case (ClassCode) case (1) print *, ”Freshman” case (2) print *, ”Sophomore” case (3) print *, ”Junior” case (4) print *, ”Senior” case (5) print *, ”Graduate” default print *, ”Illegal class code”,Class code end select Class

24 Exercises & Reading (Ellis’s Book)  Study examples 5.1, 5.3, 5.4 and 5.5 on pages 112, 118, 124 and 126 respectively.  Read summary on page 131.  Study Chapter 5: Pages between 103 and 131.

25 The LOGICAL data type  Two logical constants in Fortran are.TRUE. and.FALSE.  A logical variable is declared using a logical type statement logical :: list where list is a list of variables being typed as logical. An assignment statement of the form logical-variable = logical-expression can be used to assign a value to a logical variable For example: logical :: RootExists, End_of_Data End_of_Data=.true. RootExists=Discriminant >= 0 are valid assignment statements and assigns.TRUE. to RootExists if Discriminant is nonnegative and assigns.FALSE. otherwise.

26 The LOGICAL data type Display of logical variables are as follows: print *, A,B,C,.true.,.false. produces T F F F T F as output.