Really dumb so far with C++ Currently having problems with if-else statements
I seriously cannot get them to do what I want. I can do an if statement, but as soon as I try and else or an nested if I get errors. And if I try to just do multiple if statements I get statements to override each other and I cant get the combined effects. Just no idea how to fix it.
So, I guess I dont know what to put next without causing an error. I dont know how to post the code properly, so Ill just type it manually. This part is just the beginning of the if if(num > 0 && denom > 0) num = abs(num); denom = abs(denom); else And then whatever came after that was wrong. I do have num and denom declared, so no issues with that, just...not sure. What am I supposed to do with the next statement if Im using else? Maybe im using it incorrectly? Ill post what I tried to put if needed, but I think this is more of a conceptual misunderstadning on my part then me doing something silly.
One problem I see right off is the lack of { }
I never understood the explanation of that. Lecture made it seem like it was optional, like some sort of organizational tool. Until now, we just had the whole program inside of one setof { }. So where and when do I need them in if-else?
They group commands. If more than one command is triggered by an if or an else, they need to be inside { }. That is probably what is causing the else to fail. It see no if rignt before it.
``` #include<iostream> using namespace std; int main() { int num = 1; int denom = 0; if(num > 0 && denom > 0) // More than one, so { } { num = abs(num); denom = abs(denom); } else if (denom == 0) cout << "Undefined\n"; // One command, no { } else { // do // other // things } } ```
Oh, thats what you mean by commands. So if I only had one command the programm wouldve recognized the if?
Well, it would recognize the else. You had two lines of code. The first was seen as the end of the if. The second was seen as something different. That makes the else into an else without an if, which is what your error probably was.
Gotcha. Ill see if I can stay consistent with that and if I can get the rest of it to work that way. I wasn't sure how to get the next part to work, but I also need it to change an improper fraction into a mixed number. I figure you would need to use % for that, but I guess it didnt click in my head quite how to do it :/ Of course I was worried about getting everythign else before it correct but yeah, lol.
``` if(num > 0 && denom > 0) num = abs(num); // seen as the end of the if denom = abs(denom); // more code else // else without an if ```
Ah, for ain improper to a mixed number, yah, you use %. First divide off the number to get the whole part. Then % off the remainder to get the numerator of the mixed fraction.
So, informally, something like num / denom (num % denom) << "/" << denom I havent gotten down the sequence of things yet to do all the cout << stuff properly, I just want to make sure im gettign the right idea of how to input it. I probably made it messier than what would be needed.
And I figure the "/" is so it doesnt actually divide again and just outputs the actual forward slash.
In software development they have a name for informal coding to get an idea. It is psudocode. That would work fine in a cout for just printing them. If they are needed in other calculations: whole_part = num/den new_num = num%den
And I could just used those as declared variables?
Well, yah, you would have to make them as ints if you need to save them. But if you want to just print them, no need to save them. Just need to put a << between each step. cout << num / denom << " and " << num % denom << "/" << denom;
This there a certain point where you want to maybe stop nested if statements and just completely restart if a new if else chain? Or is that going to cause problems if you try that? Also, if you ever use else, must an if always follow it?
if: means if this, do that. else if: means if not the first if, but this, do that else: means it did not match anything above so do this. In many cases, if something gets too complex people will break it down into smaller peices. Then they will have a function do parts of it and return results. This can cause your ifs to nest into smaller clumps.
Is it wrong to have multiple if-else chains or should everything be nested if you need to have that many options? I guess I found myself at a point where I thought it might be logical to start off with another nested if chaina nd I wasnt sure.
The compiler won't break until things get super complex... I don't think there even is a limit in the standard. This is opposed to recursion, which does have very practical limits pretty quick. But past 4 or 5 nested levels it just gets hard to read. Oh, and this is not nesting: if else if else if else if else if else if else if else if else That is just one level deep with lots of options.
So what is the nested if then? If thats not nested if, then I guarantee Ill get a bunch of things wrong on exams and assignments, lol x_x
``` if if if else else if bla bla else else ```
Nesting is when one thing is inside another. Not after, but inside.
Will the first else you have only apply to the previous if or to all 3?
Only to the level it is at, if properly written. Which is why the depth can make it confusing and { } can become important. Here is a short example: http://www.tutorialspoint.com/cplusplus/cpp_nested_if.htm
In that example, if you change the value of a, then the inner check for b never happens.
So its like saying if a AND if b?
Exactly. But what if you want "a and b or a and c or not a" So you can use the nested to first check a then find out which of b or c, but skip to else if not a.
Is if(a && b) an equivalent then?
Here is a modified version if you want to complie it: ``` #include <iostream> using namespace std; int main () { // local variable declaration: int a = 100; int b = 200; // check the boolean condition if( a == 100 ) { cout << "I checked a" << endl; // if condition is true then check the following if( b == 200 ) { cout << "I checked b" << endl; // if condition is true then print the following cout << "Value of a is 100 and b is 200" << endl; } else cout << "I skipped b" << endl; } else cout << "I skipped a" << endl; cout << endl; cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; return 0; } ``` Run it. Then change b, see what happens. Then change a. You should see the messages change and skipping a should stop any messages related to b.
Oh wow. Okay, that makes sense for once x_x And last thing, how do you post the coding like that?
I put ``` above and below th blocks. For larger bits of code I suggest dpaste.com or pastebin.com.
I ran the modified code just for example reasons.
The key part to get is in the last one where it does not say it even skipped b. The entire check/skip b is ignored because all of a was skipped. That is how nested ifs work.
The site also supports \(\LaTeX\) if you want to do formatted math. \(k\begin{bmatrix} a&b\\c&d \end{bmatrix}=\begin{bmatrix} ka&kb\\kc&kd \end{bmatrix}\) \[\int\limits_1^e\ln x \text{dx}\]
I thought it does say "I skipped b"
In the middle. But scroll to the end.
There are three runs there.
Even in the end I see where "I skipped b" is typed. What amI missing? xD
All I see at the very bottom is: I skiped a Exact value of a is : 101 Exact value of b is : 201
Oh, super super bottom, not just the 3rd section. Okay, so it also works like if #1 if #2 if #3 else #3 else #2 else #1 I like how it puts the comments to illustrate what the program is doing. Thanks for all the examples and explanations :3
Yah, the picture is: code run code run code run And that final run, it flat out skips b entirely. The if, the else, all of it just goes by-by because it is nested inside a and a got skipped.
Gotcha. Yeah, never thought of it as like an and statement. Yeah, for some reason lecture is doing me less and less good in this class. Just gotta survive it, lol. Maybe itll click in my head more and more as I do it.
Lecture is a way to get an overview. It is rarely a way to get good detail. And c++ is not the friendliest language to start with...
Never really had a choice. This was the "intro" course I had to take. Where is the usual starting point?
C++ is where a lot of people start. Python and Java are both a little friendlier and get to graphics with built in things so they can be more interesting. C++ suffers from the linage of being an overhaul of C into object orientated programming. C, while very powerful, is exceptionally confusing to read at times.
Yeah, this was considered tobea course for those who know nothing about cimputers going in. Seems like that was a bit of a fib. I just need to get this assignment done with and then review something on the last assignment. I couldnt get things to align the way I needed them and I was sick, so I didnt have time to figure it out. If I got the time I can bring it back up and revisit it, but another time. Yeah, you help out a lot, so thanks again.
If you want to contrast how a Python class goes, you can do so for free from Rice University: https://www.coursera.org/course/interactivepython
And the great thing is, if you learn the concepts in Python, you can translate them to C++. I have used things I learned in C++ in Python and Java.
Join our real-time social learning platform and learn together with your friends!