当前位置: 首页 > news >正文

一起学习Firtran: Fortran中的流程控制与操作符

流程控制语句

 在计算机编程中,控制程序流程是算法设计中的一个核心概念,它使得程序能够根据特定条件执行不同的操作或重复执行某段代码。这显著增强了计算机算法相对于简单数学公式的功能和灵活性。

if语句

 在单个 if 语句中只有当测试表达式(angle<90.0)为真时,结构内的代码才会执行。

program testimplicit noneinteger :: angle = 80if ( long < 90 ) thenprint *, "angle is less than 90"end ifend program test

if-else语句

在如 if 等结构内缩进代码是一种良好的编程习惯,可以提高代码的可读性。我们可以使用关键字 else 为结构添加一个备选分支。

program testimplicit noneinteger :: ageprint *, "Enter your age:"read *, ageif (age > 18) thenprint *, "You are Old B."else if (age .eq. 18) thenprint *, "You are an adult."elseprint *, "You are a little B."end ifend program test

循环结构do

循环具有一个整数计数器变量,用于跟踪当前正在执行的循环迭代。在此示例中,我们为此计数器变量使用了一个通用名称:do 循环中的 i

当我们定义循环的开始时,使用计数器变量名后跟等号(=)来指定计数变量的起始值和最终值。

program testimplicit noneinteger :: ido i = 1, 10print *, iend doend program test

带跳步的do循环 

 我们只需要在循环条件末尾设置跳步的步数即可。

program testimplicit noneinteger :: ido i = 1, 10, 2print *, i  ! Print odd numbersend doend program test

条件循环(do while)

可以使用关键字 do while 向循环添加条件。只要给定的条件评估为真,循环就会执行。

program testimplicit noneinteger :: ii = 1do while (i < 11)print *, ii = i + 1end doend program test

循环控制语句(exit 、cycle)

通常情况下,如果满足某个条件,需要停止循环。Fortran 提供了两个可执行语句来处理这种情况。需要注意的是,在嵌套循环中使用时,cycle 和 exit 语句作用于最内层的循环。

exit 用于提前退出循环。它通常包含在 if 语句内。

program testimplicit noneinteger :: ido i = 1, 100if (i > 10) thenexit  ! Stop printing numbersend ifprint *, iend doend program test

另一方面,cycle 会跳过循环中剩余的部分,并进入下一个循环周期 。

program testimplicit noneinteger :: ido i = 1, 10if (mod(i, 2) == 0) thencycle  ! Don't print even numbersend ifprint *, iend doend program test

嵌套循环控制:标签

在任何编程语言中,嵌套循环都是一个常见的情况。嵌套循环指的是存在于另一个循环内的循环。Fortran 允许程序员为每个循环添加标签或名称。如果为循环添加了标签,则有两个潜在的好处:

  1. 可以提高代码的可读性(当命名有意义时)。
  2. 可以使用带标签的 exit 和 cycle,从而实现对循环的精细控制。
program testimplicit noneinteger :: i, jinteger :: sum_limit = 15logical :: found = .false.! 外层循环,带有标签outer_loopouter_loop: do i = 1, 10! 内层循环,带有标签inner_loopinner_loop: do j = 1, 10if (i + j > sum_limit) then! 如果和超过了限制,跳出内层循环的当前迭代cycle inner_loopend ifprint *, 'i =', i, 'j =', j, 'sum =', i + j! 如果需要在特定的和后想要退出所有循环if (i + j == 10) then! 设置标志变量表示已找到found = .true.! 退出外层循环exit outer_loopend ifend do inner_loopend do outer_loop! 根据是否找到特定的和,打印相应的消息if (found) thenprint *, 'Found a pair (i, j) whose sum is 10.'elseprint *, 'Did not find any pair (i, j) whose sum is 10.'end ifend program test

可并行化循环(do concurrent)

do concurrent 循环用于明确指定循环内部没有相互依赖关系;这告诉编译器可以使用并行化/SIMD 来加速循环的执行,并更清晰地传达程序员的意图。更具体地说,这意味着任何给定的循环迭代都不依赖于其他循环迭代的先前执行。同时,任何可能发生的状态变化都必须仅在每个循环内部发生。这些要求对可以放在循环体内的内容施加了限制。

简单地用 do concurrent 替换一个循环并不能保证并行执行。上述解释并没有详细说明编写正确 do concurrent 循环所需满足的所有要求。编译器也可以自行决定如何优化(例如,对于执行简单计算且迭代次数较少的循环,如以下示例),这意味着它们可能不会优化循环。通常,需要编译器标志来激活循环的可能并行化。

program testimplicit noneinteger :: iinteger, parameter :: n = 1000real, dimension(n) :: a, b, c! 初始化数组a和ba = 1.0b = 2.0do concurrent (i = 1:n)c(i) = a(i) + b(i)end do! 打印数组的前10个元素以验证结果print *, 'First 10 elements of array c:'do i = 1, 10print *, c(i)end doend program test

操作符

编程中,逻辑表达式用于评估和比较值,以确定条件是否为真。构建逻辑表达式时,可以使用关系运算符和逻辑运算符。

关系运算符

关系运算符用于比较两个值,并返回一个布尔结果(真或假)。 

操作符  

选择  

描述

==

.eq.

测试两个操作数是否相等

/=

.ne.

测试两个操作数是否不相等

>

.gt.

测试左操作数是否严格大于右操作数

<

.lt.

测试左操作数是否严格小于右操作数

>=

.ge.

测试左操作数是否大于或等于右操作数

<=

.le.

测试左操作数是否小于或等于右操作数

program testimplicit noneinteger :: iinteger :: jprint *, "Enter First Integers"read *, iprint *, "Enter Second Integers"read *, jif (i .eq. j) thenprint *, "i = j"else if (i .ne. j) thenprint *, "i != j"if (i .gt. j) thenprint *, "i > j"else if (i .lt. j) thenprint *, "i < j"end ifend ifif (i .ge. j) thenprint *, "i >= j"end ifif (i .le. j) thenprint *, "i <= j"end ifend program test

逻辑运算符

逻辑运算符用于组合或反转布尔表达式,从而构建更复杂的条件。

操作符  

描述

.and.

如果左右操作数都为 TRUE,则为 TRUE

.or.

如果左侧或右侧为 TRUE 或者两个操作数都为 TRUE,则为 TRUE

.not.

如果右操作数为 FALSE,则为 TRUE

.eqv.

如果左操作数与右操作数具有相同的逻辑值,则为 TRUE

.neqv.

如果左操作数与右操作数具有相反的逻辑值,则为 TRUE

program testimplicit nonelogical :: a, blogical :: r_and, r_or, r_not, r_eqv, r_neqv! 初始化逻辑变量a = .true.b = .false.! 使用 .and. 操作符r_and = a .and. bprint *, 'a .and. b = ', r_and! 使用 .or. 操作符r_or = a .or. bprint *, 'a .or. b = ', r_or! 使用 .not. 操作符r_not = .not. aprint *, '.not. a = ', r_not! 使用 .eqv. 操作符(等价于)r_eqv = a .eqv. bprint *, 'a .eqv. b = ', r_eqv! 使用 .neqv. 操作符(不等价于)r_neqv = a .neqv. bprint *, 'a .neqv. b = ', r_neqv! 额外的示例,展示当 a 和 b 相同时的情况a = .true.b = .true.r_and = a .and. bprint *, 'When both a and b are true, a .and. b = ', r_andr_or = a .or. bprint *, 'When both a and b are true, a .or. b = ', r_orr_eqv = a .eqv. bprint *, 'When both a and b are true, a .eqv. b = ', r_eqvr_neqv = a .neqv. bprint *, 'When both a and b are true, a .neqv. b = ', r_neqvend program test

http://www.lryc.cn/news/513494.html

相关文章:

  • Sonic:开源Go语言开发的高性能博客平台
  • SpringBoot教程(十四) SpringBoot之集成Redis
  • RSI和CCI指标组合:如何评估需求供应区?昂首资本实战指南
  • PTPVT 插值说明
  • Spring MVC和servlet
  • java下载文件流,不生成中间文件。
  • 计算机专业考研 408 学科学习方法
  • mapper文件的解释
  • 常见协议的高危软件漏洞信息
  • Mediatek Android13 ROM定制
  • RedisInsight:企业级 Redis 管理与分析工具
  • c# 快捷键模块
  • 【笔记】增值税计算笔记
  • 请解释 JavaScript 中的闭包,以及它的优缺点和常见使用场景?
  • SpringBoot 集成 Caffeine 实现本地缓存
  • druid连接池参数配置
  • 【OceanBase】通过 OceanBase 的向量检索技术构建图搜图应用
  • Linux 安装运行gatk的教程
  • 什么是unit l2 norm
  • 手写顺序流程图组件
  • 适配器模式概述
  • Logo设计免费生成器:轻松设计个性化标志
  • 智能停车场车牌识别计费系统
  • 谷歌开通第三方平台OAuth登录及Java对接步骤
  • 人体:精妙绝伦的生命之躯
  • python的urllib模块和http模块
  • Java [后端] 开发日常记录(1)
  • jetbrain 安装 copilot
  • 万里数据库GreatSQL监控解析
  • OpenCV-Python实战(9)——滤波降噪