Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Alex Atwater
Alex Atwater
2,624 Points

Really need help. If-else statements not running

This is driving me nuts. This is a “mocked up” version of a major issue i’m having in my actual project and is really frustrating me, any help would be so much appreciated. So you can copy this exact code into a blank command line app and see my issue:

NSMutableDictionary *loadGradeDictionary = [[NSMutableDictionary alloc] initWithCapacity:11];
        [loadGradeDictionary setObject:[NSArray arrayWithObjects:@"A", @"A-", @"B+", @"B", @"B-", @"C+", @"C", @"C-", @"D+", @"D", @"D-", @"F" , nil] forKey:@"gradesArray"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (92.5 + 0.5)] forKey:@"A"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (89.5 + 0.5)] forKey:@"A-"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (86.5 + 0.5)] forKey:@"B+"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (82.5 + 0.5)] forKey:@"B"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (79.5 + 0.5)] forKey:@"B-"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (76.5 + 0.5)] forKey:@"C+"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (72.5 + 0.5)] forKey:@"C"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (69.5 + 0.5)] forKey:@"C-"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (66.5 + 0.5)] forKey:@"D+"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (62.5 + 0.5)] forKey:@"D"];
        [loadGradeDictionary setObject:[NSString stringWithFormat:@"%.2f",  (59.5 + 0.5)] forKey:@"D-"];
        NSLog(@"\n%@", loadGradeDictionary);


        float passedGrade = 90;
        NSDictionary *gradeScale = loadGradeDictionary;
        NSMutableString *returnString = [[NSMutableString alloc] initWithCapacity:2];
        NSString *xString = [NSString stringWithFormat:@"%.2f", passedGrade];

        if (([xString floatValue] > [[gradeScale valueForKey:@"A"] floatValue])) {
            //return the letter here
            returnString = [@"A" mutableCopy];
        } else if (([[gradeScale valueForKey:@"A"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"A-"] floatValue])) {
            //return the letter here
            returnString = [@"A-" mutableCopy];
            //return @"A-";
        } else if (([[gradeScale valueForKey:@"A-"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"B+"] floatValue])) {
            //return the letter here
            returnString = [@"B+" mutableCopy];
            //return @"B+";
        } else if (([[gradeScale valueForKey:@"B+"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"B"] floatValue])) {
            //return the letter here
            returnString = [@"B" mutableCopy];
            //return @"B";
        } else if (([[gradeScale valueForKey:@"B"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"B-"] floatValue])) {
            //return the letter here
            returnString = [@"B-" mutableCopy];
            //return @"B-";
        } else if (([[gradeScale valueForKey:@"B-"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"C+"] floatValue])) {
            //return the letter here
            returnString = [@"C+" mutableCopy];
            //return @"C+";
        } else if (([[gradeScale valueForKey:@"C+"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"C"] floatValue])) {
            //return the letter here
            returnString = [@"C" mutableCopy];
            //return @"C";
        } else if (([[gradeScale valueForKey:@"C"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"C-"] floatValue])) {
            //return the letter here
            returnString = [@"C-" mutableCopy];
            //return @"C-";
        } else if (([[gradeScale valueForKey:@"C-"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"D+"] floatValue])) {
            //return the letter here
            returnString = [@"D+" mutableCopy];
            //return @"D+";
        } else if (([[gradeScale valueForKey:@"D+"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"D"] floatValue])) {
            //return the letter here
            returnString = [@"D" mutableCopy];
            //return @"D";
        } else if (([[gradeScale valueForKey:@"D"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"D-"] floatValue])) {
            //return the letter here
            returnString = [@"D-" mutableCopy];
            //return @"D-";
        } else if ([[gradeScale valueForKey:@"D-"] floatValue] > passedGrade) {
            //return the letter here
            returnString = [@"F" mutableCopy];
            //return @"F";
        }

        NSLog(@"\nreturnString: %@", returnString);

i get a blank string at the end of it, and when you debug it and it goes to evaluate the if/else statements, it like “crashes” and blanks out, then comes back and just skipped evaluation of the whole thing. So weird and frustrating. Any help would be great!

1 Answer

Ravi Shankar
PLUS
Ravi Shankar
Courses Plus Student 7,823 Points

I don't see any problem with this program, since none of condition matches the program execution reaches the NSLog line. Try changing the condition and you will see the the correct grade getting printed. For example if you change the first if else statement

else if (([[gradeScale valueForKey:@"A"] floatValue] > passedGrade) && (passedGrade > [[gradeScale valueForKey:@"A-"] floatValue])) { //return the letter here returnString = [@"A-" mutableCopy]; //return @"A-"; }

to

else if (([[gradeScale valueForKey:@"A"] floatValue] > passedGrade) && (passedGrade >= [[gradeScale valueForKey:@"A-"] floatValue])) { //return the letter here returnString = [@"A-" mutableCopy]; //return @"A-"; } you will notice the return string as -A. (the condition changed from > to >=).

Hope this helps.

Regards, Ravi