Decision Making वाली संरचनाओं के लिए आवश्यक है कि Programmer एक या अधिक शर्तों को निर्दिष्ट करता है कि Programmer द्वारा Evaluation या परीक्षण किया जा सकता है साथ ही एक Statement या Description के साथ निष्पादित किया जाता है. यदि स्थिति True होने के लिए निर्धारित होती है और वैकल्पिक रूप से, अन्य Statement का निष्पादन Condition False होना निर्धारित है.
Swift अपने Programming के अंतर्गत निम्नलिखित प्रकार के Decision Making Statements प्रदान करता है.
If Statement
If….else Statement
Else if Statement
Nested if Statement
Switch Statement
सभी Programming भाषा में सबसे पहले और सबसे आम है अगर यह Users के अनुमति के माध्यम से कार्यक्रम के प्रवाह को नियंत्रित करता है. यह Conditional Statement केवल तभी काम करता है जब Condition True हो.
if boolean_expression {
/* statement(s) will execute if the boolean expression is true */
}
import Cocoa
var a1:Int = 60;
if a1 > 42 {
/* if the condition is true then print */
println("1st Varable is greater than 42");
}
println("The value of variable is \(a1)")
अगर यहां एक वैकल्पिक और Description के साथ अगर Statement का पालन किया जाता है और यह Description केवल Boolean Expression (i.e. the condition) false हो जाती है तो उसे केवल Execute किया जाता है.
if boolean_expression {
// statement(s) will execute if the boolean expression is true
} else {
// otherwise statement(s) will execute if the boolean expression is false
}
var g:Int = 1000;
if g < 60 {
println("Value is less than 60");
} else {
println("Value is not less than 60");
}
println("Value of variable is \(g)");
एक Statement अगर एक वैकल्पिक और else if…else Statement, जो एक से अधिक Conditions का परीक्षण करने के लिए बहुत ही उपयोगी है और इसे पीछा किया जा सकता है अगर if…else if Statement है.
if boolean_expression_1 {
} else if boolean_expression_2 {
.
} else if boolean_expression_n {
. . . . . .
} else {
}
import Cocoa
var varA:Int = 100;
/* Check the boolean condition using if statement */
if varA == 20 {
/* If condition is true then print the following */
println("varA is equal to than 20");
} else if varA == 50 {
/* If condition is true then print the following */
println("varA is equal to than 50");
} else {
/* If condition is false then print the following */
println("None of the values is matching");
}
println("Value of variable varA is \(varA)");
यह हमेशा Swift में Legal होता है यदि Nest if-else Statement जिसका मतलब है कि Programmers एक if or else if Statement कर सकते हैं यदि Statement अगर if or else if Statement के अंदर हो.
if boolean_expression_1 {
/* Executes when the boolean expression 1 is true */
if boolean_expression_2 {
/* Executes when the boolean expression 2 is true */
}
}
import Cocoa
var varA:Int = 200;
var varB:Int = 300;
/* Check the boolean condition using if statement */
if varA == 200 {
/* If condition is true then print the following */
println("First condition is satisfied");
if varB == 300 {
/* If condition is true then print the following */
println("Second condition is also satisfied");
}
}
println("Value of variable varA is \(varA)");
println("Value of variable varB is \(varB)");
Swift में एक Switch Statement अपने निष्पादन को पूरा करता है जैसे ही बाद के मामलों के नीचे से गिरने की जगह जैसे कि C और C ++ Programing भाषाओं में होता है, जैसे पहले मिलान केस पूरा हो जाता है। निम्नलिखित C और C ++ में switch Statement का एक सामान्य सिंटैक्स है .
switch expression {
case expression1 :
statement(s)
fallthrough // optional
case expression2, expression3 :
statement(s)
fallthrough //optional
default : // Optional
statement(s);
}
import Cocoa
var value = 20
switch index {
case 60 :
println( "Value of index is 60")
fallthrough
case 20,30 :
println( "the value is either 20 or 30")
fallthrough
case 40 :
println( "The value is 40")
default :
println( "Wrong Case Input")
}