CSC 425 – INTROUCTION

TO COMPUTER
PROGRAMMING
CHAPTER 1.1 – LANGUAGE BASICS

Learning Objectives
What

is identifier, variables,
constants?
Input and Output
Basic mathematical,
relational and logical
operators
Basic control structures such
as sequence, selection and
noth/csc425 - ch2

2

ch2 3 . the body. the return statement and the last one is close curly bracket.h>  void main()  {  cout <<”\n Hello”. the main function. open curly bracket. Eg:  #include <iostream.  } noth/csc425 . always start with a comment. variable and constant? Basic structure of C++ programming.What is identifier.  return. includes header file (library).

Sample header files. math. the program will contain many errors. string.h.h.docx noth/csc425 . Eg : iostream.ch2 4 . EXAMPLE OF HEADER FILES.Header File  Each program must contain the header file or library.h.h. iomanip.h. conio. If the header file is not included.

  GetData.ch2 5 .Identifier A name which will be used to describe functions.  count---valid ◦ The blank space cannot be used.  box_22A.a-z). digits(0-9). Ex:  sum_of_squares. ◦ Ex Int noth/csc425 . variables. ◦ Only letters(A-Z.   Reserved words or keywords are identifiers reserved for system use. and other items. ◦ The rules for writing identifiers in C++ are: ◦ Identifiers must begin with a letter or underscore. constants. Ex: Get Data    cannot be an identifier as blanks are not allowed in identifiers ◦ Identifiers cannot be reserved words. or underscore( _ ) may follow the initial letter.

4 important types of variable : int. noth/csc425 .Variable Also referred to as identifier. A location in memory that holds changeable data. char float. float rate. char name[30]. It has a set of operations that can be used to change or manipulate them. eg     int number.ch2 6 . double Notation in giving the variable names :  ◦ Always start with alphabetic characters. double charges.

and "A" through "Z". ◦ Numbers .0 through 9."a" through "z".Variables An identifier name can be made up of only the following: ◦ Letters . Do not use reserved words such as "int" since it is indicate the type of the variable. Identifiers must begin with a letter or an underscore.ch2 7 . ◦ The underscore character "_". noth/csc425 .

volatile. long. extern. try. short. signed. if. Eg : asm. throw. this. unsigned. enum. protected. operator. template. Eg: 1marks. virtual. class. case. else. default. catch. double. inlineint. noth/csc425 . Variable cannot start with number.Rules of variable declaration: Start with alphabetic or underscore Don’t declare the same variable more than once. do. struct. continue. float. break. switch. while. Cannot use the reserved word to declare variable. sizeof. wchar_t. union. const. static. auto. public. private. void. typedef.ch2 8 . char. goto. register. friend. for. 4_age. delete. new. return.

compiler dependent) Boolean literal. you need string to add the statement: #include <string> to the start of your program. (7 digits or more compiler dependent) Double precision floating point number. noth/csc425 . In order to use this data type. int A 4 byte integer. long 4 or 8 byte integer (compiler dependent).char Variables Type A 1 byte integer usually used to store a character. (15 digits or more . short A 2 byte integer. Number of characters. Values are true (numerically=1) or false (numerically=0).ch2 9 . float double boolean Single precision floating point number.

142. Eg : const float pie = 3. noth/csc425 . ◦ Normally constant is applied to a variable which hold a value and the value is never change.ch2 10 .Constant A location in memory that holds unchangeable data.

etc.ch2 11 . b. Example: //Author : Aminah Bt Abu Bakar //Date : 20 Jan 2010 //Purpose : to calculate the total of two numbers   #include <iostream. purpose.Commenting The Program Comment is very important in any program. The purpose of commenting the program is to make us easier to refer and understand the function of the program.h> void main() { int a. b=6. a=5. Normally the program will consists like author’s name. cout << a+b. noth/csc425 . date program created.

30). Example of input ◦ Ex 1 : cin >> a. noth/csc425 .Input and output Statements The C++ identifier for input is cin and output is cout.get(name. ◦ Ex 2 : cin. Example of output ◦ Ex 1: cout << a. ◦ Ex 2: cout << “\n Enter your name: “.ch2 12 .

return. const int currentyear = 2007. cin>>year.h> void main() { char name[100]. cout<<”\n Enter your name : “. age = currentyear – year.ignore(80. age. cout<<”\n Enter year of birth : “.Example #include <iostream. int year. cin.30).ch2 13 .’\n’). cin.get(name. } noth/csc425 . cout << “\nName : “<<name <<” and the age is “ <<age.

\t and \a every single line end with . (depends on types of method) noth/csc425 . every program must end with return().ch2 14 .Important!!! the usage of \n. or return 0.

\n  Newline. \r Carriage return. Outputs a double quote character. \t Horizontal tab.Character Escape Sequences Predefined symbols can be used to display better output.ch2 15 . noth/csc425 . \a Alert sound (bell). \ ###BOT_TEXT###quot; Outputs a backslash character.

30).getline(name. 30). grade = ‘A’.Example     : Input Statement Data to be keyed in through keyboard eg: cin >> value1 cin. strcpy(name.   Example : Assignment ◦ To assign a value to variable without keying in the data. The value has been assigned in the program. “Sameha”)   noth/csc425 . ◦ Eg : mark = 3.get(name. cin.ch2 16 .

Basic Mathematical.  *  mult = a * b. /. noth/csc425 .ch2 17 .  . sub = a – b. The complex mathematical operations. * and %  +  sum = a + b. -.  Eg : x = (y-z) + b/(y+a). Relational and Logical Operators The Basic Operators in mathematical operations are +.  %  mod = a % b. normally will involved the usage of brackets.  /  div = a / b.

docx noth/csc425 . Eg: a * (b+c) There are several rules that you need to know: Operators in expressions contained within pairs of parenthesis are evaluated first.ch2 18 . / and % are applied next. *.Parenthesis is used in C++ expressions in much the same manner as in algebraic expressions. + and – operations are applied last. Refer to examples: Example mathematics Problem.

>. <=.ch2 19 . || (OR) and ! (NOT)  (7 > 8) || ( 8 < 7)  0(false)  (8>7) && (7<8)  1 (true)  (8>=7)  1 (true)  !(8==8)  0 (false) noth/csc425 . >=. ! =.Relational Operators Are: <. ==.  7 < 8  1 (true)  7>=71 (true)  8 != 8  0 false) Logical Operators Are: && (AND).

The Truth Table   A B A&&B 000 0 010 1 100 1 111 1 A||B !A 1 1 0 0 noth/csc425 .ch2 20 .

y) //xy sqrt(x) exp(x) //ex log(x) //ln x (natural log) log10(x) //log10 x (base 10) sin(x) //x is angle in radian cos(x) //convert angle from degree to radian ◦ tan (x) noth/csc425 .Mathematics Function MUST add header Examples: #include <cmath> ◦ ◦ ◦ ◦ ◦ ◦ ◦ ◦ abs(x) //display positive from x pow(x.ch2 21 .

// j becomes 2 Example:- ◦ ◦ ◦ ◦ ◦ i =1. i is 0 noth/csc425 . //j is 1. j = 3. ◦ i++ // i becomes 4 ◦ j-. //j is 1 . //j is 2. i is 0 i--.Increment and Decrement Operators Example:- ◦ int i = 3. //j is 0. int j = int j = int j = int j = ++i .ch2 22 . i is 2 i++. i is 2 --i.

-> i-=3. noth/csc425 . modified and then reassigned back to the same variable. > i *=3. ->i /=3.Other shorthand Assignment Operators It is used when the current value of a variable is used. i * 3.ch2 23 . i% 3. i – 3. i / 3. Example: ◦ ◦ ◦ ◦ ◦ i i i i i = = = = = i + 3. -> i+=3. ->i %=3.

ch2 24 .674 in a field of width 8 and with 2 digits of precision ***25. //print the next number in a 8 character wide column Cout<<setw(8)<<fixed<<setfill(‘*’) << setprecision(2)<<25.Formatting Program Output #include <iomanip> cout<<setw(8).67 noth/csc425 .674 //prints the 25.