玖叶教程网

前端编程开发入门

周一分享(三十一):Matlab入门学习1

Matlab入门学习

Getting started with Matlab

分享兴趣,传播快乐,

增长见闻,留下美好。

亲爱的您,

这里是LearingYard学苑!

今天小编为大家带来Matlab入门学习。

欢迎您的用心访问!

本期推文阅读时长大约5分钟,请您耐心阅读。

Share interests, spread happiness,

increase knowledge, and leave beauty behind.

Dear you,

this is LearningYard Academy!

Today,the editor brings you Matlab introductory learning.

Welcome your visit!

The reading time of this tweet is about 5 minutes, please read it with patience.

今天小编为大家介绍if语句,for语句,while语句。

Today I will introduce you to if statement, for statement, while statement.

if语句

if statement

if语句是条件语句,每一个条件语句都需要end关键词结束。

The if statement is a conditional statement, and each conditional statement needs to end with the end keyword.

下面举一个例子,生成一个随机数,如果小于0.3,显示“<0.3”,如果大于0.7,显示“>0.7”,其他显示“0.3~0.7”。

Let's take an example to generate a random number. If it is less than 0.3, it will display "<0.3", if it is greater than 0.7, it will display ">0.7", and the others will display "0.3~0.7".

代码如下图所示:

The code is shown below:

运行结果如下图所示:

The running result is shown in the following figure:

for语句

for statement

for语句通过遍历一个数组,执行有限次的循环。

The for statement executes a finite number of loops by iterating over an array.

基本语法如下:

The basic syntax is as follows:

for 变量=初值:增量:结束值

程序模块;

end

for variable = initial value: increment: end value

program module;

end

while语句

while statement

如果进入循环前,循环的次数是不确定的,要根据循环中代码的执行结果来决定何时退出,此时可用while循环。

If the number of times of the loop is uncertain before entering the loop, it is necessary to decide when to exit according to the execution result of the code in the loop. At this time, a while loop can be used.

基本语法如下:

The basic syntax is as follows:

while 条件

循环体语句

end

while condition

loop body statement

end

for与while对比

for vs while

for用于循环次数确定的情况下,while多用于循环次数不确定的情况。

for is used when the number of loops is determined, while is mostly used when the number of loops is uncertain.

下面举一个例子,使用for语句和while语句分别求1到100的和。

Here's an example that uses the for statement and the while statement to sum from 1 to 100, respectively.

for语句代码如下图:

The code for the for statement is as follows:

while语句代码如下图:

The while statement code is as follows:

得出的结果相同,都是5050。

The result is the same, both 5050.

今天的分享就到这里了。

如果您对今天的文章有独特的想法,

欢迎给我们留言,

让我们相约明天。

祝您今天过得开心快乐!

That's it for today's sharing.

If you have a unique idea about today’s article,

welcome to leave us a message,

let us meet tomorrow.

I wish you a happy day today!

参考资料:谷歌翻译、百度、哔哩哔哩

本文由LearningYard学苑原创,如有侵权请在后台留言!

文字|Zheng

排版|Zheng

审核|任务达人

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言