Lecture 4/8: Lecture 2 Q&A
Lecture 2 Questions and Answers (Exported from Zoom Q&A log)
Q: What's the name of this song?
A1: Quero Que Tu Va
A2: Quero Que Tu Va by Ananda and Joker Beats!
Q: Thank you
A1:
Q: We're muted automatically right?
A1: yes!
Q: How crucial is it to follow with the textbook or is reviewing the lectures sufficient?
A1: The textbook is a helpful secondary resource, but all the info you need will be presented in lecture
Q: do you mind sharing the password for webinars here? the webinar opens automatically if I click the URL on Canvas but not if I type the meeting ID in
A1: Can you send me an email to remind me about this zelenski@stanford.edu
Q: how does the ACE session differ from CS106L?
A1: See the https://web.stanford.edu/class/cs106b/handouts/course_placement
Q: Will the Qt Installation Session be recorded and posted?
A1: live answered
Q: Are there multiple sections at the same time?
A1: Yes
Q: Will we be âproficientâ in C++ at the end of this course?
A1: Goal is proficiency in classic data structures, algorithsm, good design, but not a deep dive into C++ language itself. If you want that, check out CS106L
Q: are the slides posted online?
A1: Yes, under the lectures tab. For today the slides are here : http://web.stanford.edu/class/cs106b/lectures/c++/
Q: does char work for all strings?
A1: a char is a single character, string is a sequence of characters. In python, a character is more a special case of a length 1 string, but in C++ it is more distinct
Q: What happens if you mix the types? What if I write the following:
int a = âxâ
A1: That particular mix is allowed because as Chris said, integer and chars are related behind the scenes. We sometimes say they are in same type âfamilyâ
Q: do you have to put f at the end of float declarations?
A1: No, not necessarily. (This is an aside, but without the suffix, a constant defaults to being double-width, the f suffix indicates single-width)
Q: can he explain string again?
A1: live answered
A2: live answered
Q: what does the semicolon do? do you need to add that after every line
A1: C++ uses semicolon as a statement terminator, every statement must end with a semi-colon to be âlegalâ syntax
Q: Neat! Thank you.
A1: live answered
Q: Chris is awesome
A1: Agreed!
Q: do floats have an objective limit to how many numbers after a decimal point they can have compared to a double? also, is there ever a considerable storing issue for being more conservative and using âdoubleâs as opposed to âfloatâs?
A1: Yes, there is! But the details of this are outside the scope of this class. Youâll learn more about this in CS107!
A2: Itâs roughly 6 decimal places for float and twice that for double. Modern practice is to mostly use double for everything, space is cheap!
Q: wait so having âint a = 5â and âint a = 3â in the same code block renders an error⊠but having âint a = 5â followed by âa = 3â does not?
A1: Each new scope (opened by a curly brace) can shadow/replace a name in outer scope
Q: can you redefine a variable as a different type outside of the curly brackets later in the program
A1: Yes, we will talk about scope and different sets of curly braces a little later in this lecture!
Q: after how many decimal points should a number be defined as a double and not a floating point?
A1: Itâs roughly 6 decimal places for float and twice that for double. Modern practice is to mostly use double for everything, space is cheap!
Q: Can floats and doubles interact? or can you only use floats with floats, etc?
A1: From a high level perspective, they can interact, but really what is going on behind the scenes is that the types are being converted back and forth.
Q: do we need to have all the #âs at the beginning of the code?
A1: The #include is the rough equivalent of import â it is you telling the compiler what libraries your code is using
Q: so is string a defined type?
A1: string is a type defined in the C++ ilbrary
A2: yes, string is a defined type that we are allowed to use becuase of the #include
Q: whats the difference between using apostrophes and quotations? â â vs â â
A1: â â is used for char type and â â is used for strings
Q: Is it allowed to redefine a variable to another type?
A1: generally, once a variable is defined to be of a certain type, it can only store values of that type.
Q: what is the difference between having âintâ before the variable and not having that
A1: live answered
Q: How do I access the Stanford libraries?
A1: they will come packaged together with the starter code for every assignment!
Q: Do we add a semicolon for each line? What if the code does not end on the first line and continues on to the next?
A1: The semi-colon is the what terminates the statement, the whitespace/return/tab dosenât matter to C++ (this is unlike Python)
Q: Can I do addition and subtraction between float and double?
A1: Yes
A2: Yes, and the resulting value will be of type float
Q: I read ahead and the definition of headers isn't entirely clear. Are they simply files that advertise the available public functions and variables of a given c++ file?
A1: Yes, exactly!
Q: whats the point of declaring a variable without setting it to something early on?
A1: Best practice would be to initialize when delcaring, but sometimes you need to do some additional calculation before you can make the assignment
Q: So we should not declare int a at the top of our prgrams and define it later?
A1: generally, you should only define a variable when you need to use it. But yes, you are not allowed to re-define a variable of the same name once you have defined it once
Q: Are there predefined integer values associted with each letter character / special characters?
A1: Yes, it is called the ASCII table.
Q: May be the confusion is because the declaration and initialization happens in the same line as âint a= 10â
A1: live answered
Q: im pleased at how similar this is to Java thus far
A1: live answered
Q: is there a style preference for typing with spaces (ex. a = 5 vs a=5)?
A1: spaces are preferred because it makes your code more readable!
Q: if you define a statment with int a = 5; and then call a = a string, will it be an error?
A1: yes! once a variable has been defined to be a certain type in C++, it can only store values of that type
Q: is the scope where the â}â bracket ends?
A1: yes! We will cover this more later in the lecture
Q: Thanks!
A1:
Q: if we can't make the qt installation session, is it pretty straightforward to install?
A1: there are detailed instructions on the course website that you can follow. The session is mostly for debugging any issues that arise.
Q: what does stream mean?
A1: live answered
Q: Why does char a=4 work?
A1: Each letter is assigned a number in the ASCII table.
Q: why are multiple « necessary ?
A1: the << operastor is used to add together all the individual things you want to output â since we want to output a string and a newline, we have to use two « symbols
Q: why does it use brackets instead of parentheses like a normal function?
A1: Iâm not sure I quite follow, can you clarify?
Q: Can you explain the "stream" cout again? (what does stream mean)
A1: live answered
Q: Can you print by doing
cout << theAnswer is num. << endl;
A1: live answered
Q: how does c++ deal with naming variables things like endl?
A1: endl is a reserved name that youâre not allowed to use for your variables.
Q: do you recommend using endl instead of \n?
A1: Yes
Q: Is «endl; always required at the end of printing?
A1: live answered
Q: do we also put semicolons between control flow statements like âifâ or âforâ?
A1: live answered
Q: Can we just run code from Qt to our terminal for our homework/projects or do we have to always download the blankproject in order to have the console work?
A1: You have to use Qt to run all the code for this class â each assignment will have its own starter project that you will download and write code in. Each lecture also has an associated Qt project that you can download and use
Q: whats the difference between endl; and \n
A1: basically same
Q: is endl equivalent to the new line character in python?
A1: yes
Q: What I meant is why does cout use « instead of (âŠ), like a normal function would?
A1: arguments being output to stream are chained togetherr, the « is acting more like a operator in this case
Q: can we run the all-examples.cpp file? Or we can only edit the code, but not see it runs the result?
A1: Open the project file (named with .pro extension) and then you can build and run the program
Q: why i++?
A1: shorthand for i = i + 1
Q: why do you have to put i++ after the print statement?
A1: live answered
Q: whats the difference between i++ and ++i
A1: pre vs post increment
Q: Does the (i<5) need to be in parantheses?
A1: Yes, it does, the condition in the if or while statement has to be in parentheses
Q: for nested loops syntax do we put another curly bracket within the first?
A1: yes
Q: Does i have to be int for i++? Or does float, double etc work too?
A1: Yes it does
Q: would i += 2 compile?
A1: yes
Q: if you want to increment by more than one, do you add a value after the ++?
A1: live answered
Q: if you are doing a different number than 1 do you still do ++ or do you do like i + 2
A1: live answered
Q: sorry, what does post increment mean? Iâm not quite sure what ++i; does
A1: Donât worry for now, both increment I
Q: Would i++ also work in Python?
A1: No, not legal syntax for python
Q: could we also do i = i + 2;
A1: Yes
Q: could you also just say i = i + 2;
A1: Yes
Q: is i=i+1 ok?
A1: yes!
Q: this is very simliar to java! âșïžđ
A1: live answered
Q: what tablet/program/function is chris using to be able to draw on his screen
A1: Wacom table and InkToGo software
Q: Is string defined in the std:: library? It seems I need to do something like
A1: Yes, string is in the std:: namespace. We will typically do a using namespace std: at the top of our programs so we can use the nickname without the formal name
Q: std::string theAnswer = "Hello";
A1: live answered
Q: how is the update statement different from including an update in the body fo the for loop?
A1: It could be same, but by promoting to loop header makes sure you donât forget to have an increment step
A2: either one will work, but typically we include the update step as part of the for loop declaration, since this will automatically cause the update to happen at the end of the loop
Q: Why does the for loop output 0,1,2? does it not do the update statement until after it runs the first time?
A1: Update hapens at botom of loop
Q: are there boolean variables in c++?
A1: yes, type is bool, keywords true and false
Q: those two things are functionally the same, but is one better style wise?
A1: live answered
Q: is there a difference in using an equivalent while/for loop? is it just a style difference or is there a differnece in runtime/efficiency for a more complicated example
A1: live answered
Q: What day of the week are homework assignments due?
A1: always due on fridays, with the late deadline on Sundays
Q: why would we want to start a for loop with i = 0 instead of i = 1?
A1: C programmers start counting from 0, take 107 if you want to learn why
Q: The loop does not go back to intialization statement ever after it starts running,right?
A1: No, first time only
A2: no, this statement only happens once!
Q: Will we have access to the Qt project with the notes after lecture?
A1: Yes, it already posted on the lecture page
Q: Is there 0 indexing in C++ as well?
A1: Yes
Q: instead of cout « can you use print(ââ);?
A1: no, there is no defined print() function in C++
Q: Is he referring to global variables vs. local variables?
A1: scope applies to all variables â global variables are just variables that are defined at the top level of {} which means they are available everywhere
Q: Are blocks defined by {}?
A1: yes!
Q: Can you access loopVar inside the for loop?
A1: Yes
Q: How can C++ access updated outsideVar values from inside the block?
A1: The C++ compiler keeps a table mapping names to variables
A2: scope works from outside in â if you define a variable in an outer block, you can access this variable in an inner block defined in that inner block
Q: What if you have an insideVar outside the block, and try to define another one inside the block.
A1: The inner variable will âshadowâ the outer one
A2: you run into the same issue of redefiniing variables as we talked about before
Q: does a new insideVar get created each time the for loop runs? Why is that ok?
A1: The one from previous iteration is no longer needed, so it basically can just re-use it on the next iteration
Q: so just to be clear, a variable that is manipulated within another block, as with outsidevar, will update its value outside of that block? iâm just confused why printing outside var wouldnt still print 5 since it only added within the for loop?
A1: A new scope inherits the outer variables, it does not make copies of them
Q: So in C++, For Loop is more like a function in Python right?
A1: Not quite
Q: if we increment outsideVar inside the for loop, will that be the new value for outsideVar both inside and outside of the loop?
A1: live answered
Q: if a and b are true does it satisfy a l l b
A1: yes!
Q: What actually is
A1:
Q: what about calling variables among different functions?
A1: weâll talk about that on friday!
Q: thank you all! <3
A1:
Q: Can we post the question and answer thread from lecture afterwards?
It is hard to follow while listening to the lecture.
Q: could you confirm this:
in a < b : a and b are both doubles, both ints, both floating points, or a mix?
whereas in a&& b : a and b are both booleans?
Q: Also, will your example code with comments be posted after lecture?
Q: Will we be going over scoping again on Friday?
Q: Regarding scoping, could we then reassign loopVar outside of the for loop to a different var type and value?
Q: is outsideVar still 5 after the for-loop runs?