C++ Programming

Post on 27-Oct-2014

93 views 4 download

Tags:

Transcript of C++ Programming

C++ Programming

Revision

Hello World!

Basic fundamental data types

C++ operators

• Several classes of operators– Arithmetic– Relational – Logical – Assignment

Arithmetic operators• Seven arithmetic operators in C++ ( E.g. int a = 7, b = 2; )

Operators Action

- Substraction

+ Addition

* Multiplication

/ Division

% Modulus division

- - Decrement

+ + Increment

Expression Value

a - b 5

a + b 9

a * b 14

a / b 3

a % b 1

a-- 6

b++ 3

Arithmetic Operators#include <iostream>using namespace std;

int main(){ // Integers Expression

cout << "2 + 5 = "<< 2 + 5 << endl; cout << "4 * 3= "<< 4 * 3 << endl; // Floating-point expression cout<<"4.0 * 3.0= "<<4.0 * 3.0<<endl; cout<<"5.0 / 2.0= "<<5.0 / 2.0<<endl;

return 0;}

Relational operators• Six relational operators in C++• The results is either TRUE (1) or FALSE (0)• E.g. if a = 7 and b = 5, then a < b yields 0 and a != b yields 1

Operator Meaning

< Less than

<= Less than or equal

> Greater than

>= Greater than or equal

= = Equal

!= Not equal

Logical Operators• && (logical AND)– Returns true if both conditions are true

• || (logical OR)– Returns true if either of its conditions are true

• ! (logical NOT, logical negation)– Reverses the truth/falsity of its condition– Returns true when its condition is false

• Logical operators used as conditions in loops Expression Resulttrue && false falsetrue || false true!false true

Assignment Operators

• Most commonly used assignment operator is = (e.g. int Q = 5; )

• Assignment expression abbreviationsc = c + 3; can be abbreviated as c += 3; using the

addition assignment operator

• Examples of other assignment operators include:d -= 4 (d = d - 4)e *= 5 (e = e * 5)f /= 3 (f = f / 3)g %= 9 (g = g % 9)

Memory Variables• double sale;• int num1, num2;• char first;• string str;

• Variable? A memory location whose content may change during program execution.

• num1 = 4; num2 = 4*5-11;• sale = 0.02 * 1000;• first = ‘D’; str = “It is a sunny day.”;

Constants Variables

• const double CONVERSION = 2.54;• const int NO_OF_STUDENTS = 20;• const char BLANK = ‘ ‘;• const double PAY_RATE = 15.75;

• Constant? A memory location whose content is not allowed to change during program execution.

#include <iostream>using namespace std;

int main(){

const int NO_STUD = 20;

string str; str = “All are good students”;

cout<<“Num of students is “<<NO_STUD; cout<<str; str = “Some are so-so”; cout<<str;

return 0; }

Input (Read) Statement

int main(){ int feet, inches;

cout<<“Enter two integers separated by spaces: “; cin>> feet >> inches; cout<<endl;

cout<< “Feet = “<< feet << endl; cout<< “Inches = “<< inches << endl; system(“pause"); return 0;}

Increment & Decrement Operators

int main(){ int num = 0, num2 = 0, tot = 5;

num++; num2 = num2 + 1; cout<< num <<“ “<< ++num <<endl;

tot--; cout<< tot; system("Pause"); return 0;}

Lets Try!

• Write a program that prompts the user to enter five test scores and then prints the average test score. (Assume test scores are decimal numbers.)