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
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; }
That is the above code which I have been working on so far
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.
Thanks if anyone can help, Comm.Dan
In the debugger console, no code is outputted. It says that it ran without any issues.
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)
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...
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.
Thanks for the help @dape, -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
Join our real-time social learning platform and learn together with your friends!