Scala中的while循环

在Scala中的while循环 (while loop in Scala)

while loop in Scala is used to run a block of code multiple numbers of time. The number of executions is defined by an entry condition. If this condition is TRUE the code will run otherwise it will not run.

Scala中的while循环用于多次运行代码块。 执行次数由输入条件定义。 如果此条件为TRUE,则代码将运行,否则它将不运行。

while loop is used when the program does not have information about the exact number of executions taking place. The number of executions is defined by an entry condition that can be any variable or expression, the value evaluated in TRUE if it's positive and FALSE if it's zero.

当程序没有有关确切执行次数的信息时,使用while循环 。 执行次数由输入条件定义,该条件可以是任何变量或表达式,如果值为正数则为TRUE,如果值为零则为FALSE。

This loop might not run even once in the life span of code. If the condition is initially FALSE. The flow will not go in to loop in this case.

即使在代码的生命周期中,此循环也可能不会运行一次。 如果条件最初为FALSE 。 在这种情况下,该流将不会进入循环。

The while loop is also called entry controlled loop because its condition is checked before the execution of the loop's code block.

while循环也称为条目控制循环,因为在执行循环的代码块之前先检查其条件。

Syntax of while loop:

while循环的语法:

    while(condition){
//Code to be executed...
} 

Flow chart of while loop:

while循环流程图:

while loop in Scala

Example of while loop:

while循环示例:

object MyClass {
def main(args: Array[String]) {
var myVar = 2; 
println("This code prints 2's table upto 10")
while(myVar <= 10){
println(myVar)
myVar += 2;
}
}
}

Output

输出量

This code prints 2's table upto 10
2
4
6
8
10

Code explanation:

代码说明:

The above code is to explain the usage of while loop in Scala. In this code, we have used a variable named myVar that is used as a counter in while loop. For printing text, to the screen, we are using println method that moves the cursor to the next line after printing. We have used += assignment operator that we have learned previously. The code prints that table of 2 up to 10.

上面的代码是解释Scala中while循环的用法。 在这段代码中,我们使用了一个名为myVar的变量,该变量在while循环中用作计数器。 为了将文本打印到屏幕上,我们使用println方法,该方法在打印后将光标移动到下一行。 我们使用了先前学习的+ =赋值运算符。 该代码打印2到10的表。

From this section, I am going to give you assignments that you can complete and submit to know your progress.

在本节中,我将为您提供可以完成并提交的作业,以了解您的进度。

Assignment 1 (difficulty - beginner): Print all numbers from 100 - 399 that are divisible by 3. (use while loop and functions.)

作业1(难度-初学者):打印100至399的所有可被3整除的数字(使用while循环和函数)。

Assignment 2 (difficulty - intermediate): Print all number between 541 - 643 that have 3, 5 and 7 as a factor.

分配2(难度-中等):打印541-643之间的所有数字,其中3、5和7为因数。

翻译自: https://www.includehelp.com/scala/the-while-loop-in-scala.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377310.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux操作系统启动过程

在做开发的过程中&#xff0c;突然发现&#xff0c;要对系统做一些有意义的改变&#xff0c;必须要对操作系统的启动过程有一定的了解&#xff0c;不然就是修改你都不知道从哪里下手啊&#xff0c;然后就是找来资料看&#xff0c;去网上看别人的博客&#xff0c;有了前一周一些…

方法命名的区别

GetDecimalFromString ExtractDecimal 这2个方法名那个比较好呢。上边的明显的是中式英语&#xff0c;单词拼凑而成的。下边的更加流畅一些。方法名称取名还是很有要求的。要通俗易懂还要符合文法。从上边的可以扩展出什么想法呢。 ExtractDecimalExtractDoubleExtractInt16Ext…

工作排序问题

Problem statement: 问题陈述&#xff1a; Given an array of jobs where every job has a deadline and a profit. Profit can be earned only if the job is finished before the deadline. It is also given that every job takes a single unit of time, so the minimum p…

牛客网与leetcode刷题(高频题中简单or中等的)

目录1、反转链表2、排序3、先序中序后序遍历4、最小的k个数5、子数组的最大累加和6、 用两个栈实现队列7、142. 环形链表 II8、20. 有效的括号9、最长公共子串(动态规划),磕磕绊绊10、二叉树之字形层序遍历11、重建二叉树12、LRU缓存13、合并两个有序链表15、大数加法16、一个二…

AMUL的完整形式是什么?

AMUL&#xff1a;阿南德牛奶联盟有限公司 (AMUL: Anand Milk Union Limited) AMUL is an abbreviation of Anand Milk Union Limited. It is an Indian milk product cooperative dairy organization that is based in the small town of Anand in the state of Gujarat. AMUL …

mochiweb 源码阅读(十一)

大家好&#xff0c;今天周六&#xff0c;继续接着上一篇&#xff0c;跟大家分享mochiweb源码。上一篇&#xff0c;最后我们看到了mochiweb_socket_server:listen/3函数&#xff1a; listen(Port, Opts, State#mochiweb_socket_server{sslSsl, ssl_optsSslOpts}) ->case moch…

Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能 (转)

转载请注明出处&#xff1a;http://blog.csdn.net/guolin_blog/article/details/9255575 最 近项目中需要用到ListView下拉刷新的功能&#xff0c;一开始想图省事&#xff0c;在网上直接找一个现成的&#xff0c;可是尝试了网上多个版本的下拉刷新之后发现效果都不怎么理 想。有…

Python中的append()和extend()

列出append()方法 (List append() method) append() method is used to insert an element or a list object to the list and length of the List increased by the 1. append()方法用于将元素或列表对象插入列表&#xff0c;并且列表长度增加1。 Syntax: 句法&#xff1a; …

红黑树的实现

目录1、红黑树原理1、红黑树性质2、变换规则&#xff08;从插入结点的角度来讲&#xff09;1.变色2.左旋3.右旋3、删除结点需要注意的地方2、代码1、定义结点以及构造函数2、定义红黑树类以及声明它的方法3、左旋4、右旋5、插入操作6、修正操作7、删除操作3、参考链接1、红黑树…

118 - ZOJ Monthly, July 2012

http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId339 都是赛后做的。。。弱爆了 A题是找由2和5组成的数字的个数 直接打个表就行了 只是比赛的时候不知道怎么打表啊。。 View Code #include<cstdio> #include<cstring> #include<algorith…

edp1.2和edp1.4_EDP​​的完整形式是什么?

edp1.2和edp1.4EDP​​&#xff1a;电子数据处理 (EDP: Electronic Data Processing) EDP is an abbreviation of Electronic Data Processing. It alludes to the functioning of operations of commercial data, documents processing of storing, with the use of a compute…

高效读书心得

1.尽量阅读中文版 虽然有人英文很强&#xff0c;有的翻译很差&#xff0c;但AnyWay 中文阅读与理解的时间&#xff0c;略读与快速定位感兴趣内容的速度还是要快一些。 2.即时批注、总结笔记与交流 虽然爱书&#xff0c;但发现最有效的读书方式还是不断的制造脂批本&…

《MySQL——增删改查以及常用语法》

目录登录和退出MySQL服务器基本语法&#xff08;增删改查&#xff09;登录和退出MySQL服务器 # 登录MySQL 密码 $ mysql -u root -p12345612 # 退出MySQL数据库服务器 exit;基本语法&#xff08;增删改查&#xff09; -- 显示所有数据库 show databases;-- 创建数据库 CREA…

WCF简介

一、简介 WCF是Windows Communication Foundation缩写&#xff0c;是Microsoft为构建面向服务的应用提供的分布式通信编程框架&#xff0c;是.NET Framework 3.5的重要组成部分。使用该框架&#xff0c;开发人员可以构建跨平台、安全、可靠和支持事务处理的企业级互联应用解决方…

css链接样式_CSS中的样式链接

css链接样式CSS样式链接 (CSS Styling Links) The links in CSS can be styled in various ways to make our website more presentable and attractive. The links can also be styled depending on their states e.g. visited, active, hover, etc. CSS中的链接可以通过各种方…

《MySQL——约束》

目录主键约束唯一主键非空约束默认约束外键约束主键约束 -- 主键约束 -- 使某个字段不重复且不得为空&#xff0c;确保表内所有数据的唯一性。 CREATE TABLE user (id INT PRIMARY KEY,name VARCHAR(20) );-- 联合主键 -- 联合主键中的每个字段都不能为空&#xff0c;并且加起…

UIControl事件

CHENYILONG BlogUIControl事件 FullscreenUIControl事件1.UIControlEventTouchDown单点触摸按下事件&#xff1a;用户点触屏幕&#xff0c;或者又有新手指落下的时候。2.UIControlEventTouchDownRepeat多点触摸按下事件&#xff0c;点触计数大于1&#xff1a;用户按下第二、三、…

C++ 为什么要使用#ifdef __cplusplus extern C { #endif

经常看到别人的头文件 有这样的代码 #ifdef __cplusplus extern "C" { #endif// C 样式 的函数#ifdef __cplusplus } #endif 为什么要这样呢&#xff1f; 因为 C 语言不支持重载函数 也就是同名函数&#xff0c;参数却不一样,C支持&#xff0c;其编译器对函数名的处理…

css中的媒体查询_CSS中的媒体查询

css中的媒体查询CSS | 媒体查询 (CSS | Media Queries) Creating a web page is not an easy task as it requires loads of content and data so that it becomes strongly responsive to the users. To do that various contents are even added e.g.: resources, informativ…

SharePoint2013安装组件时AppFabric时出现1603错误,解决方法:

采用PowerShell命令批量下载必备组件: 下载完成后&#xff0c;采用批处理命令安装必备组件。 注&#xff1a;SPS2013安装必备组件及批处理下载地址&#xff1a; 需要将必备组件放在安装文件的PrerequisiteInstallerFiles文件夹中&#xff0c;将PreReq2013.bat放在安装文件根目录…