Basic Programming Control Flow Flashcards

1
Q

What are Control Flow Statements in coding?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the 4 main control flow statement types?

A

○ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Imperative Programming?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Declarative Programming?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

REVIEW:

Conditional Statements

A

→ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you do Nesting/Branching in Conditional Statements? Is it best practice?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

REVIEW:

Loops and Iteration

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can you mix & match Conditional Statements and Loops?

Can you mix them with Nesting?

A

YES

YES

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the D.R.Y method stand for?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an “infinite loop” ?

How do you prevent this?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Functions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Is calling a Function and Looping the same thing?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you customize a function when it is called?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a Method(s)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

→ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the KISS and DRY concepts?

A

KISS - Keep It Simple Stupid

DRY - Do not Repeat Yourself

17
Q

What is Object-Oriented Programming (OOP)?

A

OOP is is an architectural style of programming supported by many programming languages. OOP creates guidelines, rules, structure, and form that help us when writing code

The key component of OOP is the use of Objects, which are just instances of a Class or Class Definition.

18
Q

REVIEW: OOP Key Points

→ OOP is used to support Isolation, Separation of Concerns, Encapsulation, etc. – all about separation; creating things that do NOT interact with each other (or at least do so in a very minimal way).

→ Objects can be created/isolated/destroyed/manipulated in isolation from one another. We create a Class Definition, and then from there create Objects and then work with them individually – can work with one and not have to worry about the others

A

This supports DRY and KISS methodology:

§ Maintainable - code that can be easily fixed/updated/changed over time

§ Debuggable - easy to isolate issues and troubleshoot/fix bugs

§ Testable - very important; writing code that can have automated unit tests run against it

§ Legible - easy to read and understand

19
Q

In OOP, are Classes and Objects two different things or the same thing?

A

They are 2 different/distinct things - these are the foundational pillars of OOP.

20
Q

What is a Class in OOP?

Can you have multiple Objects/Definition assigned to a Class?

A

The blueprint of the code; this is the definition/instructions of how to build something i.e the blueprint example of the object that we want to create.

YES - One definition can be associated with many objects, where Objects can be run against the Class.. The Class is just the template.

21
Q

What is an Object in OOP?

A

The instantiation of a class; you instantiate a class into an object –> to create an instance of an object in an OOP language.

Objects can have different attributes/properties/information associated with them that make them different.

Ex. The Blueprint of a house is the Class, and the houses that are built from it are Objects. Each house might have a different color roof or door, which is the same as an Object in code having different attributes.

22
Q

What are the 2 things that a Class in OOP is made up of?

A

State and Behavior

Together, State + Behavior make up a Class Definition.

Every object in your code is an instance of State + Behavior

23
Q

In an OOP Class, what is the:

State?

Behavior?

A

State - the condition of the object.

EX – The circumference of “Circle-1” where the circle is the class and the value (lets say 13 inches) is the state of that circumference aka the state of the class. Any reference to “Circle-1” will yield a value of 13”.

Behavior - the code that can execute inside of our class; the “behavior of our class” .. Adds a function element to the code/class.

24
Q

Basic Programming Control Summary

A
→ Conditionals, Loops, Functions can all get bundled together to form a CLASS. From this Class, you can then instantiate an Object or many Objects and reuse the code over and over again
○ All the objects instantiated from the class get the same capability
○ Different variables in the Object will yield different results 

→ All these ways of organizing code are designed to support KISS and DRY principles

→ The reason that we follow these principles is because we want code that is testable, legible, debuggable, and maintainable
○ These structures are fairly universal for all programming languages
○ You can translate these principles into almost any programming language that you would want to use

25
Q

What is the difference between a Method and a Function?

A

A function is a piece of code that is called by name. It can be passed data to operate on (i.e. the parameters) and can optionally return data (the return value).

A method is a piece of code that is called by a name that is associated with an object. In most respects it is identical to a function except for two key differences:

  1. A method is implicitly passed the object on which it was called.
  2. A method is able to operate on data that is contained within the class (remembering that an object is an instance of a class - the class is the definition, the object is an instance of that data).
26
Q

REVIEW: What is a Function

A

In Python, a Function is a named block of code that can take a wide variety of input parameters (or none at all) and return some form of output back to the code.