Ask your own question, for FREE!
Computer Science 11 Online
OpenStudy (comm.dan):

I need help with how to do if else if statements in C I am doing this in Xcode 4.6.3, while learning how to program in C The code that I have done so far and the directions that I am following using to follow this part of the assignment are going to be in the next post that I am dong I will give a medal to anyone who helps me, and if I think that there answer helps me

OpenStudy (comm.dan):

Code: #include <stdio.h> int main(){ int age = 37; if(age < 18-29) { age = 0-18; } else { age = 19-29; } if(age < 30-49) { age = 30-39; } else { age = 40-49; } if(age < 50-90) { age = 50-59; } else { age = 60-90; } return 0; }

OpenStudy (comm.dan):

That is the above code which I have been working on so far

OpenStudy (comm.dan):

Here are the directions that I am using to do this part of the assignment: 2. Create a int variable called age and set the value to 37. Using an if...else...if statement series printf() the following message based on the value of age: 0-18 You’re just a youngster! 19-29 Have fun now, while you can! 30-39 Time to focus on family and career 40-49 These are the best years of your life! 50-59 Better be saving for retirement. 60+ Enjoy retirement. I hope you saved enough money 3. Compile and test your code and insure you get the expected result. Change the value of the age variable and retest your program.

OpenStudy (comm.dan):

Thanks if anyone can help, Comm.Dan

OpenStudy (comm.dan):

In the debugger console, no code is outputted. It says that it ran without any issues.

OpenStudy (dape):

You need to have a printf statement to see any output. Also when you write if (age < 18-29) it is interpreted by the compiler as if (age < -11) so you just want if (age < 19)

OpenStudy (dape):

Another very useful tip is that you don't need multiple if-statements in this case, you can chain them like this: if (age < 19) { printf("You’re just a youngster!"); } else if (age < 30) { ... } else if (age < 40) { ... } etc...

OpenStudy (dape):

In this the program will compare the age variable with 19, if it's less, it will print the thing and jump out of the "if-chain". Otherwise it will compare age with 30, etc.

OpenStudy (comm.dan):

Thanks for the help @dape, -Comm.Dan

OpenStudy (comm.dan):

It worked @dape. I really appreciate the help, the next time I have any questions about programming in C, I know that I can turn to you. Thanks again so much, -Comm.Dan

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!