Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.

Similar presentations


Presentation on theme: "Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016."— Presentation transcript:

1 Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016

2 Data Types Each named variable must have a type C++ supports three simple types: IntegerReal or Floating-pointCharacter It is a whole number, either positive or negative. An integer value may be stored in an integer variable declared with the keyword int. You can also declare an integer variable using short int and long int. They are numbers that include decimal positions, such as 98.6, 1000.00002, and -3.85 They may be stored in variables with type float, double, and long double. It may be stored in variables declared with the keyword char. A character may hold any single symbol in the ASCII character set. Often it contains a letter of the alphabet, but it could include a space, digit, arithmetic symbol, or other special symbol. A character value is always expressed in single quotes, such as ‘A’ or ‘&’.

3 Examples #include using namespace std; int main() { int a=5; cout<<"a="<<a<<“\n\n”; return 0; } #include using namespace std; bool main() { bool b=true; cout<<b<<"\n"; cout<<!b<<endl; bool b1=false; cout<<b1<<"\n"; cout<<!b1<<endl; return 0; } #include using namespace std; char main() { char ch=97; cout<<ch<<"\n"; return 0; } #include using namespace std; void main () { string mystring; mystring = "This is a string"; cout << mystring; }

4 Declaring & Initializing Variables Variables can be initialized when declared:  int first=13, second=10;  char ch='';  double x=12.6; All variables must be initialized before they are used  But not necessarily during declaration

5 Examples #include using namespace std; int main() { int a; a=5; cout<<"a="<<a<<endl; return 0; } #include Using namespace std; int main() { int a=5; cout<<"a="<<a<<endl; return 0; }

6 #include using namespace std; int main() { int a; int b; int c; a=5; b=7; c=a+b; cout<<“a="<<a<<endl; cout<<“b="<<b<<endl; cout<<“c="<<c; return 0; } #include using namespace std; int main() { int a,b,c; a=5; b=7; c=a+b; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"c="<<c; return 0; } #include using namespace std; int main() { int a=5,b=7,c; c=a+b; cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<"c="<<c; return 0; }

7 Examples (Cont…) #include using namespace std; int main() { int a,b,c; cout<<“Enter first value"; cin>>a; cout<<"\nEnter second value"; cin>> b; c=a+b; cout<<"c="<<c<<endl; return 0; } #include using namespace std; int main() { int a,b; cout<<"Enter two numbers:"; cin>> a >> b; cout<<"\n"; cout<<"a+b="<<(a+b); return 0; }

8 Examples (Cont…) #include using namespace std; int main() { int a,b; int result; a = 5; b = 2; a = a + 1; result = a - b; cout<<result<<endl; return 0; }

9 Assignment When a variable is declared, space is allocated in the computer’s memory for the variable. When a variable is assigned a value, the value is placed into the variable’s memory location. 10 Total Total = 2 + 3 + 5;

10 Examples #include using namespace std; int g; int main() { int a=10, b=20; g = a + b; cout<<g<<endl; return 0; } #include using namespace std; int g=20; int main() { int g = 10; cout<<g<<endl; return 0; }

11 Const Qualifier A variable that does not change in a program should not be declared as a variable. Instead, it should be a constant. Const variables must be initialized when you define them, and then that value can not be changed via assignment. The statement const double MINIMUM_WAGE = 5.75; declares a constant named MINIMUM_WAGE that can be used like a variable, but cannot be changed during a program. Example: const int x=5;

12 Example #include using namespace std; int main() { const int a=5; int b=2; a = a + b; cout<<a; return 0; }

13 C++ Binary Arithmetic Operators Often after data values are input, you perform calculations with them. There are 5 binary arithmetic operators. Binary operators are operators that take a left and right operand.

14 Order of Precedence Arithmetic expressions are evaluated according to the following order of operations. At each level, operations are evaluated left to right: 1.Parenthesis 2.Multiplication, Division, Modulus 3.Addition, Subtraction When operators are on the same level, they are performed from left to right. 3 * 7 - 6 + 2 * 5 / 4 + 6 means (((3 * 7) – 6) + ((2 * 5) / 4 )) + 6

15 Examples #include using namespace std; int main() { int a = 20, b = 10, c = 15, d = 5, e; e = (a + b) * c / d; cout<<"value of e is: "<<e<<endl; e = ((a + b) * c) / d; cout<<"value of e is: "<<e<<endl; e = (a + b) * (c / d); cout<<"value of e is: "<<e<<endl; e = a + (b * c) / d; cout<<"value of e is: "<<e<<endl; return 0; }

16 Examples (Cont…) Program that uses two ways to produce 21. #include using namespace std; int main() { cout<<(12+9)<<endl; // displays the value of 21 int sum=12+9; // calculates sum whose value becomes 21 cout<<sum; // displays the value of sum return 0; }

17 Shortcut Arithmetic Operators C++ employs several shortcut operators. OperatorSymbolFormEquivalent Form Operation Addition+=x += yx = x + yAdd y to x Subtraction-=x -= yx = x - ySubtract y from x Multiplication*=x *= yx = x * yMultiply x by y Division/=x /= yx = x / yDivide x by y Modulus%=x %= yx = x % yPut the remainder of x / y in x

18 Example #include using namespace std; int main() { int a=5, b=2 ; a += b; // a = a + b; cout<<a<<“\n”; return 0; }

19 Mixed Expressions Mixed expression: Has operands of different data types Contains integers and floating-point Evaluation rules: If operator has same types of operands  Evaluated according to the type of the operands If operator has both types of operands  Integer is changed to floating-point  Operator is evaluated  Result is floating-point Entire expression is evaluated according to precedence rules

20 Example

21 Mixed Expressions (Cont…) In Figure 2-2, each operation is assigned to a result variable of the correct type. The expression a + b has an integer result because both a and b are integers, not because their sum is stored in the intResult variable. If the program contained the statement doubleResult = a+b; the expression a+b would still have an integer value, but the value would be cast, or transformed, into a double when the sum is assigned to doubleResult.

22 Mixed Expressions (Cont…) The automatic cast that occurs when you assign a value of one type to another is called an implicit cast. The modulus operator ( % ), which gives the remainder of integer division, can be used only with integers.

23 Example #include using namespace std; int main() { int number1, sum; float number2; cout<<“number1="; cin>>number1; cout<<"\nnumber2="; cin>> number2; sum=number1+number2; cout<<"sum="<<sum<<endl; return 0; }

24 Type Conversion (Casting) Printing chars as integers via type casting: If we want to output a char as a number instead of a character, we have to tell cout to print the char as if it were an integer. One (poor) way to do this is by assigning the char to an integer, and printing the integer:

25 Type Conversion (Casting) (Cont…) A type cast creates a value of one type from a value of another type. To convert between fundamental data types, we use a type cast called a static cast. The syntax for the static cast:  static_cast (expression)

26 Examples #include using namespace std; int main() { char ch='a'; cout<< ch <<endl; cout (ch) << endl; cout<< ch <<"\n\n"; return 0; }

27 Examples (Cont…)

28 Escape sequences C++ has some characters that have special meaning. These characters are called escape sequences. An escape sequence starts with a ‘\’ (backslash), and then a following letter or number.

29 Example #include using namespace std; void main() { cout<< "\"This is quoted text\"\n"<<"\n\n\n"; cout<< "This string contains a single backslash \\" <<"\n\n\n"; cout<< " My name start with \'N\'" <<"\n\n\n"; }

30


Download ppt "Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016."

Similar presentations


Ads by Google