Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.

Similar presentations


Presentation on theme: "C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation."— Presentation transcript:

1 C++ Basics March 10th

2 A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation //print output to user } Notes: what follows after // on the same line is considered comment indentation is for the reader; compiler ignores all spaces and new line ; the delimiter for the compiler is the semicolon all statements ended by ; Lower vs. upper case matters!! –Void is different than void –Main is different that main

3 Variable declaration type variable-name; Meaning: variable will be a variable of type Where type can be: –int//integer –double//real number –char//character Example: int a, b, c; double x; int sum; char my-character;

4 Input statements cin >> variable-name; Meaning: read the value of variable from the user Example: cin >> a; cin >> b >> c; cin >> x; cin >> my-character;

5 Output statements cout << variable-name; Meaning: print the value of variable to the user cout << “any message “; Meaning: print the message within quotes to the user cout << endl; Meaning: print a new line Example: cout << a; cout << b << c; cout << “This is my character: “ << my-character << “ he he he” << endl;

6 If statements if (condition) { S1; } else { S2; } S3; condition S1 S2 S3 TrueFalse

7 Boolean conditions..are built using Comparison operators == equal != not equal < less than > greater than <= less than or equal >= greater than or equal Boolean operators && and || or ! not

8 Examples Assume we declared the following variables: int a = 2, b=5, c=10; Here are some examples of boolean conditions we can use: if (a == b) … if (a != b) … if (a <= b+c) … if(a <= b) && (b <= c) … if !((a < b) && (b<c)) …

9 If example #include void main() { int a,b,c; cin >> a >> b >> c; if (a <=b) { cout << “min is “ << a << endl; } else { cout << “ min is “ << b << endl; } cout << “happy now?” << endl; }

10 While statements while (condition) { S1; } S2; condition S1 S2 TrueFalse

11 While example //read 100 numbers from the user and output their sum #include void main() { int i, sum, x; sum=0; i=1; while (i <= 100) { cin >> x; sum = sum + x; i = i+1; } cout << “sum is “ << sum << endl; }

12 Arrays Used to store a collection of elements (variables) type array-name[size]; Meaning: This declares a variable called which contains elements of type The elements of an array can be accessed as: array-name[0],…array- name[size-1] Example: int a[100]; //a is a list of 100 integers, a[0], a[1], …a[99] double b[50]; char c[10];

13 Array example //Read 100 numbers from the user #include void main() { int i, a[100], n; i=0; n=100; while (i<n) { cout << “Input element “ << i << “: ”; cin >> a[i]; i = i+1; } //do somehing with it.. }

14 Problems Write a C++ program to read a sequence of (non-negative) integers from the user ending with a negative integer and write out the average of the numbers the smallest number the largest number the range of the numbers (largest - smallest) Example: –The user enters: 3, 1, 55, 89, 23, 45, -1 –Your program should compute X{3, 1, 55, 89, 23, 45}


Download ppt "C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation."

Similar presentations


Ads by Google