Docsity

C++ Programming Basics: Understanding the Structure and Variables of a C++ Program, Slides of Computer Science

Toggle sidebar
bg1
C++ Programming Basics February 2,2006
1
Basics of C++ Programming
Basics of C++ Programming
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
February 2, 2006
2
Outline
• Review last lecture
• Basic structure of a C++ program
– Comments and statements
– White space not part of program
– Variables are case sensitive
– Collection of functions with execution
starting in a function called main
• Screen output and keyboard input
3
Review
• http://www.csun.edu/~lcaretto/comp106
– Structure of computer
• Programming in higher level language
like C++
– Compile, link, execute, debug
• Integrated development environment
• Design, code, test
– Program correctness essential
4
Basic C++ Programs
• A C++ program is a collection of
functions
– Execution starts in the main function
– Functions may be in more than one file
– Have comments and code
– Must include libraries
• Initial assignments will have only one
function, called main
5
A Note on Syntax
• When we describe programs we
distinguish between items that are
written as stated and descriptions of
other items that are required or optional
• Items written as stated will be in a non-
proportional font like this one
• Descriptions of other items will be in
bold italic in brackets
<like this>
• Example => Name:
<your name>
6
Basic C++ Program Structure
#include <iostream>
using namespace std;
int main()
{
<your program statements>
return EXIT_SUCCESS;
}
• May have other #include statements
pf3
pf4
pf5

Partial preview of the text

Download C++ Programming Basics: Understanding the Structure and Variables of a C++ Program and more Slides Computer Science in PDF only on Docsity!

Basics of C++ ProgrammingBasics of C++ Programming

Larry Caretto Computer Science 106

Computing in Engineering

and Science

February 2, 2006

2

Outline

  • Review last lecture
  • Basic structure of a C++ program
    • Comments and statements
    • White space not part of program
    • Variables are case sensitive
    • Collection of functions with execution starting in a function called main
  • Screen output and keyboard input

3

Review

  • http://www.csun.edu/~lcaretto/comp
    • Structure of computer
  • Programming in higher level language

like C++

  • Compile, link, execute, debug
  • Integrated development environment
  • Design, code, test
  • Program correctness essential

4

Basic C++ Programs

  • A C++ program is a collection of

functions

  • Execution starts in the main function
  • Functions may be in more than one file
  • Have comments and code
  • Must include libraries
  • Initial assignments will have only one

function, called main

5

A Note on Syntax

  • When we describe programs we

distinguish between items that are

written as stated and descriptions of

other items that are required or optional

  • Items written as stated will be in a non-

proportional font like this one

  • Descriptions of other items will be in

bold italic in brackets

  • Example => Name: 6

Basic C++ Program Structure

#include

using namespace std;

int main()

return EXIT_SUCCESS;

  • May have other #include statements

7

Purpose of Basic Structure

  • The #include statement

invokes the basic input/output library

  • The using namespace std; statement

defines standard names so you do not

have to declare them in your code

  • The int main() declares the start of the

main function and declares it to be of

type int (more on types next week)

8

Purpose of Basic Structure II

  • The opening and closing braces, { and

}, mark the start and end of the main

function

  • EXIT_SUCCESS is a symbolic constant

defined to have a value of zero

  • The return statement passes a value to

the calling function – in this case the

operating system

9

C++ Statements

  • C++ statements can have multiple lines
  • The end of a C++ statement is marked

with a semicolon (;)

  • White spaces are ignored in C++
  • w = x +

y; is the same as w=x+y;

  • C++ statements use program variables

that represent computer memory

locations

10

Program Variables

  • Look like mathematical variables but

they not

  • Program variables describe operations

on memory locations, e. g .,

  • Take the value from memory location 121
  • Add to this the value from location 237
  • Store the result in location 142
  • C++ writes this as w = x + y;

11

Programming vs. Math

  • Consider the following operations
    • y = 2;
    • x = y;
    • y = 3;
  • What is x at the end of this sequence?
    • In mathematics x would equal y, which is 3
    • In programming, we set the value of x to the value of y when y was 2; thus x is 2

12

Programming vs. Math II

  • What the statements on the previous

slide mean

  • y = 2; // assign a value 2 to the variable named y representing a memory location
  • x = y; // find the value of the variable named y and assign that value to x
  • y = 3; // assign a value 3 to y
  • Once x is set to the value of y (2), it is

not changed (in this sequence)

19

Input using cin

  • Input prompt tells user what to input
  • Enter several variables with a single cin command - Separate entries by a space and press (the Enter key) after last entry

Type 1.4 -3.2 12.7 to set x = 1.4, y = -3.2 and z = 12.

cout << “Enter x, y, and z: “; cin >> x >> y >>z;

cout << “Enter x: “; cin >> x; Type 2.3 for x = 2.

Code Actions

20

Output spacing

  • cout does not provide any spacing

between output or new lines

  • E.g., x = 13.2; y = 12.6; cout << x << y;

(or cout << x; cout << y;) would print

  • You can put a string of blanks in your

cout commands: cout << x << “ “ << y;

would print 13.2 12.

21

Escape sequences

  • Special characters to control printing

entered in strings

  • \n for new line (can also use endl)
  • \t for tab
  • \” for quotation mark
  • \ for backslash
  • E.g cout << x << “\t” << y << “\n”;
  • Or cout << x << “\t” << y << endl;

22

Equivalent Statements

cout << “\n radius = “ << r << “\ndiameter = “ << d << “\n area = “ << a; cout << “\n radius = “ << r << “\ndiameter = “ << d << “\n area = “ << a; cout << endl; cout << “ radius = “ << r << endl; cout << “diameter = “ << d << endl; cout << “ area = “ << a;

23

Equivalent Statements II

  • What is the output from these statements? double r = 1, PI = 3.14159265358979; double d = 2 * r; double a = PI * r * r; cout << “\n radius = “ << r << “\ndiameter = “ << d << “\n area = “ << a; radius = 1 diameter = 2 area = 3.

Default output is six significant figures with no trailing zeros 24

Output Errors

  • What is wrong with these statements?
    • cout << “Enter x: “; cin >> y;
    • cout << “x = “ << y;
    • cout << “x divided by y is “ << y / x;
    • cout << “ x minus y is “ << y – y;
  • Note that the above statements are not

syntax errors, but they are errors that

could lead to very confusing results.

25

Output Quiz

  • What is the output from these statements? double x = 3.1, y = 2.2; cout << “x = “ << y; cout << x << y; cout << x << “x = “; cout << “My name is << x”; My name is << x cout << “My name is \n“ << x;

x = 2. 3.12. 3.1x =

My name is