Programming Logic and Syntax, Explained with Code Examples

UPI Study › Courses › Programming in C › Free lessons › Lesson 1.3 Free with a free account

Programming Logic and Syntax

Programming in C · Chapter 1: Programming Fundamentals, Pseudocode, and Program DevelopmentSection 1.3 · lesson 3 of 788 min · free with a free account

← Core Program Structure: Input, Processing, and OutputNext lesson: Writing Pseudocode for Program Design →

Earn 3 college credits for Programming in CRecommended for credit by NCCRS · Computer Science 240 · self-paced · one payment, lifetime access

In this lesson

Tell syntax errors from logic errors and use Boolean logic in C.

  • Distinguish syntax errors caught by the compiler from logic errors caught by testing
  • Read a compiler error message by its location, kind and caret
  • Explain how C represents true and false with numbers
  • Evaluate AND, OR and NOT using a truth table

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,097 words). Read along, or search it.

Opening Puzzle

Broken Grammar or Broken Meaning

Points nine fifty scored Sam. Every word is real, but the sentence is broken. The scoreboard ate Sam. The grammar is perfect, but the meaning is nonsense. Code can go wrong in exactly these two ways. One is caught before your program ever runs. The other runs happily, and gives you the wrong answer.

Two Kinds of Rules

Syntax Is Grammar, Logic Is Meaning

Every language has syntax: the grammar rules that say how words and symbols may be put together. Code also has logic: what the instructions actually mean and do. Code with perfect syntax can still have broken logic. And here's the key point. The compiler checks only the grammar. Only you, and a good tester like Sam, can catch a logic error.

C Grammar Rules

Semicolons, Pairs and Case

C's grammar is strict, and the compiler checks every character. Three rules show up in every program. A statement, one complete instruction, ends with a semicolon, the way an English sentence ends with a period. Brackets come in pairs: round ones, curly ones, and the double quotes around text. And C is case-sensitive: printf with a small p and Printf with a capital P are two different names. Break a rule, and you get a syntax error. The compiler stops, explains what confused it, and makes no executable at all.

Reading an Error

One Missing Semicolon on Line Five

Here's a score program with one missing character. Can you find it, before the compiler does? Compile it, and Clang answers with an error. Notice there's no run command after it: with a syntax error, there's nothing to run. The message looks busy, but it always has the same parts. First, where: the file, line five, column thirty-nine. Then the kind, error, and what went wrong: expected a semicolon after expression. Below that, it copies line five and puts a caret at the exact spot. It even prints the fix: a semicolon, right there.

Debugging Habits

Check the Line Above, Fix the First

Two habits help. The named line is where the compiler noticed the problem, which isn't always where you made it. If it looks perfect, check the line above. And when it lists several errors, fix the first one and compile again. The others are often side effects of it.

Logic Error

An Average of One Hundred Twenty-Five

A logic error is a mistake in what the code means. The grammar is fine, so the compiler accepts it, and the program runs. Priya, who writes the arcade's software, wants the average of Sam's two rounds: eighty and ninety. The answer should be eighty-five. The program compiles with no warning at all, and prints: average score, one hundred twenty-five. That can't be right: an average can never be bigger than both numbers. The line says what Priya typed, not what she meant.

The Fix

Parentheses Before Dividing by Two

Like a math teacher, C divides before it adds. So it worked out ninety divided by two first, and added eighty. The fix is one pair of parentheses: round one plus round two, in brackets, then divided by two. Now it prints eighty-five.

Common Mistake

It Compiled, So It Works

The common mistake is thinking: it compiled, so it works. The compiler checks grammar, not meaning. It has no idea what an average is. The only way to catch a logic error is to know the right answer ahead of time, and compare. That's exactly what Sam does with every program.

Quick Check

An Average of Five Hundred Fifty

Quick check. A program compiles and runs. A player scored three hundred and five hundred, but it reports an average of five hundred fifty. Is that a syntax error, a logic error, or no error at all? Pause, and decide. It's a logic error. It compiled and ran, so the grammar is fine; a syntax error would have stopped the build. And it's certainly an error: an average can't be bigger than both scores. The right answer is four hundred.

Boolean Values

True Is One, False Is Zero

Now the answers behind every decision. Is this a new high score? Does the player have a token? Each question has only two answers: true or false. A value that can only be true or false is called a Boolean value, after George Boole, an English mathematician of the eighteen hundreds. Here's the C twist: C works with numbers. A comparison gives back one when it's true, and zero when it's false. And when C needs a yes-or-no answer, it treats zero as false, and any other number as true.

Truth in C

Any Nonzero Value Counts as True

Two counters show it. Tokens holds three, which isn't zero, so the message for tokens prints. Lives holds zero, so its message never appears. Newer C can also write the words true and false, but underneath, they still mean one and zero.

Boolean Operations

AND, OR, NOT and Truth Tables

Simple answers combine with three Boolean operations. AND is true only when both parts are true. OR is true when at least one part is true. And NOT flips an answer: not true is false. The arcade has two birthday rules: a gold badge for a birthday AND a score over nine hundred. A free game for a birthday OR a score over nine hundred. A table listing every combination is a truth table. Both true: badge and game. Birthday only: no badge, but a game. Score only: no badge, but a game. Neither: nothing. In C, you'll write these as two ampersands, two bars, and an exclamation mark.

Real Bug

OR Written Where AND Belonged

On the first day, Sam noticed every birthday player walking away with a gold badge, even with a score of a hundred twenty. Priya's code compiled perfectly. She had written OR where the rule said AND: one word, and a logic error only a tester could catch. Now Sam checks every row.

Quick Check

The Warning Light Puzzle

Quick check. A machine's warning light is on when the machine is NOT working, OR when it's out of tickets. Right now the machine is working, but it's out of tickets. Is the light on, off, or can't you tell? Pause, and decide. It's on. Not working is false, because the machine works. But out of tickets is true, and OR needs only one true part. Off would be the answer for AND, which needs both. And you can always tell: a truth table has a row for every case.

Key Takeaways

Grammar, Meaning and Truth Values

To sum up. Syntax is grammar; break it, and the compiler stops with an error you can read. Logic is meaning; a logic error compiles and runs, so you catch it by comparing with the right answer. In C, a comparison gives one or zero, and any nonzero value counts as true. AND needs both parts true, OR needs one, and NOT flips the answer.

Think About It

A Closed Sign Rule With NOT

Something to think about. Write one rule for the arcade that uses NOT, such as: show the closed sign if the arcade is NOT open. Then fill in a two-row table: open is true, then false. What does the sign do in each row?

About Programming in C at UPI Study

Programming in C (Computer Science 240) is a self-paced online course of 78 video lessons in 11 chapters, from pseudocode and your first program to data types, decisions, loops, functions, arrays, strings, pointers, files, structures, dynamic memory and linked lists. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: C vs C++

Why take it here

Why students take Programming in C with UPI Study

How programs work close to the machineProgramming in C teaches how a program uses memory: variables and data types, control flow, functions, arrays and strings, pointers, files, structures and dynamic memory. C is the base for operating systems, embedded devices and many computer-science courses.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Programming in C carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Programming Logic and Syntax

What is the difference between a syntax error and a logic error in C?
A syntax error breaks the grammar rules of C, while a logic error is valid code that does the wrong thing. The compiler catches syntax errors, such as a missing semicolon, and makes no executable. A logic error compiles and runs but gives a wrong answer, like writing a + b / 2 for an average. Logic errors are found by comparing output with an answer worked out beforehand.
How do you read a C compiler error message?
A compiler error message gives the location, the kind of problem, and a description. Clang starts with the file name, line and column, then says error or warning, then explains what it expected, such as a semicolon after an expression. Below that it repeats the line with a caret under the exact spot. The named line is where the compiler noticed the problem, so if it looks correct, check the line above.
How does C represent true and false?
C represents truth with numbers: a comparison gives 1 when true and 0 when false. When C needs a yes-or-no answer, as in an if condition, it treats zero as false and any other value as true. So a variable holding 3 counts as true, and one holding 0 counts as false. The words true and false in newer versions of C still stand for 1 and 0.
What are the AND, OR and NOT operators in C?
AND, OR and NOT are the Boolean operators, written in C as &&, || and !. AND is true only when both parts are true. OR is true when at least one part is true. NOT flips a value, so not true is false. A truth table lists every combination, and for two inputs it has four rows. Writing OR where a rule needs AND still compiles, but it creates a logic error.

About the Programming in C course

How many college credits is Programming in C?
Programming in C (Computer Science 240) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Programming in C cost and what is included?
$250, paid once, with lifetime access: all 78 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Programming in C take?
It is self-paced with no deadlines. The 78 lessons total about 9 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Programming in C.

78 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Programming in C
3 credits · NCCRS
Enroll →