![]() |
VOOZH | about |
Decision-making statements are those statements in which the compiler needs to make decisions on the given conditions. If the given condition is satisfied then the set of statements is executed otherwise some other set of statements are executed. These statements always return a boolean value that is either true or false. If true then the if block is executed. Otherwise else (or) else if block is executed. Swift supports five different decision-making statements and they are :
In Swift, a simple if works based on condition. If the given condition is satisfied, then the statements inside the if block is executed. If the given condition is false then the control return from the if statement and executes the statement present after the if statement block.
Syntax:
if condition
{
// statements
}
Flow diagram:
👁 ImageExample:
Output:
Welcome to GeeksforGeeks
In Swift, if - else statement is just like other programming languages. If the condition is satisfied or true then the statements in the if block are executed. If the condition is not satisfied then the statements in the else block are executed.
Syntax:
if condition
{
// Statements
}
else
{
// Statements
}
Flow diagram:
👁 ImageExample:
Output:
b is the largest number
Generally else doesn't have any condition checking part so when if condition fails then else part will be executed automatically. But using else if condition we can write a condition in else part also. We can write n number of else if conditions. So, it is called as if-else if ladder.
Syntax:
if condition
{
// Statements
}
else if condition
{
// Statements
}
....
else
{
// Statements
}
Flow diagram:
👁 ImageExample:
Output:
b is biggest number
Nested means within. Nested if means if within if condition. Generally in if-else, if condition satisfied then the statements under if are executed. But in nested if, again condition is checked. Like that in single if condition we can write many if conditions. The statements are executed only after all the if conditions are satisfied. Otherwise else condition statements are executed.
Syntax:
if condition
{
if condition
{
if condition
{
......
//statements
}
}
}
else
{
//statements
}
Flow diagram:
👁 ImageExample:
Output:
geeks
A switch is also like if - else if ladder we can write n number of else if conditions. Likewise in the switch statement also we can write n number of cases. if-else if ladder works automatically based on conditions. But the switch statement works like a switch. Based on the choice the desired case is executed. For example, we have chosen 2, then case 2 will be executed. And there is a default case, if all the cases are failed then the default case is executed. It is not mandatory to write a default case, it's an optional case. And we write a break statement to stop controls to fall through the next subsequent cases.
Syntax:
switch(expression)
{
case constant-expression :
statement(s);
break;
case constant-expression :
statement(s);
break;
default : optional
statement(s);
}
Example:
Output:
Example of switch statement using break