Basic Programming Control Flow Flashcards
What are Control Flow Statements in coding?
Statements that are used to control:
○ Which code executes
○ When it executes
○ How often it executes
○ Whether or not it repeats
i.e controls how the code operates
What are the 4 main control flow statement types?
○ Conditions (if-then-else)
○ Loops (while, for)
○ Exception handling (Try); Ex - only gets run in the presence of an error or an exception in your code (like a division statement saying to divide by 0)
- –> This adds a bit of fault tolerance; your code can “try” something else in the event of an error
- –> This is called “graceful degradation”
○ Go-To and Return - these aren’t really used anymore i.e “go to” and jump around within your code and cause different pieces of code to execute under certain circumstances
What is Imperative Programming?
Programming language that describes a process – This is the majority of languages and by far the most common.
You outline how you want your code to execute; you do this step by step.
What is Declarative Programming?
Language that describes the result i.e what we want. A “parser” describes the HOW, not us, like in Imperative Programming.
SQL is the most common declarative language… when you run a query or code, you describe what you want out of the table under certain conditions and the SQL engine (aka parser) handles the “HOW”
REVIEW:
Conditional Statements
→ One of the most basic/primitive control flow statements that is offered; foundational i.e “if-then-else” statements
→ IF followed by a condition – if it’s true, a certain code runs and if it’s false, a different certain code runs
They are mutually exclusive to each other i.e if the statement goes down one path (then) it cannot go down the other (else); they are separate
Can you do Nesting/Branching in Conditional Statements? Is it best practice?
YES - You can have another IF statement within an umbrella IF statement further changing how the program works.
–> If you overuse conditionals and nesting, it can lead to very poor code i.e complexity + hard to read
–> Rule of thumb: if it’s hard to read, it’s hard to maintain and if it’s hard to maintain then it’s hard to fix bugs
REVIEW:
Loops and Iteration
Looping - allows you to create code that will be executed multiple times over and over in succession.
–> Once the code gets to the end it will restart back at the beginning until a certain action triggers it to end; you can also specify how many times the code runs if you want (For Loop)
Can you mix & match Conditional Statements and Loops?
Can you mix them with Nesting?
YES
YES
What is the D.R.Y method stand for?
Do not Repeat Yourself
Write the code once and then have the code work for you.
Example:
○ If you write code, there’s a good chance it will have a bug
○ If you write the same code twice, it’s likely it will have a different bug
○ If you ever want to fix some code, you really only ever want to fix it once i.e in one place
What is an “infinite loop” ?
How do you prevent this?
A loop that gets created that accidentally fails to have a break, or a trigger that will cause the code to stop so it just runs infinitely (Ex. if you coded “if true = true” .. where True is obviously always True, so the code just never stops running.
–> You should always have a “break condition” within a loop definition; always understand what the breakout parameters are i.e what will break the code out of a loop
What are Functions?
Functions allow for a block of code to be assigned to a “symbol” so that it can be invoked on demand (can be called as many as you need it to run).
Invoking the function is also called “calling a function” .. the Code in a function does NOT run until it is actually invoked or called.
Function takes in an INPUT and puts out an OUTPUT based on that input.
Is calling a Function and Looping the same thing?
NO.
→ Function is different than looping, in which the code runs in succession over and over until it is triggered to stop
→ with Functions, you can invoke the code on demand whenever you want, how ever many times as you want
How can you customize a function when it is called?
You can pass ‘parameters’ into a Function, which allows you to supply values to the function. This causes the Function to run a bit differently when invoked.
Different objects that have different parameters will yield different results when the function is executed –> same function, same code, but different results based on which variable was passed into it
What is a Method(s)?
A method is a function that “belongs to” an object.
Functions that are defined as part of a class; they can only be invoked by instances that are part of that class. A method is associated with an object, while a function is not.
Methods can reference the object instance they’re being invoked from.
REVIEW: Classes, Methods, Functions and Instances
→ A class describes state and behavior of an object
→ Methods and Functions are the behavior - they describe the functionality or what the class DOES as opposed to what the class IS
→ Instances can also be called “versions” i.e you can have different “versions” of the specified class for that code that have different values passed in which would in turn yield different results when executed against the code definition
→ In the example Player1 and Player 2 are part of the “Player” class, so they can invoke that function.. But since they have different values, they will have different results when they invoke the Class Definition