Sorry for kind of stupid question.
I use an UITextField, where I can enter some numbers.
No i use this code to detect if the entered number is 0, greater than 0, and less than 15.
if (myNr <= 15){
NSLog (#"the number is OK");
}
else if (myNr > 15)
{
NSLog(#"this number doesn't existing");
}
else if (myNr == 0)
{
NSLog(#"number should be between 1 and 15");
}
I got some errors when the number is 0.
i Need to be able to insert numbers only between 1 and 15, if the number is 0 or greater then 15, NSLog should say.
thanks
you should put if(myNr == 0) at first
if (myNr == 0)
{
NSLog(#"number should be between 1 and 15");
} else if (myNr <= 15)
{
NSLog (#"the number is OK");
}
else if (myNr > 15)
{
NSLog(#"this number doesn't existing");
}
What error do you get?
Offhand, remember: Capitalization matters. NSLog is different than nslog.
Your code reduces to:
if (myNr <= 15)
{
NSLog(#"the number is OK");
}
else
{
NSLog(#"this number doesn't existing");
}
The case for 0 is never reached.
Remember the tests are considered sequentially.
Related
How to remove decimal if value is equal to 10 else decimal value should not removed.
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
if (_ratingValue >= 100) {
_ratingValue = 10;
_formatValue = #"%0.0f";
}
else{
_formatValue = #"%.01f";
}
}
_ratingValue == 0.0f ? [cellProductInfo.view_ProductRating setInnerText:#"review"] :
[cellProductInfo.view_ProductRating setInnerText:[NSString stringWithFormat:#" %.01f / 10",_ratingValue]];
If ratingValue is equal 10, I do not want to show the decimal value else if below 10 want to show decimal value.
For 1 decimal place your format needs to be %.1f. For no decimals you want %.0f.
If you want values less than 10 to show 1 decimal and values greater or equal to 10 to show none, you want:
if (_ratingValue >= 10.0) {
_formatValue = #"%.0f";
} else {
_formatValue = #"%.1f";
}
_ratingValue == 0.0f ? [cellProductInfo.view_ProductRating setInnerText:#"review"] :
[cellProductInfo.view_ProductRating setInnerText:[NSString stringWithFormat:_formatValue,_ratingValue]];
Its works for me!
I need to check in two of string, if those string contains any particular string or not
NSString *startLocationAddress = #"Any address for start location";
NSString *endLocationAddress = #"Any address for end location";
if ([startLocationAddress rangeOfString:#"Australia"].location == NSNotFound)
{
NSLog(#"string does not contain Australia");
startLocationAddress = [startLocationAddress stringByAppendingString:#",Australia"];
}
else if ([endLocationAddress rangeOfString:#"Australia"].location == NSNotFound)
{
NSLog(#"string does not contain Australia");
endLocationAddress =[endLocationAddress stringByAppendingString:#",Australia"];
}
else {
NSLog(#"string contain Australia");
}
As my both of strings does not contains "Australia". So the first condition will be checked first and if the first condition valid then it exit out of the conditions, if the first condition is not valid then only it check the else if condition. In this way the if else if conditional works.
As my both of strings does not contains "Australia". First if condition is working fine and it append "Australia" to the string but second else if condition is not working
How on earth can an else if block execute when it's corresponding if was executed? Expectation is not logical.
If you want your else if block to also check then separate it from your main if and start a new if condition, not an else if
You better reconstruct it like:
if ([startLocationAddress rangeOfString:#"Australia"].location == NSNotFound)
{
//codes..
}
else
{
NSLog(#"startLocationAddress contain Australia");
}
if ([endLocationAddress rangeOfString:#"Australia"].location == NSNotFound)
{
//codes..
}
else
{
NSLog(#"endLocationAddress contain Australia");
}
and review how if-else if-else statement works.. See: #
Hanky 웃 Panky answer for that, since confusion is very prone to us..
if (ifcondition)
{ .. }
else if (elseifcondition)
{ .. }
else
{ .. }
/*
if `ifcondition` == true, dont expect anything from `else-if` or `else`,
compiler won't care about them anymore. that goes with `else-if` and `else` as well..
*/
In if-else if-else condition if any of one is gets satisfied it will ignore the rest of the condition.
This means if First if gets satisfied, then it will ignore the "else if - else". Hence your second condition is not executing.
You are required to read the basic if else logic of execution.
Here is the example
int x=70;
string Grade;
IF (x > 90) THEN
Grade = "O"
ELSE IF (x > 80) THEN
Grade = "A"
ELSE IF (x > 70) THEN
Grade = "B"
ELSE IF (x > 60) THEN
Grade = "C"
ELSE
Grade = "F"
Here the value for variable Grade will be only one per execution.
I know there are a couple of questions like this on stack overflow but the problem is that it does not solve my answer.
I want to show an if-statement inside a switch case.
Here is my code
NSMutableArray *stdMarks = [[NSMutableArray alloc]initWithObjects:#"100", #"70", #"97", #"11", #"59", nil];
int marksObtained;
for (int i= 0; i < [stdMarks count]; i++) {
marksObtained += [[stdMarks objectAtIndex:i] integerValue];
}
int totalMarks = 500;
int percentage = (marksObtained * 100)/totalMarks;
NSLog(#"The total marks are: %d", marksObtained);
NSLog(#"The percentage is: %d", percentage);
NSLog(#"The total numbers of subjects are: %d", [stdMarks count]);
switch (percentage) {
case 1:
if (percentage >= 70) {
NSLog(#"You get 50 percent fee concession");
}
break;
case 2:
if (percentage >= 60) {
NSLog(#"You get 45 percent fee concession");
}
break;
default:
NSLog(#"you do not qualify");
break;
}
No matter what the percentage is I always get the default answer "You do not qualify".
What am I doing wrong
Please help me out here
that is because the switch is being fed the percentage which only caters for 1 and 2 percent, so those if statements will never fire because to get into those cases, they have to be lower then the values in the if statements
my suggestion is to drop the switch statement, because it seems unnecessary, just have if/else statements that encompass the ranges you want
In your code, if your percentage is not equal to run 1 or 2, it will go to default.
switch (percentage) { <---- this percentage, you only provide the case of 1 and 2.
case 1:
if (percentage >= 70) {
NSLog(#"You get 50 percent fee concession");
}
break;
case 2:
if (percentage >= 60) {
NSLog(#"You get 45 percent fee concession");
}
break;
default:
NSLog(#"you do not qualify");
break;
}
guessing you need to change the percentage in switch(percentage).
Updated:
if (percentage >= 70) {
NSLog(#"You get 50 percent fee concession");
}
else if (percentage >= 60) {
NSLog(#"You get 45 percent fee concession");
}
else{
NSLog(#"you do not qualify");
}
update:
if you want to do it using switch case:
for percentage >= 70
switch (percentage)
case 70:
case 71:
case 72:
case 73:
.
.
.
case 100:
NSLog(#"You get 50 percent fee concession");
break;
case 60:
.
.
.
and so on which is very stupid , so a if else case is more suitable
I have a section of code, in which I want a different accessoryView for the TableView cell, based off the number for that cell's entry. The code I have set up is:
NSInteger warriors = [entry.prayerWarriors intValue];
if (warriors == 0) {
//Do nothing
//NSLog(#"0");
}
else if (0 < warriors < 50) {
cell.accessoryView = firstLevel;
// NSLog(#"50");
}
else if (51 < warriors < 100) {
cell.accessoryView = secondLevel;
// NSLog(#"100");
}
else if (101 < warriors < 500) {
cell.accessoryView = thirdLevel;
// NSLog(#"500");
}
else {
cell.accessoryView = fourthLevel;
// NSLog(#"A Lot");
}
However, it always returns only the first entry for warriors == 0. What am I doing wrong?
Rather than doing this...
else if (0 < warriors < 50) {
cell.accessoryView = firstLevel;
// NSLog(#"50");
}
do this...
else if (0 < warriors && warriors < 50) {
cell.accessoryView = firstLevel;
// NSLog(#"50");
}
EDIT
To answer your comment...you probably mean to have some <= or >= in there, as it's going to the last else when warriors equals the border of your if conditionals (50, 100 or 500).
You probably want it to look like this...
NSInteger warriors = [entry.prayerWarriors intValue];
if (warriors == 0) {
//Do nothing
//NSLog(#"0");
}
else if (0 < warriors && warriors <= 50) {
cell.accessoryView = firstLevel;
// NSLog(#"50");
}
else if (50 < warriors && warriors <= 100) {
cell.accessoryView = secondLevel;
// NSLog(#"100");
}
else if (100 < warriors && warriors <= 500) {
cell.accessoryView = thirdLevel;
// NSLog(#"500");
}
else {
cell.accessoryView = fourthLevel;
// NSLog(#"A Lot");
}
The statement
if (0 < warriors < 50)
evaluates different than you might think. The first part
(0 < warriors)
evaluates as a boolean, and that boolean will be compared to 50.
So, you need to do: if (0 < warriors && warriors < 50)
The other answers make good points about the later cases needing a logical and but if you are getting stuck at if (warriors == 0) { most likely your object entry.prayerWarriors is nil. Put a break point and print it out. (and print out it's class to make sure its as expected)
Also minor but a good habit to be in your conversion of what I'm guessing is an NSNumber isn't using the same type as your variable. Since you are writing into a NSInteger you should replace intValue with integerValue
For clarity, I prefer to put the variable first in each condition and to encapsulate each part of the condition:
if (warriors == 0) {
//Do nothing
NSLog(#"0");
}
else if ((warriors > 0) && (warriors < 50))
{
cell.accessoryView = firstLevel;
NSLog(#"50");
}
else if ((warriors > 51) && (warriors < 100)) {
cell.accessoryView = secondLevel;
NSLog(#"100");
}
else if ((warriors > 101) && (warriors < 500)) {
cell.accessoryView = thirdLevel;
NSLog(#"500");
}
else {
cell.accessoryView = fourthLevel;
NSLog(#"A Lot");
}
Just be sure you've have enough parens around the conditions.
You do not need an if cascade for this:
You can store the levels one time anywhere
NSArray *levels = #[firstLevel, secondLevel, thirdLevel, fourthLevel];
And use it indexed:
if( warriors > 0) {
cell.accessoryView = levels[(warriors-1) / 50]
}
But if you want to have an if cascade, you do not have to double check:
NSInteger warriors = [entry.prayerWarriors intValue];
if (warriors == 0) {
//Do nothing
//NSLog(#"0");
}
else if (warriors <= 50) {
cell.accessoryView = firstLevel;
// NSLog(#"50");
}
else if (warriors <= 100) {
cell.accessoryView = secondLevel;
// NSLog(#"100");
}
else if (warriors <= 500) {
cell.accessoryView = thirdLevel;
// NSLog(#"500");
}
else {
cell.accessoryView = fourthLevel;
// NSLog(#"A Lot");
}
If you are in an else, it is already tested that the preceding condition failed.(That's the meaning of else.)
if(![myNumberFormatter numberFromString:[tempColumn objectAtIndex:j]])
{
numericalColumns[j] = NO;
if(j == 8)
{
NSLog(#" non numerical value in column 8 i = %d object = %# ", i , [tempColumn objectAtIndex:j]);
}
}
What I find:
0 good
-19.49883745 good
+38.85928608 bad
+46.94000154 bad
-0.36042119 good
+38.30408636 bad
-44.29029741 good
+26.91823821 bad
-79.06183133 good
-16.69693020 good
Try doing this:
[myNumberFormatter setPositivePrefix:#"+"];
before calling numberForString