once again google has kinda just left me confused on this one. I'm having trouble nesting a if within a if.
how would I nest this:
if ( letter == 'A' { cout << "Letter is A" << endl ; }
into this?
if ( num > 5 )
{ cout << "Number exceeds five" << endl ; }
else
{ cout << "Number is five or less" << endl ; }
You can just stick it inside the if statement like so:
[code]
if ( num > 5 ) {
cout << "Number exceeds five" << endl;
if (letter == 'A') {
cout << "Letter is A" << endl;
}
} else {
cout << "Number is five or less" << endl;
if (letter == 'A') {
cout << "Letter is A" << endl;
}
}
[/code]
If that is what you meant..
how does the if ( num = 5 ) distinguish which cout to execute? I don't understand how num skips [code]{ cout << "Letter is A" << endl ; }[/code]
and goes straight to
[code]{ cout << "Number is five or less" << endl ; }[/code]
I just want to understand this before I move on. I basically just started programming recently.
You have to think of it sequentially. The first if determines if the number is higher or lower/equal 5, and it puts out the string. And then letter is checked if it is 'A' and this puts out another string.
oh my god I get it now. Thanks guys.
Is there some form of html like tag for code? That's be handy...
If I'm right, for what you seem to want to do here, couldn't you just use the && operator in the if statement? And then an else if statement?
Just another way to approach this I guess?
[code]if(num > 5 && letter == 'A'){
cout << "Number is greater than Five\n";
cout << "Letter is A\n";
}
else if(num < 5 && letter == 'A'){
cout << "Number is less than Five\n";
cout << "Letter is A\n";
}
else {
cout << "Number is Five\n"; // num is not larger or smaller than 5
cout << "Letter is not A\n"; //letter == 'A' resulted in false in both previous statements...
}
//more checks could be added like if letter IS A and number IS 5... I think you get the picture...
//end[/code]
[QUOTE=BiciMann;43390023]Is there some form of html like tag for code? That's be handy...[/QUOTE]
It's [noparse][code][/noparse].
Like [code]this[/code]. (You can view most of an all ASCII post's source by replying to it.)
Sorry, you need to Log In to post a reply to this thread.