










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to programming basics using c++. Topics covered include problem solving steps, programming concepts, comments, variables, data types, constants, assignment statements, input and output, and arithmetic expressions. It also includes examples and explanations of various programming concepts.
Typology: Study notes
1 / 18
This page cannot be seen from the preview
Don't miss anything!











Programming Basics Problem Solving Programming Comments Variables Data Types Constants Assignment Statements Input and Output Arithmetic Expressions Operator Precedence Rules Basic Program Structure Software Engineering Tips Initial Sphere Program Final Sphere Program Initial Statistics Program Final Statistics Program
Problem Solving Steps Understand the problem to be solved Decompose problem into pieces you know how to solve Write computer instructions for each piece Combine pieces into a single program Compile, test, and debug program Use program to solve initial problem
Comments Comments are used to explain program purpose and methods used Comments are human readable, but ignored by compiler Should write comments when designing program Should also add comments explaining major changes Two types of comments supported /* Old style C comment that can span multiple lines / // New C++ comment that goes to the end of the line Pay attention: / */ style comment cannot be nested.
Variables We need to store and access data to perform calculations Can allocate space in computer for data by declaring a variable Syntax is: data_type variable_name; (data_types are listed below) Should choose meaningful names for variables (ie "Age" not "A") Variable names must start with upper or lower case characters variable names may contain numbers and selected special characters (underscore) C++ is case sensitive so "Num" is a different variable from "num" Examples of variable names: a, a3, a_3, ab, a_b, bbb_,
Constants Certain variables have values which will never change Can use constants to store these values Define constants by adding reserved word const before declaration Also need to provide value at declaration time Can have constants of any variable data type Constant declarations must end with a semi-colon // Constant example const int SILLY = 42; const float PI = 3.1415926535; const char YES = 'Y'; Pay attention: If you have the following statements in a program: const int c=5; c=7; You will have the following compiler error when you try to compile the code: assignment of read-only variable ‘c’.
Assignment Statements Can copy one variable to another using the assignment operator = Variable on left of = is destination, where data is copied to Value on right of = is source, where data comes from Data values should be of the same type Conversion will occur if values are not of same type Assignment statements must end with a semi-colon // Assignment example int value; int number; float data; int x, y, z; bool flag; data = 2.158; // data variable now equals 2. value = 17; // value variable now equals 17 number = value; // number variable now equals 17 data = SILLY; // data variable now equals 42. number = PI; // number variable now equals 3 x = y = z = 0; // multiple assignments on one line flag = false; // flag is now set to false Pay attention: You cannot give an integer variable a float value. For example: int a; a = 3.5; // a will have a value 3
Arithmetic Expressions Often need to combine variables to get new values Calculations can be expressed as arithmetic expressions The arithmetic operators are:
Operator Precedence Rules Values are computed using "natural" operator precedence rules Parenthesized expressions ( ) evaluated first from inside out Multiplication, division, modulo have high precedence Addition, subtraction have lower precedence High precedence operations computed before low precedence Values calculated left to right at same precedence level Number the order of operations by hand for debugging // Complex expressions number = silly * value - number; data = (number + value) * silly; value = PI / data * PI; cout << 1+2-3+4-5+6; number = (1+2)-((3+4)-(5+6));
Software Engineering Tips Try to pick variable names which are meaningful Always initialize variables before you use their values You should write your comments before you write your code Add to your program a little at a time (grow your solution)
Initial Sphere Program //--------------------------------------------------- // Purpose: Program to calculate the volume and area // of a sphere of a given radius. // Author: //--------------------------------------------------- int main() { // Local variable declarations // Read input parameters // Calculate volume // Calculate surface area // Print output return 0; }
Initial Statistics Program //--------------------------------------------------- // Purpose: Program to calculate mean and variance // of five input values. // Author: //--------------------------------------------------- int main() { // Local variable declarations // Read input parameters // Calculate mean // Calculate variance // Print statistics return 0; }
Final Statistics Program //--------------------------------------------------- // Purpose: Program to calculate mean and variance // of five input values. // Author: //--------------------------------------------------- #include