lec02 - CSC 1520 Computer Principles and C+ Programming C+...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 1520 Computer Principles and C++ Programming C++ Basics (Part I) Fall, 2009
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline 1. Language Syntax 2. Variables and Assignment Operators 3. Naming Variables Identifiers Reserved Words Predefined Identifiers 4. Data Types, Numeric Constants, String Literals 5. Console Input
Background image of page 2
3 1. Language Syntax Every programming language has its own syntax (defined by a set of rules) Like grammar and spellings in English These rules are very strict and must be obeyed. A program cannot be compiled successfully if it contains syntax errors.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 #include <iostream> using namespace std; int main() { Cout << "Good morning!" ; Cout << "\n" ; return 0; } Can you spot the error(s)? 1 2 3 4 5 6 7 8 9 Some Basic Rules C++ is case sensitive . Multiple whitespace characters are treated the same as one whitespace character. Space , newline ("Enter"), and tab characters are all considered as whitespace characters
Background image of page 4
5 2. Variables A variable is used to hold a value in a program. It corresponds to a location in the memory. A variable has a name . We use the name to specify which variable to use. A variable has a type . The type determines what kind of value the variable holds. 100 num You can treat a variable as a box that stores data.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 #include <iostream> using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10 ; integer2 = 20 ; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n" ; return 0 ; } 10 + 20 = 30 A C++ program that adds two numbers and output their sum 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Background image of page 6
7 2.1. Declaring Variables Variables must be declared first before they can be used in the program to store data. Syntax type2 var1, var2, …, varN; type1 variable1; Declaring multiple variables of the same type Declaring a single variable of type type1
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 #include <iostream> using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10 ; integer2 = 20 ; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n" ; return 0 ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ? ? ? sum integer1 integer2 Declared but uninitialized variables contain unknown values.
Background image of page 8
9 #include <iostream> using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10 ; integer2 = 20 ; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n" ; return 0 ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ? 10 ? sum integer1 integer2 Store 10 in integer1
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 #include <iostream> using namespace std; int main() { int sum; int integer1, integer2; integer1 = 10 ; integer2 = 20 ; sum = integer1 + integer2; cout << integer1 << " + " << integer2 << " = " << sum << "\n" ; return 0 ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ? 10 20 sum integer1 integer2 Store 20 in integer2
Background image of page 10
11 2.2. Assigning Values to Variables We use an assignment operator ( = ) to copy/assign a value to a variable. Syntax
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 35

lec02 - CSC 1520 Computer Principles and C+ Programming C+...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online