Jump to content

Logic in Computer Programming

rexdog7

This may seem strange but I have to write a paper on mathematical logic and how it is used in regards to computer programming. However, I'm pretty clueless when it comes to programming and after researching for a couple hours, I'm more confused than when I started. I'm not asking anyone to write a paper for me or anything, but does anyone know of a resource which explains things in such a way that someone as inexperienced as myself could understand it? If someone could steer me in the right direction that would be fantastic. Also, I apologize if this isn't something that should be posted here but my experience with this forum has been great so far and I figured I'd give it a shot considering right now I'm lost.

Link to comment
Share on other sites

Link to post
Share on other sites

Mathematical logic in programming can be used for core values, data values and even for variables. Can be used in clock or a timer telling a event/timeline to wait or stop. If something were not be equal to zero and it needs to be, it will wait and keep checking until that variable is equal to zero. Mathematical logic is used a lot in programming, say you have a player collecting items, such as gathering wood from a tree, if that tree were to obtain you 5 wood, then it has to tell the counter that it needs to add 5 to the current amount of 'wood'. Or taking damage, someone yields a weapon and it does 18 attack damage, math is used to tell the program it needs to take away 18 from the existing health. 

 

If you need more feel free to ask.

Link to comment
Share on other sites

Link to post
Share on other sites

Mathematical logic in programming can be used for core values, data values and even for variables. Can be used in clock or a timer telling a event/timeline to wait or stop. If something were not be equal to zero and it needs to be, it will wait and keep checking until that variable is equal to zero. Mathematical logic is used a lot in programming, say you have a player collecting items, such as gathering wood from a tree, if that tree were to obtain you 5 wood, then it has to tell the counter that it needs to add 5 to the current amount of 'wood'. Or taking damage, someone yields a weapon and it does 18 attack damage, math is used to tell the program it needs to take away 18 from the existing health. 

 

If you need more feel free to ask.

 

This is really interesting. Everything I've researched so far has led me to believe that logic has to do more with "If, then" statements. Is this not true? The main source of my confusion was the lack of math involved with those types of statements. The three topics that I have to cover in my paper are:

 

1) Explain what Logic is

2) Explain what computer programming is

3) Explain how Logic is used in regards to computer programming

 

Somehow I'm going to have to elaborate on these three until I can fill 3 pages. Luckily I actually do have an interest in programming, I just haven't had the time to explore it yet. This may be a long night haha

Link to comment
Share on other sites

Link to post
Share on other sites

If then is the basis of the logic. To break it down that's not in words, but i'll explain it: 'If canshoot is = 1' - Variable if that isn't = 1 then nothing can happen is pretty much what it means. Logic in a nutshell is the commonsense of programming, Programming is everything a computer works off of and programming is like super advanced settings. Logic in regards to programming here is an example, a real life example: If your computer monitor isn't working, hitting the keyboard isn't going to help. - This makes no sense to do so correct? So then why add it to something unless you are irritable or angered. Logic can also be how exactly the game works with 'If, then' statements.

Link to comment
Share on other sites

Link to post
Share on other sites

So essentially computer programming is the writing of a sequence of instructions that are implemented by a computer. And logic is the "If, then" system that is used to create these instructions?

Link to comment
Share on other sites

Link to post
Share on other sites

So essentially computer programming is the writing of a sequence of instructions that are implemented by a computer. And logic is the "If, then" system that is used to create these instructions?

Pretty much yes!

 

Sorry for late response, I was doing some studying for a final :P

Link to comment
Share on other sites

Link to post
Share on other sites

Pretty much yes!

 

Sorry for late response, I was doing some studying for a final :P

 

It's fine! I have to do some of that too after I finish this paper. Like I said it's going to be a long night  :D

Link to comment
Share on other sites

Link to post
Share on other sites

It's fine! I have to do some of that too after I finish this paper. Like I said it's going to be a long night  :D

Yeah, same. Probably until 1 or 2 in the morning, then a 7 am class till noon then I can come home and sleep.

Link to comment
Share on other sites

Link to post
Share on other sites

One question that I have is if logic is just "If, then" statements, where does the math come into play? I understand what you previously said about the 5 wood blocks being added to your inventory or 18 damage being subtracted from your health, but is doesn't seem like math is a fundamental part of logic. Since I'm writing this for a math class I figured it would be more math related than it appears to be.

Link to comment
Share on other sites

Link to post
Share on other sites

You might have better luck posting on http://stackoverflow.com/

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

One question that I have is if logic is just "If, then" statements, where does the math come into play? I understand what you previously said about the 5 wood blocks being added to your inventory or 18 damage being subtracted from your health, but is doesn't seem like math is a fundamental part of logic. Since I'm writing this for a math class I figured it would be more math related than it appears to be.

 

Sorry, I will explain a bit more later...in a rush right now.

 

Basically it boils down to boolean logic...e.g. if(!(a == b && b == c)) can be written if(a!=b || b != c)...this is derived from boolean logic itself

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

One question that I have is if logic is just "If, then" statements, where does the math come into play? I understand what you previously said about the 5 wood blocks being added to your inventory or 18 damage being subtracted from your health, but is doesn't seem like math is a fundamental part of logic. Since I'm writing this for a math class I figured it would be more math related than it appears to be.

The Math comes into play when you actually construct the logic statements. You can have some pretty complex conditions.

 

Say, if ( x % 5 == 0 ) - that's the basic remainder operator. It checks to see if x is completely divisible by 5. Mathematically, that's actually: 

x/5 - floor(x/5) == 0 - floor is a round down operator (depends on language). 

 

That's still pretty simple math. There are no limits to what conditions you can throw into the if/then statements, though. 

 

You can also use math to create simple functions. Say, evaluating the 1001th prime number, or nth prime number. You could either use an equation for it, or a simple algorithm to calculate it recursively. It's still math. 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Check out "From NAND to Tetris". It is a course but there are some vids on YT that might help you understand the concepts a bit more. Although if it has to be in tomorrow then this might be a bit late.

 

Rig: i7 2600K @ 4.2GHz, Larkooler Watercooling System, MSI Z68a-gd80-G3, 8GB G.Skill Sniper 1600MHz CL9, Gigabyte GTX 670 Windforce 3x 2GB OC, Samsung 840 250GB, 1TB WD Caviar Blue, Auzentech X-FI Forte 7.1, XFX PRO650W, Silverstone RV02 Monitors: Asus PB278Q, LG W2243S-PF (Gaming / overclocked to 74Hz) Peripherals: Logitech G9x Laser, QPad MK-50, AudioTechnica ATH AD700

Link to comment
Share on other sites

Link to post
Share on other sites

@smjpl 's video is quite good...

 

To go into more of what I was saying earlier depending how much liberties you want to take, concepts of boolean algebra are all over computers...the fundamentals behind the computer logic are all based on boolean algebra.

 

For example, computers are made up of logic gates...these logic gates are NAND, AND, OR, XOR, etc....putting together these gates you can create devices that add two numbers together.

 

In terms of programming, I would argue it is a bit more abstract but the concepts from boolean algebra are brought over to programming.  The if-then statement is the key to programming and to an extent boolean algebra.

 

P -> Q (P implies Q, or if P then Q) means that P is correct then Q is correct.  While this math isn't directly in computing it is similar...I always imagine it's the application of the P then Q.
  Taking the P->Q example...for programming it is rather statements that setup whether P is true or false....if P is true then Q must be true....so do the stuff in Q, otherwise Q is false and Q code is not run.

 

Anyways that is my take on it...there are obviously more complex aspects which could probably be addressed, but it has been so long since my formal math classes that my head is a bit spongy on that material.  But for things like if statements...all the rules of boolean algebra apply (e.g. demorgans law)

 

I would recommend reading this section on wiki though...I think it pretty good (as well as the Application section...the application is very much like programming)

http://en.wikipedia.org/wiki/Boolean_algebra#Propositional_logic

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Programming isn't as much about what the answer is. It's more about how to get the answer.  It's using logic to devise an algorithm that the computer uses to output the answer you want. You don't need to know the answer, you just need to walk the computer through step-by-step to get the answer.

If it can mean anything to anybody at any time, it means nothing.

Link to comment
Share on other sites

Link to post
Share on other sites

Some things I would like to add: (hope it's not too late :unsure: )

1) Explain what Logic is

You can begin with a bit history. First research in this field was done by philosophers in the ancient world. Today the most known mathematical logic is propositional calculus, which is a boolean algebra (like already mentioned). But there are also more complex formes of mathematical logics, e.g. predicate logic. There is enough material out there about this stuff.

 

2) Explain what computer programming is

Computer programming is telling a computer how to solve a mathematically specified problem, in which way doesn't matter. Different approaches to computer programming are also called programming paradigmes. Imperativ programming is basically giving sequential instructions in combination with flow control (if - then -else; loops). Functional programming would be another paradigm. It is more mathematical and describes the problem to solve, instead of describing the way to solving it.

 

3) Explain how Logic is used in regards to computer programming

As I mentioned there are many programming paradigmes and one of them is logic programming (http://en.wikipedia.org/wiki/Logic_programming). The most famous example of a logic programming language is Prolog (http://en.wikipedia.org/wiki/Prolog). Basically you write down facts and rules that describe your problem. Then you can ask certain questions and you will get the result. Prolog performes this on basis of mathematical logic, it tests satisfiablility etc.

 

Furthermore logics is heavily used in hardware development, eg FPGA programming using VHDL. (not exactly sure if this is relevant for you)

 

Hope i could give you some more ideas for your paper.

Link to comment
Share on other sites

Link to post
Share on other sites

About the if statements:

 

if(this) then

     do this

else

     do this

endif

 

The actual statement in this example is "if(this) then". I don't think the word "then" is required in some languages but that's beside the point.  The purpose of this statement is to determine if a value is true or compare it to another value. You could say "if [value] + [value] = [value]" but it's better to do that calculation outside of the if statement and assign the result to a variable.  So the statement would be "if [variable] = [value]". This would be your comparison. There are many comparisons you can use including =, <, >, <=, >=, <>(not equal to, depending on language).

 

Determining if a value is true (called Boolean values) is the other thing you do in if statements.  This is mostly done with properties given to objects through the language such as a check box's "checked" property. The computer sees these values as a 1 (true) or a 0 (false). You can create a variable and change its value from 1 to 0 throughout the code so you may refer to its value in your if statements.  Those two would look like this:

 

if (checkbox.checked = true) then

     do this

else

     do this

endif

 

if (variable = 1) then

     do this

else                                  There is no need for "If (variable = 0) because you will only assign the variable either a 1 or 0, therefor if it's value isn't 1, it's value is 0

     do this

endif

 

The actual math part comes in the "do this" part. You can do whatever calculations you want.  I guess doing comparisons is technically math. That's up to you if you want to put that in your paper.

If it can mean anything to anybody at any time, it means nothing.

Link to comment
Share on other sites

Link to post
Share on other sites

This is all really interesting stuff. I already had to hand in my paper but researching this has definitely sparked my interest in programming. Thanks so much for your help guys!

Link to comment
Share on other sites

Link to post
Share on other sites

This is all really interesting stuff. I already had to hand in my paper but researching this has definitely sparked my interest in programming. Thanks so much for your help guys!

 

So how did it turn out... 

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

So how did it turn out... 

 

I think it was pretty good. Since this is what my teacher majored in and has an interest in and I was the only person to write a paper on the subject, I think I'll get a good grade because of that. I was struggling to get to the three page requirement but I did eventually manage. Also since it's a calculus based class and this was not relevant to what we're learning he's not going to grade it harshly.

Link to comment
Share on other sites

Link to post
Share on other sites

Good thing you did need help with this.  I'd have a problem fitting such a paper in three pages.

If it can mean anything to anybody at any time, it means nothing.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×