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, 10:51 PM #11
Originally posted by theDaftDev
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.


so if i was to do function of this type i shouldn't use nested if statements ?

and what if i wanted to make 4 if functions such as
    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";}

}
Last edited by mudville209 ; 05-06-2017 at 11:00 PM.
05-07-2017, 01:16 AM #12
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;}


This code won't tell you that "you're starting to get old" because the instruction to do that is located in a conditional block that will only be executed if the age is lower or equal to seventeen ( second conditional statement ).
What your code does:

Is age higher or equal to 18? Yes -> Is age lower or equal to 17? No -> Print "your grown go get a job\n"; -> Return.
I don't think you're quiet grasping the concept around conditional control structures.

Originally posted by mudville209 View Post

so if i was to do function of this type i shouldn't use nested if statements ?


You use nested conditional statements when you want to run code when several conditions are fulfilled or not.
If you want me to give you an example of a situation where a nested statement could be relevant, just let me know.

Originally posted by mudville209 View Post


and what if i wanted to make 4 if functions such as
    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";}

}


Watch out, "if's" aren't functions. They are conditional statement which, if evaluated as true upon runtime, will allow you to run the code you wrote into the respective conditional block.
What you did here does work and is somewhat relevant if you want to log a message when the age is higher than or equal to 18 and another message when it is higher than or equal to 30, for example. Which is the actual outcome you got out of all the different possible outcomes, considering that, in this particular case, the age is set to 36.

But, if you think about it, you would be better off nesting the statement checking if your age is higher than or equal to 30 because this condition will only be verified when your age is higher than or equal to 18 anyway.
Last edited by theDaftDev ; 05-07-2017 at 01:32 AM.

The following user thanked theDaftDev for this useful post:

mudville209
05-07-2017, 01:39 AM #13
Originally posted by theDaftDev
Is age higher or equal to 18? Yes -> Is age lower or equal to 17? No -> Print "your grown go get a job\n"; -> Return.
I don't think you're quiet grasping the concept around conditional control structures.


nah i understand what you are saying after you and cameron explained it i understood just fine ur saying in nested if statements if a if statement is false then the program ignores everything underneath that false if statement...

but i would apperciate it if u would send me an example of were u should use nested if statements
Last edited by mudville209 ; 05-07-2017 at 01:48 AM.
05-07-2017, 02:04 AM #14
Originally posted by mudville209 View Post
nah i understand what you are saying after you and cameron explained it i understood just fine ur saying in nested if statements if a if statement is false then the program ignores everything underneath that false if statement...

but i would apperciate it if u would send me an example of were u should use if statements


Well what you're trying to achieve is a decent example. Why? Because at some point you need to run code which has two different outcomes, both happening when a very particular condition is met.
( In your case, when the age is over or equal to 18. )

There are a total of 4 conditional statements in your code.

One which checks if your age is lower than or equal to 17.
One which checks if your age is higher than or equal to 18.
One which checks if your age is higher than or equal to 30.
One which checks if your age is higher than or equal to 40.

You want to be logical and efficient so you need to verify as few conditions as needed to make your program work the way you want it to do.
Think in terms of relationship. Let's take the third statement.

"One which checks if your age is higher than or equal to 30."

As a matter of fact this very statement should only be checked if your second statement is TRUE.
Why? Because you can't be 30 or older if you aren't over 18! In other words, this statement will NEVER EVER be evaluated as TRUE if you are younger than 18.

Let's take a look at your first statement now.

"One which checks if your age is lower than or equal to 17."

As a matter of fact this very statement shouldn't be nested in any other of the other previous statements.
Why? Because this statement will NEVER EVER be evaluated as TRUE if your age is higher than or equal to 18.

The same way, of all your previous statements none of them should be nested in the conditional block being run if the first statement is validated.
Why? Because if we take a look at all the other 3 statements, none of them is going to EVER be TRUE if your age is lower or equal to 17.

See? You are minimizing the amount of statements you need to verify in your code thanks to nested conditional blocks.

So how does that translates to code?
Well:

    
int main()
{
const int age = 36;

if ( age >= 18 )
{
cout << "your grown go get a job\n";

/*Assuming that you're not going to tell a 40 yo person that's "she is starting to get old" before telling her that she is old as fuck
( because that's not logical, right ), you need to use "else if" */
if ( age >= 30 )
{
cout << "your starting to get old\n";
}
else if ( age >= 40 )
{
cout << " you old af\n";
}
}
else if ( age <= 17 )
{
cout << "your hella young bro\n";
}
}
Last edited by theDaftDev ; 05-07-2017 at 02:07 AM.

The following user thanked theDaftDev for this useful post:

mudville209
05-07-2017, 04:06 AM #15
Originally posted by theDraftDev
int main()
{
const int age = 36;

if ( age >= 18 )
{
cout << "your grown go get a job\n";

if ( age >= 30 )
{
cout << "your starting to get old\n";
}
else if ( age >= 40 )
{
cout << " you old af\n";
}
}
else if ( age <= 17 )
{
cout << "your hella young bro\n";
}
}


i finally found out how and why you should use nested if statments...but i made the program without nested if statments and made it only give one outcome for any age you put..

here's the code

    #include<iostream>
using namespace std;

int main()
{
int age;
cin>>age;
if ( age >= 40 )
{
cout << "you old af\n";
}
else if( age >= 30 )
{
cout << "your starting to get old\n";
}
else if ( age >= 18 )
{
cout << "your grown go get a job\n";
}
else if ( age <= 17 )
{
cout << "your hella young bro\n";
}
}
Last edited by mudville209 ; 05-07-2017 at 04:46 AM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo