Condition (if, if..else, if..else_if…else, switch-case)

As the name suggest conditions are used to verify the particular thing before executing the program or code. When you will check the condition there might be one possibility true or false.
IF Condition : Do the task if condition is true otherwise nothing.
Lets say we want one method which has one parameter of type string. This method print the Holiday if value coming in parameter is Sunday otherwise do not print anything.
Program :
-(void)CheckForHoliday:(NSString *)strDayName
{
if ([strDayName isEqualToString:@”Sunday”]) {
NSLog(@”Holiday”);
}
}
ELSE IF : Do the task if condition is true otherwise do something else.
Lets say we want one method which has one parameter of type string. This method print the Holiday if value coming in parameter is Sunday otherwise print Not a Holiday.
Program :
-(void)CheckForHoliday:(NSString *)strDayName
{
if ([strDayName isEqualToString:@”Sunday”]) {
NSLog(@”Holiday”);
}
}
else
{
NSLog(@”Not a Holiday”);
}
IF ELSE_IF ELSE : To Check more than one condition. Means Do the task if first condition is true otherwise , check the second condition if this is true then do the second task otherwise do something else.
- You can check many conditions means you can write many else_if condition.
Lets say we want one method which has one parameter of type string. This method print the Full Day Holiday if value coming in parameter is Sunday , print the Half Day Holiday if value coming in parameter is Saturday, otherwise print Not a Holiday.
Program :
-(void)CheckForHoliday:(NSString *)strDayName
{
if ([strDayName isEqualToString:@”Sunday”]) {
NSLog(@”Full Day Holiday”);
}
else if ([strDayName isEqualToString:@”Saturday”]) {
NSLog(@”Half Day Holiday”);
}
else
{
NSLog(@”Not a Holiday”);
}
}
NOTE :
- Using any of above if condition, you can perform the check on any datatype. on above holiday example we perform the check on NSString Datatype, We can also perform the check on other datatype. For example check below method which print the high or low bases on value passed in parameter.
- If statement keep checking the all the condition from top to bottom until one condition get satisfied. For example If we pass Monday in above method then it will check first if condition , then else if condition then it will go to else because not any condition satisfied.
-(void)CheckForNumber:(int)number
{
if (number>10) {
NSLog(@”High”);
}
else
{
NSLog(@”Low”);
}
}
SWITCH-CASE : Its also used to satisfy more than one condition. Switch is used to check the condition on int/NSInteger Datatype only. Advantage of switch-case is that it does not check condition top to bottom one by one instead its directly jump to satisfied matching case.
Check below method which is useful to check the priority of number. When we will call the method and pass the 5 value in number parameter then switch will directly go to case 5 and will print the Middle. It will not check case 10 even its above case 5. Default is executed if not any case match.
-(void)CheckForNumberPriority:(int)number
{
switch (number) {
case 10:
NSLog(@”High”);
break;
case 5:
NSLog(@”Middle”);
break;
case 1:
NSLog(@”Low”);
break;
default:
NSLog(@“Not in any priority”);
break;
}
}
Hope you got the idea to check the conditions using if else , if else ladder and switch case.
Thank you for reading :)
Please checkout my youtube channel for video tutorials and subscribe it , share it with your friend.
youtube channel link : https://www.youtube.com/channel/UCWjT8XgKescscxlPbMSVzbQ