Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University...

22
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    4

Transcript of Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University...

Chapter 4 Loops and Character

Manipulation

Dr. Ali Can Takinacıİstanbul Technical University

Faculty of Naval Architecture and Ocean Engineeringİstanbul - Turkey

Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: [email protected]

Loops and Character Manipulation

CONTROL CONSTRUCTS: LOOPS

•Loops are Fortran constructs that permit us to execute a sequence of statements more than once. •There are two basic forms of loop constructs : while loops and iterative loops (or counting loops).•The major difference between these two types of loop is in how the repetition is controlled.•The code in a while loop is repeated an indefinite number of times until some user-specified condition is satisfied. •By contrast, the code in an iterative loop is repeated a specified number of times, and the number of repetitions is known before the loop starts.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 2

Loops and Character Manipulation

The While Loop

•A while loop is a block of statements that are repeated indefinitely as long as some condition is satisfied.

•A while loop may contain one or more EXIT statements to terminate its execution. •Each EXIT statement is usually a part of an IF statement or block of construct.•If the logicaI_expr in the IF is false when the statement is executed, the loop continues to execute.• If the logicaI_expr in the IF is true when the statement is executed.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 3

Loops and Character Manipulation

Example: Statistical Analysisthe average (or arithmetic mean) and the standard deviation of the set of measurements.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 4

Loops and Character Manipulation

The Do While Loop

•There is an alternative form of the while loop in Fortran, called the DO WHILE loop.•The DO WHILE construct has the form

•If the logical expression is true, statements 1 through n will be executed, and then control will return to the DO WHILE statement.•If the logical expression is still true, the statements will be executed again. This process will be repeated until the logical expression becomes false.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 5

Loops and Character Manipulation

The Iterative or Counting Loop

•In the Fortran language, a loop that executes a block of statements a specified number of times is called an iterative DO loop or a counting loop. The counting loop construct has the form

•where index is an integer variable used as the loop counter (also known as the loop index).•The integer quantities is istart, iend, and incr are the parameters of the counting loop.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 6

Loops and Character Manipulation

First, consider the following example:

In this case, statements 1 through n will be executed 10 times.

The index variable I will be 1 on the first time, 2 on the second time, and so on. The index variable will be 10 on the last pass through the statements.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

Loops and Character Manipulation

Second, consider the following example:

In this case, statements 1 through n will be executed five times.The index variable I will be 1 on the first time, 3 on the second time,

and so on.The index variable will be 9 on the fifth and last pass through the

statements.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 8

Loops and Character Manipulation

Third, consider the following example:

Here, statements 1 through n will never be executed, since index*incr > iend*incr on the very first time that the DO statement is

reached. Instead, control will transfer to the first statement after the END DO

statement.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 9

Loops and Character Manipulation

Finally, consider the following example:

In this case, statements 1 through n will be executed four times. The index variable I will be 3 on the first time, 1 on the second time,

-1 on the third time, and - 3 on the fourth time.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 10

Loops and Character Manipulation

Example: The Factorial Function The factorial function is defined as

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 11

Loops and Character Manipulation

The CYCLE and EXIT Statements

•If the CYCLE statement is executed in the body of a DO loop, the execution of the current iteration of the loop will stop, and control will be returned to the top of the loop.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 12

Loops and Character Manipulation

If the EXIT statement is executed in the body of a loop, the execution of the loop will stop and control will be transferred to the first executable statement after the loop.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 13

Loops and Character Manipulation

Named Loops

•It is possible to assign a name to a loop. The general form of a while loop with a name attached is

•name may be up to 31alphanumeric characterslong, beginning with a letter.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 14

Loops and Character Manipulation

Nesting Loops and Block IF Constructs

•It is possible for one loop to be completely inside another loop. •If one loop is completely inside another one, the two loops are called nested loops.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 15

Loops and Character Manipulation

CHARACTER ASSIGNMENTS AND CHARACTER MANIPULATIONS

•Character data can be manipulated by using character expressions. •A character expression can be any combination of valid character constants, character variables, character operators, and character functions.•A character operator is an operator on character data that yields a character result.•There are two basic types of character operators: substring specifications and concatenation.• Character functions are functions that yield a character result.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 16

Loops and Character Manipulation

Character Assignments

•A character expression may be assigned to a character variable with an assignment statement.•If the character expression is shorter than the length of the character variable to which it is assigned, then the rest of the variable is padded out with blanks.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 17

Loops and Character Manipulation

Substring Specifications

•A substring specification selects a portion of a character variable, and treats that portion as if it were an independent character variable.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 18

Loops and Character Manipulation© 2010, Dr. ALİ CAN TAKİNACI Slide No: 19

Loops and Character Manipulation

The Concatenation (//) Operator

•It is possible to combine two or more strings or substrings into a single large string. •This operation is known as concatenation.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 20

Loops and Character Manipulation

Relational Operators with Character Data

•In standard Fortran, character strings may be compared with character strings, and numbers may be compared with numbers, but character strings may not be compared to numbers.•The comparison is based on the collating sequence of the characters on the computer where the program is being executed. •For example, the character ‘A’ is character number 65 in the ASCII character set, while the character ‘B' is character number 66 in the set (see Appendix A).• Therefore, the logical expression ‘A’ < ‘B’ is true in the ASCII character set.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 21

Loops and Character Manipulation

• On the other hand, the character ‘a’ is character number 97 in the ASCII set, so 'a' < 'A' is false in the ASCII character set.

• Function ACHAR(i) accepts an integer value i, and returns the character at that position in the ASCII character set.

• For example, the function ACHAR(65) returns the character 'A' , because 'A' is the 65th character in the ASCII character set.

© 2010, Dr. ALİ CAN TAKİNACI Slide No: 22