Unsur-unsur Algoritma

Slides:



Advertisements
Similar presentations
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Advertisements

ITEC113 Algorithms and Programming Techniques
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Control Flow IF Statement Visualisation of the IF Statement IF... THEN... ELSE Construct Visualisation of the IF... THEN Construct Visualisation of the.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Flow of Control Part 1: Selection
CPS120: Introduction to Computer Science Decision Making in Programs.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 05 (Part III) Control Statements: Part II.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Fortran: Control Structures Session Three ICoCSIS.
Decision Making and Branching
Control Structures 1 The if, for, and while Statements §5.1, §5.4, & §5.5 (Several examples here; also Lab #4)
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.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Branching statements.
Definition of the Programming Language CPRL
CNG 140 C Programming (Lecture set 3)
‘C’ Programming Khalid Jamal.
Chapter 7: User-Defined Functions II
REPETITION CONTROL STRUCTURE
Introduction to the C Language
The switch Statement, and Introduction to Looping
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.
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Lecture 3- Decision Structures
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 6: Conditional Statements and Loops
JavaScript: Control Statements.
Lecture 07 More Repetition Richard Gesick.
ITM 352 Flow-Control: Loops
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
More Selections BIS1523 – Lecture 9.
Chapter 13 Control Structures
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Boolean Expressions to Make Comparisons
Loop Construct.
Objectives You should be able to describe: The while Statement
Flow of Control.
CSC215 Lecture Control Flow.
DATA TYPES AND OPERATIONS
Controlling Program Flow
REPETITION Why Repetition?
Control Structures.
Chapter 13 Control Structures
Presentation transcript:

Unsur-unsur Algoritma

Bentuk Program A simple program takes the form: [PROGRAM name ] statements END [PROGRAM name] Optional items are shown in square brackets.   Comments may be included as required, for example: PROGRAM Prog1 PRINT *, "Welcome to FTN95" ! Output a simple message. END PROGRAM Prog1

Program dalam bahasa Fortran Fortran is designed for formula translation. Many elementary Fortran programs will include 3 (three) components: The input of data which is stored as variables The evaluation of formulae using the input data The output of the results Input Hitungan / evaluasi formula Output

Contoh Program dlm Fortran Here is a simple example: PROGRAM Prog2 INTEGER i,j READ *, i j = 2*i+3 PRINT *, j END PROGRAM Prog2 input Evaluasi formula output

Contoh Program dlm Fortran The next example is similar but uses REAL numbers: PROGRAM Prog3 REAL x WRITE(*,"(a)",ADVANCE="NO") "Enter a real number:" READ *,x PRINT *, "The result is:", x/3.0-0.9E6 END PROGRAM Prog3

Penjelasan In Fortran it is possible to use a variable without explicitly declaring its type. Variables that are not declared are assumed to be either INTEGER or REAL depending on the first letter in the name. However, the use of this implicit typing is not recommended. Moreover, if a variable name is spelled incorrectly, the implicit typing rule will cause the rogue variable to be declared leading to erroneous results. In order to avoid this happening, it is recommended that the implicit typing facility be switched off. To do this, include the statement IMPLICIT NONE before any type declarations. Note: FTN95 can be configured so that IMPLICIT NONE is the default for all program units (see the compiler options /CONFIG and /IMPLICIT_NONE).

Pengaturan Langkah Perintah / Flow Control The statements within a Fortran program are implemented in their natural order. Elementary programs of the kind discussed so far (containing input, formula translation and output) do not have the ability to apply conditions to the process or to repeat sections of the code. For this we need mechanisms for controlling the order and the conditions under which the statements are executed. This section describes such flow control constructs together with the IF statement, the first of which is the IF construct.

Flow Controls IF construct atau skema perintah kondisional IF Logical IF statement atau kondisional IF diikuti perintah secara langsung CASE construct atau skema perintah kondisional CASE DO construct atau skema perintah berulang DO Iterated DO construct GOTO statement STOP statement

IF Construct A simple form for the IF construct is: IF(logical_expression) THEN statement_block 1 [ELSE statement_block 2 ] ENDIF The ELSE section is optional. If the logical expression is true, the first block is executed, otherwise the second.

IF Construct For example: IF(x > 0.0) THEN y = x ELSE y = 0.0 ENDIF If the ELSE section consists of one IF construct, then ELSE and IF can be reduced to ELSEIF provided that one of the associated ENDIFs is deleted. Sometimes it is simpler to use a CASE construct.

Logical IF statement The general form for the IF statement is: IF(logical_expression) simple_statement For example the statement: IF( x < 0.0) x = - x makes x positive.

CASE Construct The CASE construct provides an alternative to an IF construct containing multiple ELSEIFs. It takes the general form: SELECT CASE(expression) CASE(case_selector 1) statement_block 1 CASE(case_selector 2) statement_block 2 . . . CASE DEFAULT statement_block END SELECT

CASE Construct CASE constructs can be named. The expression provides a value upon which the selection is based and must be a scalar of type INTEGER, LOGICAL or CHARACTER. CASE DEFAULT is optional and is not necessarily placed last. It provides for the situation where none of the given CASEs applies. The case_selector values must be of the same type as expression. They contain a single constant value or a list of constant values. Ranges may also be used.

CASE Construct For example: ! Month and Days are of type INTEGER ! Month has been assigned a value. SELECT CASE(Month) CASE(4,6,9,11) Days = 30 CASE(1,3,5,7:8,10,12) Days = 31 CASE(2) Days = 28 ! Code to test for a leap year CASE DEFAULT ! Code to flag an error END SELECT

CASE Construct For example: ! Month and Days are of type INTEGER ! Month has been assigned a value. SELECT CASE(Month) CASE(4,6,9,11) Days = 30 CASE(1,3,5,7:8,10,12) Days = 31 CASE(2) Days = 28 ! Code to test for a leap year CASE DEFAULT ! Code to flag an error END SELECT

CASE Construct A range takes the form CASE(low: high) where low or high (but not both) may be omitted. For example, if high is omitted then the condition becomes expression>low. For example: SELECT CASE(n) CASE( : -1) Sign = - 1 ! if n < 0 CASE(0) Sign = 0 ! if n = 0 CASE( 1: ) Sign = 1 ! if n > 0 END SELECT

CASE Construct Each case must be unique with no possible overlap. In the case of a CHARACTER expression, it may represent a single character or a character string.

DO contsruct There are two forms for the DO construct. the simple DO construct the iterated DO construct A simple DO construct takes the form: DO    statement_block END DO This causes the block of statements to be repeated.

DO contsruct Typically, within the block of statements, there will be an IF statement of the form: IF(logical_expression) EXIT The EXIT statement causes control to pass to the statement following END DO. The CYCLE statement is an alternative to EXIT. It causes control to pass to a point immediately after the last statement of the block, allowing repetition to continue.

DO contsruct DO constructs can be named If a DO construct is named then the same name must be appended to the corresponding END DO and may optionally be appended to an enclosing EXIT or CYCLE. If DO constructs are nested, one within another, then a name attached to EXIT or CYCLE provides an extra degree of control.

DO contsruct A DO WHILE construct provides an alternative construction in the form: DO WHILE(logical_expression) statement_block END DO This is equivalent to: DO IF(.NOT.logical_expression) EXIT statement_block END DO

Iterated DO Construct The iterated DO construct takes the form: DO variable=start_value, end_value [, increment] statement_block END DO This is equivalent to: variable=start_value DO WHILE(variable <= end_value) statement_block variable=variable+increment END DO

Iterated DO Construct variable is an INTEGER variable, whilst start_value, end_value, and increment are INTEGER expressions. increment is optional and when omitted defaults to 1 (unity). increment may be negative in which case start_value>end_value. For example: DO I = 10,0,-2 ! Do something for i=10,8,6,4,2 and 0 END DO EXIT and CYCLE can be used with all forms of DO

GOTO statement Any statement can be given a label in the form of an integer constant in the range 1 to 99999 at the beginning of a line. The form for the GOTO statement is: GOTO label GOTO statements should only be used in exceptional circumstances. For example: 1. To exit from deeply nested flow constructs. 2. To jump to one of a number of terminating error conditions at the end of a program or procedure. Wherever possible, use an IF construct in preference to GOTO statements.

STOP statement The form for the STOP statement is: STOP [message] message is optional and is a character string. STOP causes a normal (successful) termination of the program. If present, the message is output to the standard output device. STOP does not flag an error condition to the operating system. The terminating END of a PROGRAM implies STOP.