Programming Chapter 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/35

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:03 AM on 6/14/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

36 Terms

1
New cards

//

Beginning of a comment

2
New cards

#

Beginning of preprocessor directive

3
New cards

<>

Enclose filename in #include

4
New cards

()

Used when naming a function

5
New cards

{}

Encloses a group of statements

6
New cards

“”

Encloses string of characters

7
New cards

;

End of programming statement

8
New cards

The cout Object

  • Displays output on the computer screen

  • You use the stream insertion operator («) to send output to cout

    • Ex. cout «“Programming is fun!”;

    • Can be used to send more than one item to cout

    • Ex. cout « “Hello” « “there!”; OR

    • cout « “Hello”;

    • cout « “there!”; which still produces one line of output

9
New cards

The endl Manipulator

  • You can use the endl manipulator to start a new line of output. This will produce two lines of output

cout « “Programming is” « endl;

cout « “fun!”;

  • DO NOT put quotation marks around endl

10
New cards

The \n Escape Sequence

  • You can also use the \n escape sequence to state a new line of output. This will produce two lines of output:

cout « “Programming is\n”;

cout « “fun!’’;

*\n is inside the string!

11
New cards

The #include Directive

  • Inserts the contents of another file into the program

  • A preprocessor directive, not part of C++ language

  • #include lines not seen by compiler

  • DO NOT PLACE A SEMICOLON AT THE END OF #include line

12
New cards

Variable

A storage location in memory

  • Has a name and a type of data it can hold

  • Must be defined before it can be used: int item;

13
New cards

Literals

  • a value that is written into a program’s code

    • String literal (“hello, there”)

    • 12 (integer literal)

14
New cards

Identifiers

A programmer-defined name for some part of a program (variables, functions, etc.)

YOU CANNOT USE ANY OF THE C++ KEY WORDS AS AN IDENTIFIER

Rules:

  • The first character of an identifier must be an alphabetic character or an underscore

  • After the first character you may use alphabetic characters, numbers, or underscore

  • Upper- and lowercase characters are distinct

15
New cards

Defining Variables

Variables of the same time can be defined on separate lines (;;;) or the same line (,,;,;)

Variables of different types must be in different definitions

16
New cards

Integer Literals

An integer value that is typed into a program’s code (itemsOrdered = 15)

  • Stored in memory as int by default

    • To store in a long memory location, put ‘L’ at the end of the number: 1234L

    • To store in a long long memory location, put ‘LL’ at the end of the number: 324LL

    • Constants that begin with ‘0’ are base 8: 075

    • Constants that begin with ‘0x’ are base 16: 0×75A

17
New cards

True or false: A variable name has no relation to the purpose of the variable

False, a variable name should represent the purpose of the variable

18
New cards

The char Data Type

  • Used to hold characters of very small integer values

  • Usually 1 byte of memory

  • numeric value of character from the character set is stored in memory

    • Code: char letter; letter = ‘C’; Memory: letter 67

19
New cards

Character literals

  • Must be enclosed in single quote marks (‘A’)

20
New cards

Character Strings

  • A series of characters in consecutive memory locations (“Hello”)

  • Stored with the null terminator (\0) at the end

  • Comprised of the characters between the “” (H e l l o \0)

21
New cards

The C++ string Class

  • Special data type supports working strings (# include <string>)

  • Can define string variables in programs (string firstName, lastName;)

  • Can receive values with assignment operator (firstName = “George”; lastName = “Washington”;)

  • Can be displayed via cout (cout « firstName « “” « lastName;)

22
New cards

Floating-Point Data Types

Float, double, long double

  • Can hold real numbers such as: 12.45, -3.8

  • Stored in a form similar to scientific notation

  • All floating-point numbers are signed

23
New cards

Floating Point Literals

  • Can be represented in Fixed point (decimal) notation (31.4159) or E notation (3.14159E1)

  • Are double by default

  • Can be forced to be float (3.14159f) or long double (.0000625L)

24
New cards

The bool Data Type

  • Represents values that are true or false

  • bool variables are stored as small integers

  • False is represented by 0, true by 1 (bool allDone = true; returns allDone 1)

25
New cards

Determining the Size of a Data Type

  • the sizeof operator gives the size of any data type or variable

26
New cards

Variable assignments and initialization

  • An assignment statement uses the = operator to store a value in a variable (item= 12; assigns the value 12 to the item variable)

  • Assignment= the variable receiving the value must appear on the left side of the = operator.

27
New cards

True or false: this will work:

12 = item;

False, the variable receiving the value must appear on the left side of the = operator

28
New cards

Variable Initialization

  • to initialize a variable means to assign it a value when it is defined (int length = 12;)

  • Can initialize some or all variables (int length = 12, width = 5, area;)

29
New cards

Declaring Variables with the auto Key Word

  • C++ 11 introduces an alternative way to define variables, using the auto key word and an initialization value (auto amount = 100; is an int)

  • The auto key word tells the compiler to determine the variable’s data type from the initialization value.

30
New cards

Scope

  • The scope of a variable is the part of the program in which the variable can be accessed

  • A variable cannot be used before it is defined

31
New cards

Arithmetic Operators

  • Used for performing numeric calculations

  • C++ has unary, binary, and ternary opterators:

    • Unary (1 operand): -5

    • Binary (2 operands): 13 -17

    • Ternary (3 operands): exp1 ? Exp2 : exp3

  • / (division) operator performs integer division if both operands are integers

    • If either operand is a floating point, the result is a floating point

  • % (modulus) operator computes the remained resulting from integer division

    • requires integers for both operands (NO FLOATING POINTS)

32
New cards

Comments

  • Used to document parts of the program

  • Intended for persons reading the source code of the program

    • Indicate the purpose of the program

    • Describe the use of variables

    • Explain complex sections of code

  • Are ignored by the compiler

33
New cards

Single-Line Comments

Begin with // through to the end of the line (Int length = 12; //length in inches OR // calculate rectangle area)

34
New cards

Multi-Line Comments

  • Begin with /*, end with */

  • Can span multiple lines

  • Can begin and end on the same line

35
New cards

Named Constants

  • Aka constant variable

  • A variable whose content cannot be changed during program execution

  • Used for representing constant balues with descriptive names

  • Often named in uppercase letters

  • “ const double TAX_RATE = 0.0675;

36
New cards

Programming Style

  • The visual organization of the source code

  • Includes the uses of spaces, tabs, and blank lines

  • Does not affect the syntax of the program

  • Affects the readability of the source code