➤8.1 Programming basics
• A program starts in main(){} and ‘return 0’ ends the program
• Each statement ends with a semicolon ‘;’
• Each variable must be initialized
• An input is commonly gotten from a users input on the keyboard or files.
• cin provides a standard input stream, this is followed by ‘>>’
• cout provides a standard output stream, this is followed by ‘<<’
• In order to output text you simply type the desired text in between double quotes “ ”
• The statement cout << endl starts a newline. The newline character ‘\n’ also puts out a new line
• For outputting multiple things you can split the items up with <<
➤8.2 Comments and whitespace
• Text the programmer adds to a code for a better understanding but ignored by the compiler.
• Single-line comment starts with ‘//’ followed by the desired text
• Multi-line comment (a.k.a. block comment) starts with ‘/*’ and ends with ‘*/’ with text in between
• Refers to blank spaces between items within a statement and blank lines between statements.
• It’s good practice to separate different parts of code with whitespace to make it more readable.
• Indent lines the same amount, align items and use a single space before and after operators.
➤8.3 Errors and warnings
• Syntax error/compile-time error: To violate a programming language's rules on how symbols can be
combined to create a program. A compiler generates a message when encountering such an error, telling
the programmer where and what is missing.
• But compiler error messages can be wrong, as it gives a best guess about the problem.
• Always focus on the first error first since this error could’ve caused the following errors.
• Logic error/bug: An error that is not picked up by the compiler since it’s technically correct but not in
such way the programmer meant. A warning indicates a possible logic error.
• -Wall will give the programmer more possible warnings, using this is considered good practice.
➤8.4 Computers and programs in general
• Bits are either 0’s or 1’s
• Back in the day people would connect switches to form circuits. Circuits called processors would process
instructions, which were stored in memory(a circuit that stores 0’s and 1’s).
• Instructions consists of 3 parts: location, what to do and where to store the result
• A sequence of instructions is called a program, application or app.
• High-level languages were created, allowing programmers to use ‘normal’ characters instead of just 0’s
and 1’s. Compilers would transfer this to executable programs(0’s and 1’s).
, Chapter 9 : Variables / assignments
➤9.1&9.2 Variables and assignments
• Variable: A named item used to hold a value.
• Assignment: Assigns a variable with a value, the variable keeps that value until it’s changed.
• Variable declaration: A statement that declares a new variable, specifying the its name and type.
• The processor writes the assigned value into the variable's memory location.
• Assignment statement: Assigns the variable with a value.
• You can also directly assign a value to a variable while declaring it.
• A common error is to read a variable before declaring it or writing the assignment in reverse.
➤9.3 Identifiers
• Identifier: A name created by a programmer for an item like a variable or function.
• An identifier must:
- Be a sequence of letters, underscores or digits
- Always begin with a letter or underscore
• It’s case sensitive and the identifier can’t have the same name as a reserved word/key word.
• It’s good practice to give it meaningful names and to minimize use of abbreviations.
➤9.4&9.5 Arithmetic expression
• Expression: A combination of items that evaluates to a value.
• A literal is a specific value. Operators are symbols with a built-in calculation function.
• The precedence rules of expressions work just like the math rules.
• It’s considered good practice to use spaces between the variables and operators.
• Compound operators are used as a shorthand way to update the variable. E.g. ‘+=’
➤9.7&9.8 Floating-point numbers (double)
• Type ‘double’ stores a floating-point number. A floating-point literal is a number with a fractional
part. It’s considered good practice to have a non-zero digit before the decimal point.
• The variable type should be chosen wisely, looking at which type you will use.
• Dividing a nonzero floating-point number by zero results in ‘inf’ or ‘-inf’ standing for infinity.
• If the numbers are both 0’s, the compiler will put out ‘nan’, since it’s not a number.
• Scientific notation is useful if the number is much bigger or much smaller than usual. It’s considered
good practice to have the leading digit to be non-zero.
➤9.9 Constant variables
• It’s good practice to minimize the use of literal numbers in code.
• Some variable are meant to be constant, so if you use the keyword ‘const’ the compiler will give an error
when the programmer wants to change the variable later.
➤9.10 Using math functions
• Standard math libraries like ‘cmath’ or ‘math.h’ allow the
programmer to use math functions.
• Such functions take arguments that appear in ().
• It’s common that an argument of a function call also has a
function call itself.
Commonly used math functions
➤9.11 Integer division and modulo
• When operands are integers, the division operator does not generate the fraction.
• For integer division, the second operand of / or % must never be 0, because division by 0 is
mathematically undefined it will cause an error.
• The modulo operator ‘%’ gives the remainder of an integer division.
➤9.12 Type conversions
• A type conversion changes one type to another. An automatic one is known as implicit conversion.
• For arithmetic operators both operands will become doubles. Whereas for assignments, the right side will
be converted to the type of the left side.
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller syntryx. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $8.03. You're not tied to anything after your purchase.