SlideShare una empresa de Scribd logo
1 de 19
Basic Scientific Programming
Selective Execution
Selective Execution
Selective execution gives the programmer the
ability to execute a part of the code if a
condition is met. Else that part of code is not
executed.
The selection is based on a logical
expression.
Logical Expressions






Logical expressions may be simple or
compound.

Simple logical expression
expression1 logical operator expression2
Compound logical expression
formed by combining simple logical
expressions by using the following logical
operators: NOT, AND, OR, EQV, NEQV.
Relational Operators
Symbol

Meaning

Example

<

Is less than

x<y

>

Is greater than

x>y

==

Is equal to

x == y

<=

Is less than or equal to

x <= y

>=

Is greater than or equal to

x >= y

/=

Is not equal to

x /= y


The result of a logical operation is either
“true” or “false”.
5>4
9<6
3>3

true.
false.
false.
Characters & Relational Operators




Numeric codes are used to establish an
ordering for the character set.
ASCII & EBCDIC “Refer to App. A”
“CAT” < “DOG” true C precedes D in the table.
“June” < “July” false l precedes n in the table.
“June” < “june” true J precedes j in the table.
Two strings are compared as though blanks are appended to the
shorter string.
“cat” < “cattle” true a blank space precedes t in the table.


Logical expressions formed by comparing real values
with == are often evaluated as false, why?
because real values are stored with some error.
Real:: value
value = 10
if ( (1/value) == 0.1) then
do something
end if
In this example (1/value) might not result in 0.1 due to
storage limitations, it might be 0.998 or 0.10001
Declaring logical variables
There are two logical constants in Fortran
.TRUE. .FALSE.
Declaration:
LOGICAL :: VARIABLE_LIST
Reading logical variables is similar to reading other
data types.
Ex. Logical :: A,B,C
Read *, A,B,C
Input : .T. , F, .False “any form is valid input”
Logical Operators






.NOT.
.AND.
.OR.
.EQV.
.NEQV.
A
F
T

.NOT. A
T
F
AND/ OR
AND
A
F

B
F

F

T

T
T

OR

A .AND. B
F

A
F

B
F

A .OR.B
F

F

F

T

T

F

F

T

F

T

T

T

T

T

T
EQV/NEQV
EQV
A
F

B
F

F

T

T
T

NEQV

A .EQV. B
T

A
F

B A .NEQV. B
F
F

F

F

T

T

F

F

T

F

T

T

T

T

T

F
Priorities


In logical expression containing several of
these operators, the operations are
performed in the following order
.NOT.  .AND.  .OR.  .EQV. (or .NEQV.)

Parenthesis may be used to indicate the subexpressions that must be evaluated first.
Ex:

A .AND. B .OR. .NOT. C
A .AND. (B .OR. .NOT. C)
The general case
The most general case is to have logical expressions
containing arithmetic, relational and logical operators.
The operations are performed in the following order:
1. Arithmetic operations
2. Relational operations
3. Logical operations
Ex: (((b**2 – 4* a* c) >=5) .and. c+2 <6)
Flowchart Symbols
Begin or End

Input or Output

Assignment

Point of Selection

Multiway Selection

Flow Lines join
IF Construct
IF (Logical Expression) then
Statement-Sequence
End IF
Selection is made between
either executing the sequence
of statements or bypassing it.

if

Stat. seq
General form of IF Construct
IF (Logical Expression) then
Statement-Sequence1
ELSE
Statement-Sequence2
End IF
Selection is made between
either executing sequence1
or sequence2.

if

Stat. seq1

Stat. seq2
Nested IF


The sequence of statements in an IF construct may
itself contain other IF constructs.
Ex: if(X<=0) then
value = -X
else
if (X<1.0)then
value = X**2
else
value=1.0
end if
end if
IF

ELSE IF Construct

IF(logical expression1) then
statement sequence 1
ELSE IF (logical expression2) then
statement sequence 2
ELSE IF (logical expression3) then
statement sequence 3
.
.
.
ELSE
statement sequence n
END IF
IF




ELSE IF Construct
This structure is used in cases involving
multiple alternatives.
Only one of the statement sequences will be
executed, the others will be skipped.

Más contenido relacionado

La actualidad más candente

Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
David Livingston J
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
valarpink
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
David Livingston J
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
Andrew Raj
 

La actualidad más candente (20)

Ch02
Ch02Ch02
Ch02
 
Module 3 : using value type variables
Module 3 : using value type variablesModule 3 : using value type variables
Module 3 : using value type variables
 
JetBrains MPS: Typesystem Aspect
JetBrains MPS: Typesystem AspectJetBrains MPS: Typesystem Aspect
JetBrains MPS: Typesystem Aspect
 
Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7Software Engineering - Module 3: Lesson7
Software Engineering - Module 3: Lesson7
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
 
Parsing
ParsingParsing
Parsing
 
Programming in c by pkv
Programming in c by pkvProgramming in c by pkv
Programming in c by pkv
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Type conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programmingType conversion, precedence, associativity in c programming
Type conversion, precedence, associativity in c programming
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
Frequently asked questions in c
Frequently asked questions in cFrequently asked questions in c
Frequently asked questions in c
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
C interview questions
C interview  questionsC interview  questions
C interview questions
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Java basic operators
Java basic operatorsJava basic operators
Java basic operators
 
Attribute grammer
Attribute grammerAttribute grammer
Attribute grammer
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 

Similar a 8 if

CS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docxCS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docx
richardnorman90310
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
abhi353063
 

Similar a 8 if (20)

OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
MODULE_2_Operators.pptx
MODULE_2_Operators.pptxMODULE_2_Operators.pptx
MODULE_2_Operators.pptx
 
Java Programming
Java Programming Java Programming
Java Programming
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Types of c operators ppt
Types of c operators pptTypes of c operators ppt
Types of c operators ppt
 
Control structures i
Control structures i Control structures i
Control structures i
 
Python Week 1.pptx
Python Week 1.pptxPython Week 1.pptx
Python Week 1.pptx
 
Pythonintro
PythonintroPythonintro
Pythonintro
 
Programming Fundamentals lecture 7
Programming Fundamentals lecture 7Programming Fundamentals lecture 7
Programming Fundamentals lecture 7
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
C operators
C operatorsC operators
C operators
 
INTRODUCTION TO C PROGRAMMING - PART 2
INTRODUCTION TO C PROGRAMMING - PART 2INTRODUCTION TO C PROGRAMMING - PART 2
INTRODUCTION TO C PROGRAMMING - PART 2
 
Cprogrammingoperator
CprogrammingoperatorCprogrammingoperator
Cprogrammingoperator
 
Programming in C
Programming in CProgramming in C
Programming in C
 
CS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docxCS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docx
 
Programming-in-C
Programming-in-CProgramming-in-C
Programming-in-C
 
Control Structures
Control StructuresControl Structures
Control Structures
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
 

Más de fyjordan9

Más de fyjordan9 (17)

17recursion
17recursion17recursion
17recursion
 
16 subroutine
16 subroutine16 subroutine
16 subroutine
 
15 functions
15 functions15 functions
15 functions
 
14 arrays
14 arrays14 arrays
14 arrays
 
13 arrays
13 arrays13 arrays
13 arrays
 
12 doloops
12 doloops12 doloops
12 doloops
 
11 doloops
11 doloops11 doloops
11 doloops
 
10 examples for if statement
10 examples for if statement10 examples for if statement
10 examples for if statement
 
9 case
9 case9 case
9 case
 
7 files
7 files7 files
7 files
 
6 read write
6 read write6 read write
6 read write
 
5 format
5 format5 format
5 format
 
4 design
4 design4 design
4 design
 
3 in out
3 in out3 in out
3 in out
 
2 int real
2 int real2 int real
2 int real
 
1 arithmetic
1 arithmetic1 arithmetic
1 arithmetic
 
PHYS303
PHYS303PHYS303
PHYS303
 

Último

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

8 if

  • 2. Selective Execution Selective execution gives the programmer the ability to execute a part of the code if a condition is met. Else that part of code is not executed. The selection is based on a logical expression.
  • 3. Logical Expressions    Logical expressions may be simple or compound. Simple logical expression expression1 logical operator expression2 Compound logical expression formed by combining simple logical expressions by using the following logical operators: NOT, AND, OR, EQV, NEQV.
  • 4. Relational Operators Symbol Meaning Example < Is less than x<y > Is greater than x>y == Is equal to x == y <= Is less than or equal to x <= y >= Is greater than or equal to x >= y /= Is not equal to x /= y
  • 5.  The result of a logical operation is either “true” or “false”. 5>4 9<6 3>3 true. false. false.
  • 6. Characters & Relational Operators   Numeric codes are used to establish an ordering for the character set. ASCII & EBCDIC “Refer to App. A” “CAT” < “DOG” true C precedes D in the table. “June” < “July” false l precedes n in the table. “June” < “june” true J precedes j in the table. Two strings are compared as though blanks are appended to the shorter string. “cat” < “cattle” true a blank space precedes t in the table.
  • 7.  Logical expressions formed by comparing real values with == are often evaluated as false, why? because real values are stored with some error. Real:: value value = 10 if ( (1/value) == 0.1) then do something end if In this example (1/value) might not result in 0.1 due to storage limitations, it might be 0.998 or 0.10001
  • 8. Declaring logical variables There are two logical constants in Fortran .TRUE. .FALSE. Declaration: LOGICAL :: VARIABLE_LIST Reading logical variables is similar to reading other data types. Ex. Logical :: A,B,C Read *, A,B,C Input : .T. , F, .False “any form is valid input”
  • 10. AND/ OR AND A F B F F T T T OR A .AND. B F A F B F A .OR.B F F F T T F F T F T T T T T T
  • 11. EQV/NEQV EQV A F B F F T T T NEQV A .EQV. B T A F B A .NEQV. B F F F F T T F F T F T T T T T F
  • 12. Priorities  In logical expression containing several of these operators, the operations are performed in the following order .NOT.  .AND.  .OR.  .EQV. (or .NEQV.) Parenthesis may be used to indicate the subexpressions that must be evaluated first. Ex: A .AND. B .OR. .NOT. C A .AND. (B .OR. .NOT. C)
  • 13. The general case The most general case is to have logical expressions containing arithmetic, relational and logical operators. The operations are performed in the following order: 1. Arithmetic operations 2. Relational operations 3. Logical operations Ex: (((b**2 – 4* a* c) >=5) .and. c+2 <6)
  • 14. Flowchart Symbols Begin or End Input or Output Assignment Point of Selection Multiway Selection Flow Lines join
  • 15. IF Construct IF (Logical Expression) then Statement-Sequence End IF Selection is made between either executing the sequence of statements or bypassing it. if Stat. seq
  • 16. General form of IF Construct IF (Logical Expression) then Statement-Sequence1 ELSE Statement-Sequence2 End IF Selection is made between either executing sequence1 or sequence2. if Stat. seq1 Stat. seq2
  • 17. Nested IF  The sequence of statements in an IF construct may itself contain other IF constructs. Ex: if(X<=0) then value = -X else if (X<1.0)then value = X**2 else value=1.0 end if end if
  • 18. IF ELSE IF Construct IF(logical expression1) then statement sequence 1 ELSE IF (logical expression2) then statement sequence 2 ELSE IF (logical expression3) then statement sequence 3 . . . ELSE statement sequence n END IF
  • 19. IF   ELSE IF Construct This structure is used in cases involving multiple alternatives. Only one of the statement sequences will be executed, the others will be skipped.

Notas del editor

  1. always use parenthesis to control the order of execution.