Kotlin - Control Statements |Part - 3|

Control Statements Part-3

In Kotlin Programming language, the Control Statements are similar as in most of the programing language.
Control Statements are the conditional statements that let us create a set of options to make a decision to produce the choice of output if a different type of input supplied to the program.




The Conditional Statements are 
  • if Statement
  • if-else Statement
  • if-else as expression
  • when Statement

if Statement: 

In an, if Statement, program checks for the Condition to match with the given option if matches then perform the activity following it

Syntax

if(<condition>)

{
    <Set of activity>
}

Example 1:

val name: String? = "James"
val lname = "McDonald"
var newVar: String? = null 
 
fun main()
{
if (name == "James")newVar = "Yes the name is " + name 
println(newVar)
}

Example 2:

var newVar: String? = null
var y = 20 
var x = 10
 
fun main() {
if (x > y) newVar = "x is greater number " + x
if (x < y) newVar = "Y is greater number " + y
println(newVar)
}


if-else Statement:

In an if-else statement, program checks for the Condition to match with the given option if matches then perform the activity follows it, if not matches the Condition, then conducts the other event.  

The syntax used for if the Statement is as below: 

if(<condition>) <Set of activity>
else <set of activity>

Example 1:

var newVar: String? = null
var y = 20 
var x = 10
 
fun main()
{
if (x > y) newVar = "x is greater number " + y
else newVar = "Y is greater number " + y
println(newVar)
}

Example2:

val name: String? = "James"
val lname = "McDonald"
var newVar: String? = null
fun main()
{
    if (name == "James")newVar = "Yes it is " + name
    else newVar == "No it is the Something else"

println(newVar)
}

if-else Statement as an expression:

In an if-else statement, it is just like the usual if-else Statement, but in this one, we use curly braces to supply multiple expressions instead of a single line of expression.

Syntax:

if(<condition>)
{
    <Set of activity>
}
else{
    <Set of activity>
}

Example 1:

val name: String? = "James" 
val lname = "McDonald"
var newVar: String? = null
fun main() { if (name == "James") { newVar = "Yes it is " + nameprintln(newVar) } else { newVar == "No it is the Something else"println(newVar) } }

Example 2:

var newVar: String? = null
var y = 20 
var x = 10
fun main() {
 
if (x > y)
{
newVar = "x is greater number " + y 
println(newVar)
}
 
else
{
newVar = "Y is greater number " + y 
println(newVar)
}
}

when Statement:

In a when Statement, program checks for the Condition to match with the multiple options to make a choice, when it matches one of the options, then it performs the activity following the decision.  

Syntax:

when(<variable>)
{
    <variable condition> -> <output>
    <variable condition> -> <output>
    <variable condition> -> <output>
    <variable condition> -> <output>
    else -> <output>
}

Example:

fun main() {
    when(x)
    {
        1 -> println("You selected  1")
        2 -> println("You selected  2")
        3 -> println("You selected  3")
        4 -> println("You selected  4")
        else -> println("Invalid range outside 1 to 4")
    }
}

1 comment:

  1. Across the globe blogs are supplementing the established news media. They have proved to be so powerful a medium that their influence is fast changing the face of journalism. Top Engineering Colleges/ Universities in India Bloggers have greatly rejuvenated and renewed the entire world of journalism, and in some places blogging is journalism!

    ReplyDelete

Enter your email address:

Delivered by FeedBurner