Core Programming Concepts - MDHSRobotics/TeamWiki GitHub Wiki
Home / References / [Introduction to Programming - Part 1](Introduction to Programming - Part 1)
#Core Programming Concepts
-
A program is a set of instructions that tell a computer or machine what to do
-
All programming languages have keywords and syntax. These are special words that are specific to the language and are the basis of the programming language you use. Java examples:
- primitive types: int, byte, boolean, double
- control flow: for, while, return
- conditional: if, else
- OO: new, extends, implements
-
Data is stored in variables
- Java and may other programming languages have primitive types and also Object types
- primitive type examples: int, byte, boolean, double
- Java and may other programming languages have primitive types and also Object types
int grade;
- Similar data can be grouped in collections
- arrays, Java Collections
java int[] grades;
- Most languages define operators which define language specific operations to be applied to variables * mathematical operators: +, -, *, / * logical operators: &&, ||, ! * conditional operators: >, <, ==, <=, >=, !=
- Most languages provide developers with the ability to add comments to the code * java single line comment: // * java comment block (can be multiline): /* ... */
// my really helpful comment line
/*
some really helpful
and even more valuable information
that helps others understand my code and
helps me remember what I was trying to do
*/
- A statement is a line of code, a specific instruction for the computer to execute. Examples:
- declare variable
int grade;
int grade=90;
- assign a value to a variable
grade=85;
grade=grade+grade*0.1;
- Statements can be grouped together into a code block
{
int grade=90;
grade=grade+grade*0.1;
calculateGrade(testAnswers);
}
- Most languages a have functions, sometimes called methods, which define a code block to be executed when it is called in a statement. Functions often take one or more parameters and often return a result
// a statement that calls a function with one parameter and returns a value
grade = calculateGrade(testAnswers);
// a method or function definition
public double calculateGrade(String[] testAnswers){
double score = 0;
// code statements that calculates how many answers are correct and returns a score
// ...
return score;
}
- Most programming languages support conditional logic
- if something is true, then do something, otherwise, do something else
if(grade>=90){
return "A";
}
else if(grade>=80){
return "B";
}
else if(grade>=70){
return "C";
}
else if(grade>=60){
return "D";
}
else{
return "F";
}
- Most programming language support control flow:
- examples: for loops, while loops
- 3 parts: initialization, condition, update
for(int i=0;i<limit;i++){
grades[i] = calculateGrade(testAnswers[i]);
}
boolean done = false;
...
while (!done){
doSomething();
done = amIDone();
}
- Java and other OO programming languages define objects
- an encapsulation of variables (properties) and functions (methods or behaviours)
- model real world concepts
- why? do to cool things
- polymorhism, encapsulation, inheritance, etc.
public class IPod{
//private properties
private Song[] playList;
private int currentSong;
//constructor
public IPod(Hashtable<String,Song> playList){
this.playList= playList;
int currentSong = 0;
}
//method to play current song
public play(){
stream(playList[currentSong]);
}
//method to skip to the next song and play it
public next(){
currentSong = currentSong + 1;
play();
}
}