Login
Copyright

Programming Logic & Syntax: The Programming Toolbox

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Using Pseudocode to Map Code

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 1:26 Statements
  • 1:56 Variables
  • 3:08 Keywords
  • 3:43 Controlling Workflow
  • 6:16 Comments
  • 7:03 Lesson Summary
Add to Add to Add to

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Login or Sign up

Timeline
Autoplay
Autoplay
Create an account to start this course today
Try it free for 5 days!
Create An Account

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Paul Zandbergen

Paul has a PhD from the University of British Columbia and has taught Geographic Information Systems, statistics and computer programming for 15 years.

A programming language has its own syntax that dictates how words and symbols can be put together to form a program. Learn about programming's use of statements, variables and keywords in this video lesson.

Programming Syntax

Programming is a creative process carried out by programmers to instruct a computer on how to do a task. A program is a set of instructions that tells a computer what to do in order to come up with a solution to a particular problem. The actual content of the program is called code. That is why programming is often referred to as coding or writing code.

Code is written in a language that a computer can understand. There are hundreds of different programming languages, each with their own logic and syntax. A programming language has its own syntax, which consists of the set of rules that dictate how words and symbols can be put together to form a program.

The concept of syntax in programming language is similar to the concepts of grammar and spelling in spoken language. When a sentence in English has very poor grammar and spelling, it becomes difficult or even impossible to understand. Similarly, when code has syntax errors, the program will not execute.

The difference is that when you read a sentence in English with a minor error, you can typically still understand its meaning. Very small syntax errors in code, however, make the program unusable. Coders, therefore, have to pay great attention to detail to make sure their code is not only logical but also free of syntax errors. There are a number of aspects to syntax, including statements, variables and keywords.

Statements

Statements are the basic unit of code. A statement can assign a value to a variable, perform a single action, control the execution of other statements and do any number of other things. A statement consists of a single line of code that does something. A very simple example of a statement is the following:

print 'Hello world!'

When executed, this print statement prints the text 'Hello world!' to the screen, just as you might have expected.

Variables

Programming languages use variables. You can think of a variable as a container in which you can store a value that can be retrieved at any time. In other words, variables store information. Let's consider a very simple example:

x = 17

The variable is x, and you assign the value of 17 to this variable. This is an assignment statement. Once you assign a value to a variable, you can use the variable to do something else. For example:

x * 2

Once you run this code, you get the result 34.

Variables can contain any valid data type, including strings, numbers, Boolean values, lists, arrays and others. For example, here is how you could use a string variable:

mytext = 'Hello world!'

Variable names can be just about anything, but they typically cannot start with a number since the first part of the variable would be confused with a number. Now you can do something with this string. For example, the following code:

print mytext

This will print the value of the variable 'mytext' to the screen, so 'Hello world!'

Keywords

Programming languages use keywords. A keyword is a term that has a specific meaning within that programming language. For example, the command 'print' in the previous example is a keyword. As you might have guessed, this keyword prints whatever comes after it to the screen. Another example of a keyword would be 'import,' used to import something so that it can be used in the program. Keywords are reserved, meaning that you cannot use them for anything else. For example, you cannot create a variable called 'import,' since this would get very confusing.

Controlling Workflow

You can think of a computer program as a series of statements that are executed line-by-line. By default, lines are executed in a simple sequence from the first line to the last line, unless there is a statement that tells the execution to jump to somewhere else in the code. A number of different statements are used to control the execution of part of the program.

A conditional statement allows for choosing between two or more execution paths. A conditional statement is also sometimes referred to as a selection statement or branching. For example, an 'if' statement is used to execute a block of code when a particular condition is true. If the condition is false, the block of code is skipped and the rest of the code in the program is executed. A variation on this is the 'if-else' statement. The block of code following the 'else' statement is executed if the 'if' statement is false.

Let's look at an example of an 'if-else' statement. Consider an ATM where someone wants to make a withdrawal of $100. This amount needs to be compared to the account balance to make sure there are enough funds. Here is the code:

x = 100

if balance < x:

print 'Insufficient balance'

else:

print 'Please take your money'

If the account balance is less than $100, the ATM prints the message 'Insufficient balance.' If the account balance is equal to or more than $100, the ATM prints the message 'Please take your money.' To keep the example simple, the other activities of the ATM are not included here, such as issuing bills and returning the bank card. They key point here is that only one of the two paths is executed based on a condition; the other path is skipped.

To unlock this lesson you must be a Study.com Member.
Create your account

Register for a free trial

Are you a student or a teacher?
I am a teacher

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back

Earning College Credit

Did you know… We have over 95 college courses that prepare you to earn credit by exam that is accepted by over 2,000 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it free for 5 days!
Create An Account
Support