Post: I need some help with nested if statments
05-06-2017, 01:58 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); im wondering why this code wont tell me "your starting to get old"...

    #include <iostream>
using namespace std;

int main(){

int age=36;
if(age>=1Cool Man (aka Tustin){
if(age<=17){
if(age>=30){
if(age>=40){
cout<< " you old af\n";
}
cout<< "your starting to get old\n";
}
cout<< "your hella young bro\n";
}
cout<< "your grown go get a job\n";
}

return 0;}
Last edited by mudville209 ; 05-06-2017 at 02:15 PM.
05-06-2017, 02:04 PM #2
i dont know why that emoji appears but its supposed to say if(age>=18 ){
05-06-2017, 04:09 PM #3
Dro
i<3myPs3
Originally posted by mudville209 View Post
im wondering why this code wont tell me "your starting to get old"...

    #include <iostream>
using namespace std;

int main(){

int age=36;
if(age>=1Cool Man (aka Tustin){
if(age<=17){
if(age>=30){
if(age>=40){
cout<< " you old af\n";
}
cout<< "your starting to get old\n";
}
cout<< "your hella young bro\n";
}
cout<< "your grown go get a job\n";
}

return 0;}



I dont code c++ but i think you want it like this:

#include <iostream>
using namespace std;

int main(){
int age=36;
if(age>=1Cool Man (aka Tustin)
{
cout<< "your grown go get a job\n";
}
if(age<=17)
{
cout<< "your hella young bro\n";
}
if(age>=30)
{
cout<< "your starting to get old\n";
}
if(age>=40)
{
cout<< " you old af\n";
}

return 0;}

sorry if I'm wrong, just tryin to help.
05-06-2017, 04:25 PM #4
that would work but thats not a nested if statement thats just an if statement...but thx for trying to help.

The following user thanked mudville209 for this useful post:

Dro
05-06-2017, 08:24 PM #5
Tustin
Balls of Steel
Originally posted by mudville209 View Post
im wondering why this code wont tell me "your starting to get old"...

    #include <iostream>
using namespace std;

int main(){

int age=36;
if(age>=1Cool Man (aka Tustin){
if(age<=17){
if(age>=30){
if(age>=40){
cout<< " you old af\n";
}
cout<< "your starting to get old\n";
}
cout<< "your hella young bro\n";
}
cout<< "your grown go get a job\n";
}

return 0;}


Your logic is flawed in the second if statement. You check if the person's age is 18 or greater, and if it is, your next if statement checks if they're 17 or younger. That check will never be successful. You probably want something like this:
    
#include <iostream>
using namespace std;

int main()
{
int age=18;

if(age>=17)
{
if(age != 1Cool Man (aka Tustin)
{
if(age>=30)
{
if(age>=40)
{
cout<< " you old af\n";
}
cout<< "your starting to get old\n";
}
cout<< "your hella young bro\n";
}
cout<< "your grown go get a job\n";
}

return 0;
}

The following 2 users say thank you to Tustin for this useful post:

Father Luckeyy, mudville209
05-06-2017, 08:48 PM #6
well thx for that but one more problem...is there a way to make your program ignore certain if statements once you go over a certain age.

for example when i change the age to 50 it prints on the screen

you old af
your starting to get old
your hella young bro
your grown go get a job

when the only thing i want it to print is

your old af
05-06-2017, 08:52 PM #7
thats why i chose to make the second if statment age<=17 instead of age!=18 because if i wrote the code like you and made my age 18 it would print "your grown go get a job" but if i pick 17 it would say "your hella young bro"
05-06-2017, 09:02 PM #8
okay here is a prime example of what i was asking..and i know i could have just used if else but i need help on nested if statments

    #include <iostream>
using namespace std;

int main(){
int age;
cin>>age;
if(age<=17){
if(age>=1Cool Man (aka Tustin){
cout<< "congrats your old enough to smoke weed now\n";
}
cout<< "sorry your too young to smoke weed\n";
}

return 0;}

why is it that when i put 17 or younger it says "sorry your too young to smoke weed" but when i put 18 or older it doesn't say anything
05-06-2017, 10:01 PM #9
Cameron
I love Tustin
Originally posted by mudville209 View Post
okay here is a prime example of what i was asking..and i know i could have just used if else but i need help on nested if statments

    #include <iostream>
using namespace std;

int main(){
int age;
cin>>age;
if(age<=17){
if(age>=1Cool Man (aka Tustin){
cout<< "congrats your old enough to smoke weed now\n";
}
cout<< "sorry your too young to smoke weed\n";
}

return 0;}

why is it that when i put 17 or younger it says "sorry your too young to smoke weed" but when i put 18 or older it doesn't say anything


Because you're not understanding what an if statement actually does.
Here's a screenshot of your code with line numbers to help explain.

You must login or register to view this content.

If we look at your if statements, you have two of them, one inside the other, nested as you say.
Your first if statement starts on line 1 and ends on line 6, line 2 - 5 are within that if statement.
If the condition of that if statement is true (age is 17 or less), then the next line that is ran is line 2.
If the condition is false (age is not 17 or less), then we skip over lines 2 - 5 and the next line that is ran is 7.

So when you input 18, the first condition is checked and evaluates to false, so it never reaches your second if statement.
You've set it up in a way where it is impossible for the nested if statement to ever evaluate to true. As is there is no number that is both <= 17 and >= 18.

The following user thanked Cameron for this useful post:

mudville209
05-06-2017, 10:09 PM #10
Originally posted by mudville209 View Post
why is it that when i put 17 or younger it says "sorry your too young to smoke weed" but when i put 18 or older it doesn't say anything


Your entire code is written inside your very first conditional block of code. Which means that if your very first statement is evaluated as false, nothing will be executed.

The following user thanked theDaftDev for this useful post:

mudville209

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo