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.pswp.cn/news/377310.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377310.shtml
英文地址,請注明出處:http://en.pswp.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放在安裝文件根目錄…