L1: Questions // Side videos Flashcards
In a do while loop, why do we return the variable n?
The variable can only exist within the scope defined by curly braces. For the sake of the do while, it is defined in “int n;” above do so that it can then be checked in while. We then return the value at the end of the function so that it can be used in the rest of the program, outside of the curly braces it was defined in.
In any f(), we declare a type before the function and in the parenthesis, what do these refer to? What would it mean if either of these types were defined as void?
If the function reads as “void function(int)”, we would be saying that the function gives no output, but takes an int as the input. Vice versa, “int function(void)” would say the function doesn’t take input but gives an int output.
To be clear: the input here refers to input from other lines of code. A function(void) can still take user input as a part of the function.
What is type unassigned int?
Unassigned is a type modifier that excludes values less than zero, giving the 32 bits int starts with exclusively to positive integers.
How do we define what floats can be declared?
32 bits are available, but there is no defined number of possible values. Only that we must define the precision based on available decimal places. The higher the left side of the decimal point, the fewer are available for the right side of the decimal place.
How do we increase the precision of a float?
Use a double instead.
What is the void type?
It is not a “data type”, simply a type. Since variables are data specific, they cannot be declared as void. This restricts void to being used with functions.
Improve the following code:
int x;
int y;
int z;
Reduce to one line:
int x, y, z;
When giving values to these later, do not redeclare the type (x = 2; // y = 4;)
Bool isn’t a type in C, declared in cs50.h. How are these parsed in C?
All nonzero value is true. Zero is always false. Because of this, we can use bool operators without defining the bool type.
define ||, &&, !x
They are not, and, not x respectively.
what is mutual exclusivity in conditional statements?
Nested if statements are mutually exclusive. Meeting one of the if statements nested does not exclude the other if statements from running if their conditions are also met.
When writing nested if statements, how will an else be handled?
The else that is declared at the end is exclusive to the if statement it follows, and will still run even if an earlier if statement is false.
What is a switch(x)?
Switches run starting from the conditional that meets the same value as whatever “case x: // printf(‘###’) // break;” The break statement is necessary to keep the switch from running all cases that follow, but can be omitted if wanting to run through to the (default:).
Define in steps 1, 2, and 3 what the following does:
for (int i = 0; i < 10; i++) {
- initiate counter variable (defining the type and value.
- evaluate the boolean value (run if true, quit if false)
- increment the value when running as true
The loops while, do while, and for are best used in certain situations. Give an example for each.
While: runs as long as the condition is met. Good for a control flow when the duration is unknown (how long a player plays the game)
do-while: good to check inputs and run a code once.
for: good for defined numbers of runs on a set of code.
What OS is the CS50 IDE in?
Ubuntu. Is a linux distro, so is unix based and runs same CLI commands as Mac.