Question:
What is the physics of programming?
anonymous
2011-02-05 01:03:26 UTC
so how in general does programming work? is it actually a case of literally knowing a language like a foreign language? either you know the word (code) or you don't?

Pls someone tell me very basically the procedure of how something like C++ would work.
Five answers:
anonymous
2011-02-05 01:17:22 UTC
Learning the syntax (the words and the grammar so to speak) comes quickly. It's not that much to learn.

The hard part is learning to use the code to solve a problem, this is fairly universal for all programming languages. People that have had programming experience before find it much easier to jump into a completely new language, this is unlike learning a foriegn language.



If you can think logically, then you can solve virtually all problems using only basic knowledge of the programming language.
Ben
2011-02-05 09:24:40 UTC
It's not so much a language as it is a particular way of describing a set of problems. At the very basic level, your processor understands a simple set of instructions: Add, subtract, multiply, divide read from memory, write to memory, and basic truth equations (and, or, not). You use a programming language to write a more human-readable program that can be turned into instructions for a machine.



The most basic component of a program is an expression or a statement- a single instruction for the computer to execute. Beyond that, you have blocks- groups of expressions. From there, you get control flow- only do something if a certain condition is true, or keep doing it until something changes. We can divide our code into functions (similar to mathematical functions) that take inputs and return outputs, to allow us to re-use code.



To start a program, you need a main function This is a function that takes arguments from the command line (if you aren't using the command line, the arguments are empty) and starts everything up. In C++, you declare the main function like this:



int main(int argv, char** argc)



int means that the function returns an integer number when it's done. Typically, it will return 0 if everything went ok. It will return a non-zero number: that's the error code you get when a program crashes. "main" is the name of the function.



Then, you get to the function arguments. This function has two: int argv and char** argc. argv is just a number telling you how many arguments there are. argc is rather complicated to explain exactly what it means, but suffice to say, it is a list of arguments ("char" means character- each word is made up of a list of characters, and then you have a list of those lists of characters).



Inside the body, you can do other things:

----------

//by the way, this is a comment

//I'll use them to tell you what's going on





//This statement right here is a special instruction

//it allows us to link with other people's code.

//in this case, I'm using the Input/Output library that

//comes with C++

#include



//and now here's our function

//the curly brace, {, says to start a block

int main(int argc, char** argv) {

     //This, by the way is a comment- it starts with

     //two slashes



     //and here we have basic math.

     //we add 1 and 2 and store it in a

     //variable called 3

     //statements end in a semicolon

     int three = 1 + 2;



     //and now here we have a conditional

     //we have to use two equals signs

     // because one means assignment

     //all blocks start with braces

     if(three == 3) {

          //This writes to the console,

         std::cout << "Yay! We Know Math.";

     } else {

          //this only gets executed

          //if the condition was wrong

          std::cout << "Wow we suck";

     }

}





Of course ,there's so much more you can do with programs, but this is just sort of a quick, simple program for you to see how it works.
anonymous
2011-02-05 09:18:25 UTC
It's not as complicated as knowing a foreign language. Languages typically have key words that are recognized by a compiler (an application you write programs in) and perform certain tasks with data by utilizing memory.

For example a key word in the C language is printf. You use printf to print text or anything you want to the screen. Usually, the first program any learning programmer does is the "Hello World!" program.

All it is is the printf statement that prints "Hello World!", the entire program looks like this: (In C language)





#include



void main (void)

{

printf("Hello World!");

}





All of these key words and statements have different formats (syntax) for information you give the program so that it does what you want it to do. So pretty much yes, you either know the code or you don't, like a language. Once you get past the basics, things can get very complicated and frustrating. You can create things accidentally, like infinite loops that cause the program to crash. Or you can cause debugging errors. If you write code but use incorrect syntax, the compiler will not understand and show and error for syntax. Code has to be exact.

But the feeling of finishing a program and having it work beautifully is great.
peteams
2011-02-05 12:30:25 UTC
Programming has more relation to mathematics than physics. If you look deeply into what a program is it turns out to be a mathematical proof. Different computer languages are like different branches of maths and different notations within those branches.



The same document written in two different natural languages will have roughly the same form and you'd be able to easily relate blocks of one version to blocks of another. All the words may be different, some of the sentences may be different, but perhaps all the paragraphs map one-to-one.



In maths two proofs of the same thing may be radically different, one consist of arranging symbols according to a certain grammar, the other perhaps consisting of a series of diagrams. The same axioms will underlay the proofs, but their expression can be totally obfuscated away.



Programming similarly has a set of fundamental things you need to be able to express (look up Turing Complete), but those can be achieved using radically different constructs. Ultimately a program running on your PC is translated into something the processor can execute, mapping the theoretical similarities between languages to something physical.
Moo
2011-02-05 09:13:50 UTC
There are different levels of programming, some require you to have a working knowledge of physics and mathematics while other don't, as much.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...