check var in between - irc

if (%var == 1 or 2 or 3 or 4 or 5) { }
So what u want is to check whether the variable's value is in between 1-5 in mirc?
I can do it like this:
if (%var == 1) { }
if (%var == 2) { }
if (%var == 3) { }
if (%var == 4) { }
if (%var == 5) { }
But is there any shorter way?

The idiomatic answer is:
if (%var isnum 1-5) {
}

Related

Flutter how to create a loop in else if

i got a chart that display months (in french) when i go below the month == 1, i would like to go back to my current month and not see my 'error'.
Is there a method to make it ?
String numericToStringMonth(int month) {
if (month < 1 || month > 12) {
return 'error';
} else if (month == 1) {
return 'Janvier';
} else if (month == 2) {
return 'Février';
} else if (month == 3) {
return 'Mars';
} else if (month == 4) {
return 'Avril';
} else if (month == 5) {
return 'Mai';
} else if (month == 6) {
return 'Juin';
} else if (month == 7) {
return 'Juillet';
} else if (month == 8) {
return 'Août';
} else if (month == 9) {
return 'Septembre';
} else if (month == 10) {
return 'Octobre';
} else if (month == 11) {
return 'Novembre';
} else if (month == 12) {
return 'Décembre';
} else {
return 'error';
}
}
String numericToStringMonth(int month) {
// make list of months for easier access
List<String> months = [
'Janvier',
'Février',
'Mars',
'Avril',
'Mai',
'Juin',
'Juillet',
'Août',
'Septembre',
'Octobre',
'Novembre',
'Décembre'
];
int currentMonth = month - 1;
try {
if (currentMonth > 11) {
throw Exception('Month must be between 1 and 12');
} else if (currentMonth < 0) {
// setting month to the first one when value goes below 0, add your logic here
currentMonth = 1;
}
return months[currentMonth];
} catch (e) {
print(e);
}
return '';
}

Is there any way to simplify complex if else statement in swift?

I have a long if else condition to assign a text to a UILabel. So i am looking for a better option/logic to write less number of lines.
Below is my condition,
if numberOfTeachers == 4 && numberOfStudents == 27 {
if String(interval.day! + 1) == "1" {
self.daysLabel.text = "1st"
self.testLabel.isHidden = true
self.subjectLabel.text = "Physics"
self.dayTitleLabel.text = "Today is your first day"
} else if String(interval.day! + 1) == "2" {
self.daysLabel.text = "2nd"
self.testLabel.isHidden = true
self.subjectLabel.text = "Chemistry"
self.dayTitleLabel.text = "Today is your Second day"
} else if String(interval.day! + 1) == "3" {
self.daysLabel.text = "3rd"
self.subjectLabel.isHidden = true
self.unitLabel.text = "Mathematics"
self.dayTitleLabel.text = "Today is your Third day"}
else if String(interval.day! + 1) == "4" {
self.daysLabel.text = "4th"
self.testLabel.isHidden = false
self.subjectLabel.text = "Physics"
self.dayTitleLabel.text = "Today is your fourth day" }
else if numberOfTeachers == 4 && numberOfStudents == 28 {
} else if numberOfTeachers == 4 && numberOfStudents == 29 {
} else if numberOfTeachers == 4 && numberOfStudents == 30 {
} else if numberOfTeachers == 5 && numberOfStudents == 27 {
} else if numberOfTeachers == 5 && numberOfStudents == 28 {
} else if numberOfTeachers == 5 && numberOfStudents == 29 {
} else if numberOfTeachers == 5 && numberOfStudents == 30 {
} else if numberOfTeachers == 6 && numberOfStudents == 27 {
} else if numberOfTeachers == 6 && numberOfStudents == 28 {
} else if numberOfTeachers == 6 && numberOfStudents == 29 {
} else if numberOfTeachers == 6 && numberOfStudents == 30 {
} else if numberOfTeachers == 7 && numberOfStudents == 27 {
} else if numberOfTeachers == 7 && numberOfStudents == 28 {
} else if numberOfTeachers == 7 && numberOfStudents == 29 {
} else if numberOfTeachers == 7 && numberOfStudents == 30 {
}
there are atleast 4 more conditions inside every if condition like (4,27), (4,28) , (5,27), (5,28)....so on.
So this whole condition is getting too long.
You could nest the if conditions like:
if numberOfTeachers == 4
{
if numberOfStudents == 29 {}
else if ...
}
else if ...
You could also use a switch statement which is a great alternative for long chains of if-else:
switch numberOfTeachers
{
case value 4:
// handle number of students
case value 5:
...
default:
// error?
}
Edit as Martin R noted, one could also use:
switch (numberOfTeachers, numberOfStudents)
{
case (4, 27):
// handle number of students
case (5, 28):
...
default:
// error?
}

Return list with the values on odd positions

Why does the following code return a empty list instead of a list with the values at odd positions ?
def f(arr:List[Int]) : List[Int] = {
def odd_concat(list_odd:List[Int], arr_index:Int) : List[Int] = {
if(arr_index == arr.size) {
list_odd
}
else if(arr_index % 2 == 0) {
odd_concat(list_odd, arr_index + 1)
}
else {
//println(arr(arr_index))
list_odd:+arr(arr_index)
odd_concat(list_odd, arr_index + 1)
}
}
odd_concat(List(), 0)
}
You are using immutable list, immutable means the object cannot be change.
Your code:
list_odd:+arr(arr_index)
It does not change the list_odd with the value of arr(arr_index) rather give a new instance of List with values added.
Try to insert that code inside the odd_concat() instead, like the following:
def f(arr:List[Int]) : List[Int] = {
def odd_concat(list_odd:List[Int], arr_index:Int) : List[Int] = {
if(arr_index == arr.size) {
list_odd
}
else if(arr_index % 2 == 0) {
odd_concat(list_odd, arr_index + 1)
}
else {
//println(arr(arr_index))
odd_concat(list_odd:+arr(arr_index), arr_index + 1)
}
}
odd_concat(List(), 0)
}

swift 1.2 if loop to switch statement

I have the following If-Statment and I was wondering how this could be achieved with a switch statement?
I am trying to represent an integer value within an array as a string (e.g. 1 == "Jan")
func assigningMonthName([Data]) {
for i in dataset.arrayOfDataStructures {
if (i.month) == 1 {
println("Jan")
}
else if (i.month) == 2 {
print("Feb")
}
else if (i.month) == 3 {
print("March")
}
else if (i.month) == 4 {
print("April")
}
else if (i.month) == 5 {
print("May")
}
else if (i.month) == 6 {
print("June")
}
else if (i.month) == 7 {
print("July")
}
else if (i.month) == 8 {
print("August")
}
else if (i.month) == 9 {
print("September")
}
else if (i.month) == 10 {
print("October")
}
else if (i.month) == 11 {
print("November")
}
else if (i.month) == 12 {
print("December")
}
else {
println("Error assigning month name")
}
}
}
any answers would be appreciated :)
While you can use a switch, this is essentially just another way to write if-else so there is no big improvement to your code:
switch i.month {
case 1:
print("Jan")
case 2:
print("Feb")
...
}
What about using an array?
let monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "Sept", "October", "November", "December"]
print(monthNames[i.month - 1])
The system actually already contains month names, they are even localized:
let monthNames = NSDateFormatter().monthSymbols;
print(monthNames[i.month - 1])
Try this:
switch i.month {
case 1:
print("Jan")
case 2:
print("Feb")
...
default:
print("default value")
}

Compacting the code: same action for any element of a pair

Is there any way to make code more compact for this situation?
// pseudocode
if (A == 1) {
if (B == 2) {
action1;
}
if (B == 3) {
action2;
}
}
if (B == 1) {
if (A == 2) {
action1;
}
if (A == 3) {
action2;
}
}
in Objective-C/Objective-C++?
One more example:
if (((int)fixtureUserDataA == FIXTURE_FOOT_SENSOR || ((int)fixtureUserDataB == FIXTURE_FOOT_SENSOR ))) {
// Hero foot sensors
if (bodyA->GetUserData() != NULL) {
CCSprite * sprite = (CCSprite*)bodyA->GetUserData();
if (sprite.tag == TAG_HERO) {
[Hero sharedInstance].numFootContacts++;
}
}
if (bodyB->GetUserData() != NULL) {
CCSprite * sprite = (CCSprite*)bodyB->GetUserData();
if (sprite.tag == TAG_HERO) {
[Hero sharedInstance].numFootContacts++;
}
}
}
Is it possible to use only one if or more clear construction?
for 2nd example:
checkBody(bodyA);
checkBody(bodyB);
void checkBody(Body* body)
{
CCSprite * sprite = (CCSprite*)body->GetUserData();
if (sprite.tag == TAG_HERO) {
[Hero sharedInstance].numFootContacts++;
}
}
if (A == 1 || B == 1)
if (A + B == 3)
action1;
else if (A + B == 4)
action2;