Understand These, and You’ve Got Everything You Need to Start Writing Programs in Any Language
This article will be give You’ve Got Everything You Need to Start Writing Programs in Any Language.
while(x < 10) {
x = x + 1;
print(“hello”);
}
Notice that we used the variable x as a loop counter, to control the number of times the loop runs. The other most common type of loop, the for loop, is basically just a while loop with a built-in loop counter. You tell the loop right away how many times you want it to run, like this:
for(int x; x < 10; x = x + 1) {
print(“hello”);
}
The part after “for” just defines a counter. It says “start with a number (integer) we’ll call x, and keep looping as long as x is less than 10. At the end of every loop, increment x by one.”
void PrintHellothenGoodbye()
{
print(“hello”);
print(“goodbye”);
}
Then, if you called that function three times in your code, as follows:
PrintHellothenGoodbye();
PrintHellothenGoodbye();
PrintHellothenGoodbye();
Your program would output “hello goodbye hello goodbye hello goodbye.” A function can also take variables as inputs, and return an output value. So, for instance, you could write a function that takes a number as an input, and returns that number squared. It would like look like this:
int square(int tosquare)
{
return tosquare * tosquare;
}
Notice the “return” keyword. That passes the following value back to whatever part of the code called the function. So, if somewhere else in the code we called the function like this:
print(square(5));
The program would print out the number 25.
SEMICOLON.
The semicolon is like the coding equivalent of a period each line of code ends with one. It’s important, because in many languages, line breaks are just for readability, and don’t have any effect on the execution of the code.
PARANTHESES
Parentheses are used after functions (see above) to contain that function’s parameters (or inputs). You might remember this usage from your high school math classes, when f(x) was a function that operated on the variable x.
CURLY BRACES
In a number of languages (particularly those derived from C), curly braces “{}” are used to enclose and group blocks of code. They’re used, for instance, after the control structures described on these pages (if statements, loops, and functions), to designate the block of code that the statement refers to.
INDENTATION
Because all of the control structures can benested inside each other, code tends to take on a sort of hierarchy. A particular line of code might be inside an if statement, which is inside another if statement, which is inside a loop that’s inside a function. All that can get hard to keep track of! To make it easier, code is written with variable levels of indentation. The more indented a line of code is, the more deeply nested it is. In most languages, indentation is purely for readability, but in a few (like Python), it actually controls the grouping of code, and is used instead of curly braces.
- Variables
- Conditional
- Loops
while(x < 10) {
x = x + 1;
print(“hello”);
}
Notice that we used the variable x as a loop counter, to control the number of times the loop runs. The other most common type of loop, the for loop, is basically just a while loop with a built-in loop counter. You tell the loop right away how many times you want it to run, like this:
for(int x; x < 10; x = x + 1) {
print(“hello”);
}
The part after “for” just defines a counter. It says “start with a number (integer) we’ll call x, and keep looping as long as x is less than 10. At the end of every loop, increment x by one.”
- Fungtions
void PrintHellothenGoodbye()
{
print(“hello”);
print(“goodbye”);
}
Then, if you called that function three times in your code, as follows:
PrintHellothenGoodbye();
PrintHellothenGoodbye();
PrintHellothenGoodbye();
Your program would output “hello goodbye hello goodbye hello goodbye.” A function can also take variables as inputs, and return an output value. So, for instance, you could write a function that takes a number as an input, and returns that number squared. It would like look like this:
int square(int tosquare)
{
return tosquare * tosquare;
}
Notice the “return” keyword. That passes the following value back to whatever part of the code called the function. So, if somewhere else in the code we called the function like this:
print(square(5));
The program would print out the number 25.
- Syntax
SEMICOLON.
The semicolon is like the coding equivalent of a period each line of code ends with one. It’s important, because in many languages, line breaks are just for readability, and don’t have any effect on the execution of the code.
PARANTHESES
Parentheses are used after functions (see above) to contain that function’s parameters (or inputs). You might remember this usage from your high school math classes, when f(x) was a function that operated on the variable x.
CURLY BRACES
In a number of languages (particularly those derived from C), curly braces “{}” are used to enclose and group blocks of code. They’re used, for instance, after the control structures described on these pages (if statements, loops, and functions), to designate the block of code that the statement refers to.
INDENTATION
Because all of the control structures can benested inside each other, code tends to take on a sort of hierarchy. A particular line of code might be inside an if statement, which is inside another if statement, which is inside a loop that’s inside a function. All that can get hard to keep track of! To make it easier, code is written with variable levels of indentation. The more indented a line of code is, the more deeply nested it is. In most languages, indentation is purely for readability, but in a few (like Python), it actually controls the grouping of code, and is used instead of curly braces.

0 komentar:
Posting Komentar