1 / 14

C++ Basics

C++ Basics. Csci 107 Lecture 8. A C++ program. //if necessary include headers //#include <foo.h> 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

bashton
Télécharger la présentation

C++ Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Basics Csci 107 Lecture 8

  2. A C++ program //if necessary include headers //#include <foo.h> 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 <variable-name> will be a variable of type <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 <variable-name> 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 <variable-name> 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 True False if (condition) { S1; } else { S2; } S3; condition S2 S1 S3

  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 <iostream.h> 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 True False while (condition) { S1; } S2; condition S1 S2

  11. While example //read 100 numbers from the user and output their sum #include <iostream.h> 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 <array-name> which contains <size> elements of type <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 <iostream.h> 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 the average of {3, 1, 55, 89, 23, 45} etc

More Related