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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Programming in C.
Lesson complete
Earn the 3 college credits for Programming in C
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Writing Pseudocode for Program Design →← Core Program Structure: Input, Processing, and OutputNext lesson: Writing Pseudocode for Program Design →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every course with PRO →
Compare: C vs C++
Why take it here
Why students take Programming in C with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
How do you read a C compiler error message?
How does C represent true and false?
What are the AND, OR and NOT operators in C?
About the Programming in C course
How many college credits is Programming in C?
How much does Programming in C cost and what is included?
Which universities accept UPI Study credits?
How long does Programming in C take?
How are the exams done?
How do my credits reach my college?
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.
3 credits · NCCRSEnroll →