Scala中的do ... while循環

做...在Scala循環 (do...while loop in Scala)

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

Scala中的do ... while循環用于多次運行代碼塊。 執行次數由退出條件定義。 如果此條件為TRUE,則代碼將運行,否則它將僅在第一次運行

The do...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 exit condition that can be any variable or expression, the value evaluated in TRUE if it's positive and FALSE if it's zero.

當程序沒有有關發生的確切執行次數的信息時,使用do ... while循環 。 執行次數由退出條件定義,退出條件可以是任何變量或表達式,如果值為正數則為TRUE,如果為零則為FALSE

This loop always runs once in the life span of code. If the condition is initially FALSE. The loop will run once in this case.

該循環在代碼的生命周期中始終運行一次。 如果條件最初為FALSE 。 在這種情況下,循環將運行一次。

The do...while loop is also called exit controlled loop because its condition is checked after the execution of the loop's code block.

do ... while循環也稱為退出控制循環,因為在執行循環的代碼塊后會檢查其條件。

Syntax of do...while loop:

do ... while循環的語法:

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

Flow chart of do...while loop:

do ... while循環流程圖:

do while loop in Scala

Example of do...while loop:

do ... while循環的示例:

object MyClass {
def main(args: Array[String]) {
var myVar = 12; 
println("This code prints myVar even if it is greater that 10")
do{
println(myVar)
myVar += 2;
}
while(myVar <= 10)
}
}

Output

輸出量

This code prints myVar even if it is greater that 10
12

Code explanation:

代碼說明:

This code implements the use of the do...while loop in Scala. The do...while loop being an exit control loop checks the condition after the first run. This is why the code prints 12 but the condition is myVar should not be greater than 10. In this we put the condition after the code block this means the code will run like this, print myVar increment it by 2 (using assignment operator) and then checks for the condition of the loop.

這段代碼實現了Scala中do ... while循環的使用。 作為退出控制循環的do ... while循環在第一次運行后檢查條件。 這就是為什么代碼打印12但條件為myVar不應大于10的原因。在此,我們將條件放在代碼塊之后,這意味著代碼將像這樣運行,打印myVar將其遞增2(使用賦值運算符 ),然后檢查循環條件。

The assignments for the do...while loop that you can complete and submit to know your progress.

您可以完成并提交do ... while循環的作業,以了解自己的進度。

Assignment 1 (difficulty - beginner): Print all prime numbers from 342 - 422 that is divisible by 3. (use do-while loop and functions.)

作業1(難度-初學者):打印342-422中所有可被3整除的質數。(使用do-while循環和函數。)

Assignment 2 (difficulty - intermediate): Print all number between 54 - 1145 that have 11,13 and 17 as a factor.

作業2(難度-中間):打印54-1145之間的所有數字,其中11、13和17為因數。

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377533.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377533.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377533.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

十七、完整神經網絡模型訓練步驟

以CIFAR-10數據集為例&#xff0c;訓練自己搭建的神經網絡模型架構 一、準備CIFAR-10數據集 CIFAR10官網使用文檔 torchvision.datasets.CIFAR10(root"./CIFAR_10",trainTrue,downloadTrue) 參數描述root字符串&#xff0c;指明要下載到的位置&#xff0c;或已有數…

μC/OS-Ⅱ 操作系統內核知識

目錄μC/OS-Ⅱ任務調度1.任務控制塊2.任務管理3.任務狀態μC/OS-Ⅱ時間管理μC/OS-Ⅱ內存管理內存控制塊MCBμC/OS-Ⅱ任務通信1.事件2.事件控制塊ECB3.信號量4.郵箱5.消息隊列操作系統內核&#xff1a;在多任務系統中&#xff0c;提供任務調度與切換、中斷服務 操作系統內核為每…

第二版tapout

先說說上次流回來的芯片的測試情況。 4月23日&#xff0c; 芯片采用裸片直接切片&#xff0c; bond在板子上&#xff0c;外面加了一個小塑料殼來保護&#xff0c;我們就直接拿回來測試了。 測試的主要分為模擬和數字兩部分&#xff0c; 數字部分的模塊基本都工作正常&#xff0…

cd-rom門鎖定什么意思_CD-ROM的完整形式是什么?

cd-rom門鎖定什么意思CD-ROM&#xff1a;光盤只讀存儲器 (CD-ROM: Compact Disc Read-Only Memory) CD-ROM is an abbreviation of "Compact Disc Read-Only Memory". It is a data storage memory in the form of an optical compact disc, which is read by a syst…

遠程工作時的協作工具

遠程工作時的協作工具 Google Hangout 用于日常會議和面對面交談,在國內其實可以用qq來帶起。Campfire 用于一天來的持續對話。Screenhero 用于分享屏幕&#xff0c;一起寫代碼,這個比較有用,可以一起寫代碼。Balsamiq 用于計劃要制作的 UI。Asana 用于管理任務Google Docs 用于…

十八、完整神經網絡模型驗證步驟

網絡訓練好了&#xff0c;需要提供輸入進行驗證網絡模型訓練的效果 一、加載測試數據 創建python測試文件&#xff0c;beyond_test.py 保存在dataset文件夾下a文件夾里的1.jpg小狗圖片 二、讀取測試圖片&#xff0c;重新設置模型所規定的大小(32,32)&#xff0c;并轉為tens…

二分法變種小結(leetcode 34、leetcode33、leetcode 81、leetcode 153、leetcode 74)

目錄二分法細節1、leetcode 34 在排序數組中查找元素的第一個和最后一個位置2、不完全有序下的二分查找(leetcode33. 搜索旋轉排序數組)3、含重復元素的不完全有序下的二分查找(81. 搜索旋轉排序數組 II)3、不完全有序下的找最小元素(153. 尋找旋轉排序數組中的最小值)4、二維矩…

ID3D11DeviceContext::Dispatch與numthread筆記

假定——[numthreads(TX, TY, TZ)] // 線程組尺寸。既線程組內有多少個線程。Dispatch(GX, GY, GZ); // 線程組的數量。既有多少個線程組。 那么——SV_GroupThreadID{iTX, iTY, iTZ} // 【線程組內的】線程3D編號SV_GroupID{iGX, iGY, iGZ} // 線程組的3D編號SV_DispatchT…

kotlin 查找id_Kotlin程序查找Square區域

kotlin 查找idFormula to find area of Square: area side*side 查找Square面積的公式&#xff1a; area side * side Given the value of side, we have to find the area of Square. 給定side的值&#xff0c;我們必須找到Square的面積。 Example: 例&#xff1a; Input…

小米手環6解決天氣未同步問題

最近我發現了我的米6手環天氣不同步&#xff0c;打開Zepp Life刷新同步也不行&#xff0c;后來我找了一些網上的解決方法&#xff0c;嘗試了一些也還不行&#xff0c;我這人喜歡瞎搗鼓&#xff0c;無意之間給整好了&#xff0c;后來我開始總結自己操作步驟&#xff0c;就在剛才…

c# datetime._C#| DateTime.Month屬性與示例

c# datetime.DateTime.Month屬性 (DateTime.Month Property) DateTime.Month Property is used to get the month component of this object. Its a GET property of DateTime class. DateTime.Month屬性用于獲取此對象的月份組成部分。 這是DateTime類的GET屬性。 Syntax: 句…

C++ 內存分配層次以及memory primitives的基本用法

分配層次 C memory primitives 分配釋放類型是否可重載mallocfree()C函數不可newdeleteC表達式不可::operator new()::operator delete()C函數可allocator::allocate()allocator::deallocate()C標準庫可自由設計并以之搭配任何容器 分配與釋放的四個用法 1、malloc and delet…

jQuery easyui layout布局自適應瀏覽器大小

首先解釋一下標題的含義&#xff0c;當我們用jQuery easyui layout 進行布局的時候&#xff0c;可能會遇到這樣一個問題&#xff0c;那就是當手工調整瀏覽器大小&#xff0c;或者最大化、還原窗口的時候&#xff0c;layout的某個區域不能填充因為瀏覽器擴大而產 生的空白區域&a…

JAVA 作業:圖形界面

自己動手寫的一個小JAVA 程序&#xff1a; 一個學生管理小系統&#xff0c;雖然很挫&#xff0c;但是這我學JAVA的第一步。學了2天JAVA沒有白費&#xff01; 1 import java.awt.*;2 import java.awt.event.*;3 import java.util.ArrayList;4 5 import javax.swing.*;6 7 class …

一、Pytorch對自定義表達式自動求導

例如&#xff1a;y ax bx c&#xff0c;分別對a&#xff0c;b&#xff0c;c求導 若當a3&#xff0c;b4&#xff0c;c5&#xff0c;x1時 import torch from torch import autogradx torch.tensor(1.) a torch.tensor(3.,requires_gradTrue) b torch.tensor(4.,requires…

css菜單下拉菜單_在CSS中創建下拉菜單

css菜單下拉菜單CSS | 創建下拉菜單 (CSS | Creating Dropdown) Trivia: 瑣事&#xff1a; We know the importance of navigation bar on our webpage, we know the importance of a list of items too on our webpage but what is the importance of dropdown in web pages?…

C++ 內存基本構件new/delete的意義、運用方式以及重載方式

目錄一、對new的理解1、new做了什么2、new被編譯器轉為了什么3、operate_new源代碼長啥樣二、對delete的理解1、delete做了什么2、delete被編譯器轉為了什么3、operator delete源代碼長啥樣三、構造函數與析構函數的直接調用參考一、對new的理解 1、new做了什么 C告訴我們&am…

二、線性代數

一、張量 張量表示由一個數值組成的數組&#xff0c;這個數組可能有多個維度 import torchx torch.arange(15) x # tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])1&#xff0c;shape shape屬性可以訪問張量的形狀 x.shape # torch.Size([15])2&a…

Wordpress prettyPhoto插件跨站腳本漏洞

漏洞名稱&#xff1a;Wordpress prettyPhoto插件跨站腳本漏洞CNNVD編號&#xff1a;CNNVD-201311-413發布時間&#xff1a;2013-11-28更新時間&#xff1a;2013-11-28危害等級&#xff1a; 漏洞類型&#xff1a;跨站腳本威脅類型&#xff1a;遠程CVE編號&#xff1a; 漏洞來源…

JavaScript學習筆記1

Netscape 公司 DOM模型&#xff0c;層(layer)-用ID標識。 HTML標記頁面上的元素&#xff0c; <div id "mydiv">This is my div</div> CSS為這個頁面元素定位 #mydiv{ position:absolute; left:320px; top:110px; } JavaScript 訪問 (DOM模塊不同&#x…