{"id":9695,"date":"2022-01-18T07:06:07","date_gmt":"2022-01-18T07:06:07","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9695"},"modified":"2024-04-11T07:39:13","modified_gmt":"2024-04-11T07:39:13","slug":"scala-inheritance-and-loops","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/","title":{"rendered":"SCALA &#8211; INHERITANCE AND LOOPS"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;SCALA - INHERITANCE AND LOOPS&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">SCALA &#8211; INHERITANCE AND LOOPS<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Inheritance in Scala\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in Scala by which one class is allowed to inherit the features (fields and methods) of another class.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"none\">Important terminology:<\/span><\/b><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Super Class:<\/span><\/b><span data-contrast=\"none\">\u202fThe class whose features are inherited is known as superclass (or a base class or a parent class).<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Sub Class:<\/span><\/b><span data-contrast=\"none\">\u202fThe class that inherits the other class is known as subclass (or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><b><span data-contrast=\"none\">Reusability:<\/span><\/b><span data-contrast=\"none\">\u202fInheritance supports the concept of \u201creusability\u201d, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/li>\n<\/ul>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"none\">How to use inheritance in Scala?<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">The keyword used for inheritance is\u202f<\/span><b><span data-contrast=\"none\">extends<\/span><\/b><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"none\">Syntax:<\/span><\/b><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class parent_class_name extends child_class_name{\u00a0\r\n\r\n\/\/ Methods and fields\u00a0\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><b><span data-contrast=\"auto\">Types of inheritance:<\/span><\/b><b><span data-contrast=\"auto\">\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h4>\n<p><b><span data-contrast=\"auto\">There are 5 types of inheritance\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">Single level inheritance:\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">In single inheritance, derived class inherits the features of one base class. In the image below, class A serves as a base class for the derived class B.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Multi-level inheritance:<\/span><\/b><br \/>\n<span data-contrast=\"none\">In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to another class. In the below image, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"none\">Hierarchical Inheritance:<\/span><\/b><b><span data-contrast=\"none\">\u202f<\/span><\/b><span data-contrast=\"none\">In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In below image, class A serves as a base class for the derived class B, C, and D.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"none\">Multiple Inheritance<\/span><\/b><b><span data-contrast=\"none\">:\u202f<\/span><\/b><span data-contrast=\"none\">In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. Scala does not support multiple inheritance with classes, but it can be achieve by traits.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">Note: <\/span><\/b><span data-contrast=\"auto\">Two classes, cannot be extended by one class at the same time. We can achieve this by the concept of trait.\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h5><b><span data-contrast=\"none\">Trait:<\/span><\/b><b><span data-contrast=\"none\">\u00a0 <\/span><\/b><span data-contrast=\"none\">By using <\/span><span data-contrast=\"none\">Trait we can achieve inheritance.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h5>\n<p><span data-contrast=\"auto\">Trait is a super type of class. where it can be common behavior or it can be individual behavior.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"none\">Hybrid Inheritance:<\/span><\/b><span data-contrast=\"none\">\u202fIt is a mix of two or more of the above types of inheritance. Since Scala doesn\u2019t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In Scala, we can achieve hybrid inheritance only through traits<\/span><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><b><span data-contrast=\"auto\">Nested if else: means one conditions has many conditions\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Insurance {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val p_amt: Double = 9083.54\u00a0\r\n\u00a0\u00a0\u00a0 val age: Int = 44\u00a0\r\n\u00a0\u00a0\u00a0 if (age &gt; 30) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (age &gt; 40) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance Premium will be 3x of principal amt:\" + 3 * (p_amt) + \"INR\")\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance premium will be 1x of principal amt: \" + p_amt)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance can not be given\")\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"> +\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Insurance Premium will be 3x of principal amt:27250.620000000003INR<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Insurance {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val p_amt: Double = 9083.54\u00a0\r\n\u00a0\u00a0\u00a0 val age: Int = 35\u00a0\r\n\u00a0\u00a0\u00a0 if (age &gt; 30) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (age &gt; 40) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance Premium will be 3x of principal amt:\" + 3 * (p_amt) + \"INR\")\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance premium will be 1x of principal amt: \" + p_amt)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance can not be given\")\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Insurance premium will be 1x of principal amt: 9083.54<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Insurance {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val p_amt: Double = 9083.54\u00a0\r\n\u00a0\u00a0\u00a0 val age: Int = 26\u00a0\r\n\u00a0\u00a0\u00a0 if (age &gt; 30) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (age &gt; 40) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance Premium will be 3x of principal amt:\" + 3 * (p_amt) + \"INR\")\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance premium will be 1x of principal amt: \" + p_amt)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Insurance can not be given\")\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Insurance can not be given<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h4><b><span data-contrast=\"auto\">If ladder statement:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h4>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Loan {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 var roi:Double = 0.0\u00a0\r\n\u00a0\u00a0\u00a0 val cibil_score:Int=830\u00a0\r\n\u00a0\u00a0\u00a0 val loan_amount = 500000\u00a0\r\n\u00a0\u00a0\u00a0 if (cibil_score&gt;700 &amp;&amp; cibil_score&lt;900) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"ROI, from HDFC would be:\" + 8.25)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Monthly EMI would be:\" +(500000)\/(8.25*12))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 else if(cibil_score&gt;800 &amp;&amp; cibil_score&lt;900) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"ROI,from HDFC would be :\" + 7.9)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Monthly EMI would be:\" +(500000)\/(7.9*12))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Loan can not be approved , because required CIBIL SCORE is less\" )\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">ROI, from HDFC would be:8.25<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Monthly EMI would be:5050.50505050505<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Loan {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 var roi:Double = 0.0\u00a0\r\n\u00a0\u00a0\u00a0 val cibil_score:Int=830\u00a0\r\n\u00a0\u00a0\u00a0 val loan_amount = 500000\u00a0\r\n\u00a0\u00a0\u00a0 if (cibil_score&gt;700 &amp;&amp; cibil_score&lt;800) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"ROI, from HDFC would be:\" + 8.25)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Monthly EMI would be:\" +(500000)\/(8.25*12))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 else if(cibil_score&gt;=800 &amp;&amp; cibil_score&lt;900) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"ROI,from HDFC would be :\" + 7.9)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Monthly EMI would be:\" +(500000)\/(7.9*12))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Loan can not be approved , because required CIBIL SCORE is less\" )\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">ROI,from HDFC would be :7.9<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Monthly EMI would be:5274.261603375527<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Loan {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 var roi:Double = 0.0\u00a0\r\n\u00a0\u00a0\u00a0 val cibil_score:Int=680\u00a0\r\n\u00a0\u00a0\u00a0 val loan_amount = 500000\u00a0\r\n\u00a0\u00a0\u00a0 if (cibil_score&gt;700 &amp;&amp; cibil_score&lt;800) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"ROI, from HDFC would be:\" + 8.25)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Monthly EMI would be:\" +(500000)\/(8.25*12))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 else if(cibil_score&gt;=800 &amp;&amp; cibil_score&lt;900) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"ROI,from HDFC would be :\" + 7.9)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Monthly EMI would be:\" +(500000)\/(7.9*12))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 else {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Loan can not be approved , because required CIBIL SCORE is less\" )\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Loan cannot be approved, because required CIBIL SCORE is less<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">one element we can use: if, if else statement\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">for multiple element we always work with for loop and while loop<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:2,&quot;335551620&quot;:2,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><b><span data-contrast=\"none\">Loops (while, do. While, for, nested loops)<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">Looping in programming languages is a feature which facilitates the execution of a set of instructions\/functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Scala provides the different types of loop to handle the condition based situation in the program. The loops in Scala are:<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:150,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"none\">while Loop<\/span><\/b><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:150,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">A while loop generally takes a condition in parenthesis. If the condition is\u202f<\/span><b><span data-contrast=\"none\">True,\u202f<\/span><\/b><span data-contrast=\"none\">then the code within the body of the while loop is executed. A while loop is used when we don\u2019t know the number of times we want the loop to be executed however we know the termination condition of the loop. It is also known as an<\/span><i><span data-contrast=\"none\">\u202fentry controlled loop<\/span><\/i><span data-contrast=\"none\">\u202fas the condition is checked before executing the loop. The while loop can be thought of as a repeating if statement.<\/span><br \/>\n<b><span data-contrast=\"none\">Syntax:<\/span><\/b><span data-contrast=\"none\">\u202f<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">while (condition)\u00a0\r\n\r\n{\u00a0\r\n\r\n\u00a0\u00a0\u00a0 \/\/ Code to be executed\u00a0\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"none\">While loop starts with the checking of the condition. <\/span><span data-contrast=\"none\">When the condition becomes false, the loop terminates which marks the end of its life cycle.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:150,&quot;335559740&quot;:240,&quot;469777462&quot;:[916,1832,2748,3664,4580,5496,6412,7328,8244,9160,10076,10992,11908,12824,13740,14656],&quot;469777927&quot;:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],&quot;469777928&quot;:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">\u00a0<\/span><b><span data-contrast=\"none\">Infinite While Loop:<\/span><\/b><span data-contrast=\"none\">\u202fWhile loop can execute infinite times which means there is no terminating condition for this loop. In other words, we can say there are some conditions which always remain true, which causes while loop to execute infinite times or we can say it never terminates.<\/span><br \/>\n<b><span data-contrast=\"none\">Example:<\/span><\/b><span data-contrast=\"none\">\u202fBelow program will print the specified statement infinite time and also give the runtime error\u202f<\/span><i><span data-contrast=\"none\">Killed (SIGKILL)\u202f<\/span><\/i><span data-contrast=\"none\">on online IDE<\/span><span data-contrast=\"none\">.<\/span><br \/>\n<span data-contrast=\"none\">\u202f<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:150,&quot;335559740&quot;:240,&quot;469777462&quot;:[916,1832,2748,3664,4580,5496,6412,7328,8244,9160,10076,10992,11908,12824,13740,14656],&quot;469777927&quot;:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],&quot;469777928&quot;:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}\">\u00a0<\/span><\/p>\n<h3><b><span data-contrast=\"none\">Do\u2026while Loop<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:150,&quot;335559740&quot;:240,&quot;469777462&quot;:[916,1832,2748,3664,4580,5496,6412,7328,8244,9160,10076,10992,11908,12824,13740,14656],&quot;469777927&quot;:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],&quot;469777928&quot;:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">A do..while loop is almost same as a while loop. The only difference is that do..while loop runs at least one time. The condition is checked after the first execution. A do..while loop is used when we want the loop to run at least one time. It is also known as the\u202f<\/span><b><span data-contrast=\"none\">exit controlled loop<\/span><\/b><span data-contrast=\"none\">\u202fas the condition is checked after executing the loop.<\/span><br \/>\n<b><span data-contrast=\"none\">Syntax:<\/span><\/b><span data-contrast=\"none\">\u202f<\/span><br \/>\n<span data-contrast=\"none\">\u202f<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">do {\u00a0\r\n\r\n\u00a0\r\n\r\n\/\/ statements to be Executed\u00a0\r\n\r\n\u00a0\r\n\r\n} while(condition);<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"none\">for Loop<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:150,&quot;335559740&quot;:240,&quot;469777462&quot;:[916,1832,2748,3664,4580,5496,6412,7328,8244,9160,10076,10992,11908,12824,13740,14656],&quot;469777927&quot;:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],&quot;469777928&quot;:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}\">\u00a0<\/span><\/p>\n<p><i><span data-contrast=\"none\">for<\/span><\/i><span data-contrast=\"none\">\u202floop has similar functionality as while loop but with different syntax. for loops are preferred when the number of times loop statements are to be executed is known beforehand. There are many variations of \u201cfor loop in Scala\u201d which we will discuss in upcoming articles. Basically, it is a repetition control structure which allows the programmer to write a loop that needs to execute a particular number of times.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:150,&quot;335559740&quot;:240,&quot;469777462&quot;:[916,1832,2748,3664,4580,5496,6412,7328,8244,9160,10076,10992,11908,12824,13740,14656],&quot;469777927&quot;:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],&quot;469777928&quot;:[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]}\">\u00a0<\/span><\/p>\n<h5><b><span data-contrast=\"none\">Nested Loops<\/span><\/b><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:150,&quot;335559740&quot;:240}\">\u00a0<\/span><\/h5>\n<p><span data-contrast=\"none\">\u00a0It can contain the for loop inside a for loop or a while loop inside a while loop. It is also possible that a while loop can contain the for loop and vice-versa.<\/span><span data-ccp-props=\"{&quot;134233117&quot;:true,&quot;134233118&quot;:true,&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:150,&quot;335559740&quot;:240}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">For loop:<\/span><\/b><span data-contrast=\"auto\"> with the help of for loop we can deal with multiple statements.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">Question: what are the difference between for loop and while loop?<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">For loop:<\/span><\/b> <b><span data-contrast=\"auto\">Iteration is known.<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">For loop we use when we know the number of iteration. When iteration is knowing how many times we are going to run loop.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Example of for loop.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])={\u00a0\r\n\u00a0\u00a0\u00a0 for(i &lt;-1 to 10){\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Element : \" +i)\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h2><b><span data-contrast=\"auto\">Question: Write a program to print step values given in range?<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><b><span data-contrast=\"auto\">Program: <\/span><\/b><span data-contrast=\"auto\">Suppose i =1,2,3,4,5,6,7,8,9,10<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">by 2<\/span><\/b><span data-contrast=\"auto\"> means it will work as step size. Firstly, it will print 1 then skip next value then print 3 and then 5,7,9<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">output will be: 1,3,5,7,9<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])={\u00a0\r\n\u00a0\u00a0\u00a0 for(i &lt;-1 to 10 by 2){\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Element : \" +i)\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">By 4: it wll work as step size 4<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])={\u00a0\r\n\u00a0\u00a0\u00a0 for(i &lt;-1 to 10 by 4){\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Element : \" +i)\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Element : 1<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">#Element : 5<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Element : 9<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])={\u00a0\r\n\u00a0\u00a0\u00a0 for(i &lt;- 1 to 10) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if(i%2!=0)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Odd Element :\" + (i))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0 }<\/pre>\n<p>&nbsp;<\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])={\u00a0\r\n\u00a0\u00a0\u00a0 for(i &lt;- 1 to 10) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if(i%2==0)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Even Element :\" + (i))\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0 }<\/pre>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><b><span data-contrast=\"auto\">Question: Can we use filter operation within for loop?<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])= {\u00a0\r\n\u00a0\u00a0\u00a0 val res= for(i &lt;-1 to 10) yield(i)\u00a0\r\n\u00a0\u00a0\u00a0 println(res)\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"auto\">Output \u201c<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">How to print this output in separate line?\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])= {\u00a0\r\n\u00a0\u00a0\u00a0 val res= for(i &lt;-1 to 10) yield(i)\u00a0\r\n\u00a0\u00a0\u00a0 for(j &lt;-res)\u00a0\r\n\u00a0\u00a0\u00a0 println(j)\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0 }<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"auto\">1<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">2<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">3<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">4<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">5<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">6<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">7<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">8<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">9<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">10<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">yield(i):<\/span><\/b><span data-contrast=\"auto\"> means println is not actually storing anything any output in any variable. println is just printing that output in the console.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Converting assembly language to binary language (0,1).<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args:Array[String])= {\u00a0\r\n\u00a0\u00a0\u00a0 val res= for(i &lt;-1 to 10) yield(i)\u00a0\r\n\u00a0\u00a0\u00a0 for(j &lt;-res){\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 if (j==3)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Third Element : \" +j)\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Third Element : 3<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">we can take collection elements List and iterate using for loop\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Marks {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val l = List(1,2,3,4,5)\u00a0\r\n\u00a0\u00a0\u00a0 for(x &lt;-l) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 println(x)\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output :<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">1<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">2<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">3<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">4<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">5<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">While loop: Iteration is unknown.<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">\u00a0while loop is use when we are unknown to iteration. We do not use how many times we have to iterate<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Num {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val num:Int = 10\u00a0\r\n\u00a0\u00a0\u00a0 var ind:Int = 0\u00a0\r\n\u00a0\u00a0\u00a0 while(ind&lt;num) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Element : \" +ind)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ind += 1\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0\r\n}<\/pre>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Process finished with exit code 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<h3><b><span data-contrast=\"auto\">Infinite while loop :<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"auto\">If we donot include boundary condition then the loop will\u00a0 go on the infinite .For example in the previous program ind += 1 it is a boundary condition\u00a0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Num {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val num: Int = 10\u00a0\r\n\u00a0\u00a0\u00a0 var ind: Int = 0\u00a0\r\n\u00a0\u00a0\u00a0 while (ind &lt; num) {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Element : \" + ind)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ ind += 2\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Element : 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><b><span data-contrast=\"auto\">Do while loop:\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Iter_Num {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val num:Int = 10\u00a0\r\n\u00a0\u00a0\u00a0 var ind:Int = 0\u00a0\r\n\u00a0\u00a0\u00a0 do {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 println(\"Element : \" +ind)\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ind += 2\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\u00a0\u00a0 while(ind&lt;num)\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"auto\">Element : 0<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">#Element : 2<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Element : 4<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">#Element : 6<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Element : 8<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<h4><b><span data-contrast=\"auto\">Pattern matching\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h4>\n<p><span data-contrast=\"auto\">in pattern mathing will be using case classes will be<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Pat_match {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\r\n\u00a0\u00a0\u00a0 val x: Int = 10\u00a0\r\n\u00a0\u00a0\u00a0 x match {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 case 10 =&gt; println(\"Found\")\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 case 11 =&gt; println(\"Not found\")\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 case _ =&gt; println(\"It's not int\")\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Found<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Pat_match {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\r\n\u00a0\u00a0\u00a0 val x: Int = 100\u00a0\r\n\u00a0\u00a0\u00a0 x match {\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 case 10 =&gt; println(\"Found\")\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 case 11 =&gt; println(\"Not found\")\u00a0\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 case _ =&gt; println(\"It's not int\")\u00a0\r\n\u00a0\u00a0\u00a0 }\u00a0\r\n\u00a0\r\n\u00a0 }\u00a0\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">It&#8217;s not int<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Pat_match {\u00a0\r\n\u00a0 def main(args: Array[String]) = {\u00a0\r\n\u00a0\u00a0\u00a0 val res = check(\"Hello\")\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0 def check(a:Any):Any = a match {\u00a0\r\n\u00a0\u00a0\u00a0 case \"Hello\" =&gt; println(\"String is found\")\u00a0\r\n\u00a0\u00a0\u00a0 case \"hello\" =&gt; println(\"String is not found\")\u00a0\r\n\u00a0\u00a0\u00a0 case _ =&gt; println(\"Data type def is missing\")\u00a0\r\n\u00a0 }\u00a0\r\n\u00a0 }<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:\u00a0\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"> SCALA &#8211; INHERITANCE AND LOOPS<\/span><\/p>\n<p><span data-contrast=\"auto\">String is found<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Pat_match {\r\n\u00a0 def main(args: Array[String]) = {\r\n\u00a0\u00a0\u00a0 val res = check(\"Hello\")\r\n\u00a0 }\r\n\u00a0 def check(a:Any) = a match {\r\n\u00a0\u00a0\u00a0 case \"Hello\" =&gt; println(\"String is found\")\r\n\u00a0\u00a0\u00a0 case \"hello\" =&gt; println(\"String is not found\")\r\n\u00a0\u00a0\u00a0 case _ =&gt; println(\"Data type def is missing\")\r\n\u00a0 }\r\n\u00a0 }<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\"> SCALA &#8211; INHERITANCE AND LOOPS<\/span><\/p>\n<p><span data-contrast=\"auto\">String is found<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Pat_match {\r\n\u00a0 def main(args: Array[String]) = {\r\n\u00a0\u00a0\u00a0 val res = check(\"HELLO\")\r\n\u00a0 }\r\n\u00a0 def check(a:Any) = a match {\r\n\u00a0\u00a0\u00a0 case \"Hello\" =&gt; println(\"String is found\")\r\n\u00a0\u00a0\u00a0 case \"hello\" =&gt; println(\"String is not found\")\r\n\u00a0\u00a0\u00a0 case _ =&gt; println(\"Data type def is missing\")\r\n\u00a0 }\r\n\u00a0 }<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Data type def is missing<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">object Pat_match {\r\n\u00a0 def main(args: Array[String]) = {\r\n\u00a0\u00a0\u00a0 val res = check(\"hello\")\r\n\u00a0 }\r\n\u00a0 def check(a:Any) = a match {\r\n\u00a0\u00a0\u00a0 case \"Hello\" =&gt; println(\"String is found\")\r\n\u00a0\u00a0\u00a0 case \"hello\" =&gt; println(\"String is not found\")\r\n\u00a0\u00a0\u00a0 case _ =&gt; println(\"Data type def is missing\")\r\n\u00a0 }\r\n\u00a0 }<\/pre>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">String is not found<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\"><a href=\"https:\/\/www.tableau.com\/\">Single<\/a> level <a href=\"https:\/\/prwatech.in\/blog\/\">inheritance<\/a>:\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Protected can provide access is for subclasses but other packages can not exist<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class Single_Lev {\r\n\u00a0 val prop:Double = 3000.00\r\n\u00a0 protected var are_house:Double = 2000.00\r\n\u00a0 def house () = {\r\n\u00a0\u00a0\u00a0 println(\"Villa is built over\" + are_house +\"sq ft.\")\r\n\u00a0 }\r\n}\r\nclass Child_A extends Single_Lev {\r\n\u00a0 def child_house() = {\r\n\u00a0 println(\"child house is built in area of\" + (prop - are_house) + \"sq ft.\")\r\n}\r\n}\r\nobject SCA {\r\n\u00a0 def main(args:Array[String]) ={\r\n\u00a0\u00a0\u00a0 val c = new Child_A()\r\n\u00a0\u00a0\u00a0 c.child_house()\r\n\u00a0\u00a0\u00a0 c.house()\r\n\u00a0\u00a0\u00a0 println(\"Single inheritance implemented\")\r\n\u00a0 }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b><span data-contrast=\"auto\">Output:<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">child house is built in area of1000.0sq ft.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Villa is built over2000.0sq ft.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Single inheritance implemented<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>SCALA &#8211; INHERITANCE AND LOOPS<\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335551550&quot;:6,&quot;335551620&quot;:6,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SCALA &#8211; INHERITANCE AND LOOPS &nbsp; Inheritance in Scala\u00a0\u00a0 Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in Scala by which one class is allowed to inherit the features (fields and methods) of another class.\u00a0 Important terminology:\u00a0 Super Class:\u202fThe class whose features are inherited is known as superclass (or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[560,1967,1968,576,1966],"class_list":["post-9695","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-a-scala-tutorial","tag-example-types-of-inheritance-in-scala-inheritance-in-scala-how-to-extend-a-class-in-scala","tag-inheritance-in-scala","tag-scala","tag-syntax"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SCALA - INHERITANCE AND LOOPS - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala - Inheritance and Loops - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SCALA - INHERITANCE AND LOOPS - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala - Inheritance and Loops - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-18T07:06:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-11T07:39:13+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/\",\"name\":\"SCALA - INHERITANCE AND LOOPS - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-01-18T07:06:07+00:00\",\"dateModified\":\"2024-04-11T07:39:13+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala - Inheritance and Loops - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SCALA &#8211; INHERITANCE AND LOOPS\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SCALA - INHERITANCE AND LOOPS - Prwatech","description":"Master Scala - Inheritance and Loops - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"SCALA - INHERITANCE AND LOOPS - Prwatech","og_description":"Master Scala - Inheritance and Loops - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-01-18T07:06:07+00:00","article_modified_time":"2024-04-11T07:39:13+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/","name":"SCALA - INHERITANCE AND LOOPS - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-01-18T07:06:07+00:00","dateModified":"2024-04-11T07:39:13+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala - Inheritance and Loops - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-inheritance-and-loops\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"SCALA &#8211; INHERITANCE AND LOOPS"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=9695"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9695\/revisions"}],"predecessor-version":[{"id":11346,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9695\/revisions\/11346"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}