programming language
TRANSCRIPT
![Page 1: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/1.jpg)
AU/MITM/1.6
By Mohammed A. Saleh
1
![Page 2: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/2.jpg)
The course runs from 13th April – 01st May
Total contact hours = 30 hrs
Lectures will always be conducted from
5 PM – 7 PM, unless stated otherwise.
Venue : Lab 4 (Block A, 4th Floor )
![Page 3: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/3.jpg)
Coursework: Test 1 - 20 marks Test 2 - 20 marks Assignment - 10 marks Project - 10 marks
Total 60 marks
Final Exam - 40 marks
![Page 4: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/4.jpg)
➩Module 1Problem solving with computers problem solving techniques – Structured programming sequence structure – Selection Structure – Loop Structure – Advantages
➩Module 2Fundamentals of C++ - Operators and Expressions – Data Input and Output – Control Structures – Storage Classes – Arrays and Strings
![Page 5: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/5.jpg)
➩Module 3
Functions and Pointers – Structures and Unions – Files
➩Module 4
Principles of OOPS – Tokens, expressions and control structures, functions – classes and objects, constructors and destructors, operator overloading and type conversion
![Page 6: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/6.jpg)
➩Module 5
Inheritance – Pointers – Virtual Functions – Polymorphism – Managing console I/O operations - Files
![Page 7: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/7.jpg)
• Decisions play a major role in providing problem solving techniques. Computer programs are all about making decisions. If a user presses a key the computer responds to the command.
• For example if a user presses Ctrl + C, the computer copies the currently selected area to the clipboard.
• Programs that do not make decisions are necessarily pretty boring.
![Page 8: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/8.jpg)
For a computer program to decide what action to take, it uses flow-control commands. This is based on the results of the C++ logical operator.
![Page 9: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/9.jpg)
Simple programming exercises can be solved by just writing code to implement the desired problem.
Complex programs, however can be difficult to write and impossible to debug if not implemented using a structured design process
![Page 10: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/10.jpg)
• Programs could be written in in terms of three control structures:
a) Sequence structure
b) Selection structure
c) Loop/ Repetition structure
![Page 11: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/11.jpg)
• Built into C++. Unless directed otherwise the computer executes C++ statements one after another in the order in which they are written.
Basic blocksDecision
Start
Stop Process
![Page 12: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/12.jpg)
Basic structure
Figure 1.0: SEQUENCE structure
![Page 13: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/13.jpg)
• C++ provides three types of selection structures:
i. The IF statement (single-selection structure) • When a C++ program must choose whether to take a particular action,
you usually implement the choice with an IF statement.
‘If you have a Captain cookie, you get a free cookie.’ (ordinary English)
![Page 14: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/14.jpg)
• The IF statement directs a program to execute a statement or statement block if a test condition is true and to skip that statement or block if the condition is false. Thus, an if statement lets a program decide whether a particular statement should be executed.
• The syntax for the if statement:
if (test-condition)
statement
![Page 15: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/15.jpg)
• A true test-condition causes the program to execute statement, which can be a single statement or a block. A false test-condition causes the program to skip statement.
Figure 2.0: The structure of IF statement
![Page 16: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/16.jpg)
if ( grade >= 60 )
cout << “ Passed”; The example above determines the condition
‘student’s grade is greater than or equal to 60’ is true or false.
![Page 17: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/17.jpg)
ii. The IF/ ELSE statement (double-selection) Lets a program decide which of two statements or
blocks is execute. It’s an invaluable statement for creating alternative courses of action.
‘If you have a Captain Cookie card, you get a Cookie Plus Plus, else you just get an Ordinary Cookie.’
![Page 18: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/18.jpg)
The syntax for the IF/ELSE statement:
if (test-condition)
statement1
else
statement2 If test-condition is true, the program executes
statement1 and skips over statement2. Otherwise, when test-condition is false, the program skips statement1 and executes statement2 instead.
![Page 19: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/19.jpg)
Code Fragment:
if (answer == 1492)
cout << “That’s right!\n”;
else
cout << “You’d better review the topic again.\n”;
Prints the first message if answer is 1492 and prints the second message otherwise. Each statement can be either a single statement or a statement block delimited by braces.
![Page 20: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/20.jpg)
Figure 2.0: The structure of IF statement
![Page 21: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/21.jpg)
Formatting IF/ ELSE statements The two alternatives in an if else statement must be single
statements. If you need more than one statement, you must use braces to collect them into a single block statement.
C++ does not automatically consider everything between if and else a block, so you have to use braces to make the statement a block.
![Page 22: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/22.jpg)
The following code produces a compiler error:
if (ch == ‘Z’)
zorro++; // if ends here
cout << “Another Zorro candidate\n”;
else // wrong dull++;
cout << “Not a Zorro candidate\n”;
![Page 23: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/23.jpg)
Seen as a simple if statement that ends with the zorro++; statement. Then there is a cout statement. So far, so good. But then there is what the compiler perceives as an unattached else, and that is flagged as a syntax error.
You need to add braces to the code to executed the way you want
![Page 24: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/24.jpg)
After adding braces
if (ch == ‘Z’)
{ // if true block
zorro++;
cout << “Another Zorro candidate\n”;
}
else {
dull++;
cout << “Not a Zorro candidate\n”; }
![Page 25: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/25.jpg)
iii. The SWITCH statement Suppose you create a screen menu that asks the user
to select one of five choices—for example, Cheap, Moderate, Expensive, Extravagant, and Excessive. The C++ switch statement more easily handles selecting a choice from an extended list.
![Page 26: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/26.jpg)
The syntax for the switch statement:
switch (integer-expression)
{
case label1 : statement (s)
case label2: statement (s)
...
default : statement (s)
}
![Page 27: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/27.jpg)
The switch statement acts a routing device, it tells the computer which line of code to execute next.
On reaching a switch statement, a program jumps to the line labeled with the value corresponding to the value of integer-expression, for example, if integer- expression has the value 4, the program goes to the line that has a case 4:label
![Page 28: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/28.jpg)
Figure 3.0: The structure of SWITCH statement
![Page 29: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/29.jpg)
Each C++ case label functions only as a line label, not as a boundary between choices. That is, after a program jumps to a particular line in a switch, it then sequentially executes all the statements following that line in the switch.
To make execution stop at the end of a particular group of statements, you must use the break statement
![Page 30: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/30.jpg)
A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true.
C++ provides three types of selection structures:
i. The FOR Loop
ii. The WHILE Loop
iii. The DO … WHILE Loop
![Page 31: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/31.jpg)
i. The WHILE Loop This a looping structure that executes a statement
after a test-condition holds true. The syntax for the WHILE loop:
while (test-condition)
body A program evaluates the test-condition expression.
If it true, the program executes the statement(s) in the body.
![Page 32: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/32.jpg)
Figure 4.0: The structure of a WHILE loop
![Page 33: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/33.jpg)
1. Comments
2. Load <iostream>
3. main
3.1 Print "Welcome to C++\n"
3.2 exit (return 0)
Program Output
1 // firstcpp.cpp
2 // A first program in C++
3 #include <iostream>
4
5 int main()
6 {
7 cout << ”Hello World!\n";
8
9 return 0; // indicate that program ended successfully
10 }
Hello World !
preprocessor directive Message to the C++ preprocessor.Lines beginning with # are preprocessor directives.#include <iostream> tells the preprocessor to include the contents of the file <iostream>, which includes input/output operations (such as printing to the screen).
CommentsWritten between /* and */ or following a //.Improve program readability and do not cause the computer to perform any action.
C++ programs contain one or more functions, one of which must be mainParenthesis are used to indicate a functionint means that main "returns" an integer value.
A left brace { begins the body of every function and a right brace } ends it.
Prints the string of characters contained between the quotation marks.
The entire line, including std::cout, the << operator, the string ”Hello World !\n" and the semicolon (;), is called a statement.
All statements must end with a semicolon.
return is a way to exit a function from a function.return 0, in this case, means that the program terminated normally.
![Page 34: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/34.jpg)
Printing a line of text cout
- Standard output stream object
- “Connected” to the screen <<
- Stream insertion operator
- Value to the right of the operator inserted into output stream
- cout << “ Hello World !! \n”;
![Page 35: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/35.jpg)
Printing a line of text \
- Escape character
- Indicates that a “special” character is to be output <<
- Stream insertion operator
- Value to the right of the operator inserted into output stream
- cout << “ Hello World !! \n”;
![Page 36: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/36.jpg)
There are multiple ways of to print text
Escape Sequence Description
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote character.
![Page 37: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/37.jpg)
1. Load <iostream>
2. main
2.1 Print "Welcome"
2.2 Print "to C++!"
2.3 newline
2.4 exit (return 0)
Program OutputHello World !
1 // firstcpp.cpp
2 // Printing a line with multiple statements
3 #include <iostream>
4
5 int main()
6 {
7 std::cout << ”Hello ";
8 std::cout << ”World !\n";
9
10 return 0; // indicate that program ended successfully
11 }
Unless new line '\n' is specified, the text continues on the same line.
![Page 38: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/38.jpg)
1. Load <iostream>
2. main
2.1 Print "Welcome"
2.2 newline
2.3 Print "to"
2.4 newline
2.5 newline
2.6 Print "C++!"
2.7 newline
2.8 exit (return 0)
Program Output
1 // firstcpp.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4
5 int main()
6 {
7 std::cout << ”Hello\n\n World !\n";
8
9 return 0; // indicate that program ended successfully
10 }
Hello
World !
Multiple lines can be printed with one statement.
![Page 39: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/39.jpg)
Adding Two Integers Variable
Location in memory where a value can be stored for use by a program
Must be declared with a name and a data type before they can be used
Some common data types are: int - integer numbers char - characters double - floating point numbers
![Page 40: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/40.jpg)
Example: int myvariable; Declares a variable named myvariable of
type intExample: int variable1, variable2;
Declares two variables, each of type int
![Page 41: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/41.jpg)
1. Load <iostream>
2. main
2.1 Initialize variables integer1, integer2, and sum
2.2 Print "Enter first integer"
2.2.1 Get input
2.3 Print "Enter second integer"
2.3.1 Get input
2.4 Add variables and put result into sum
2.5 Print "Sum is" 2.5.1 Output sum
2.6 exit (return 0)
Program Output
1 // Add.cpp
2 // Addition program
3 #include <iostream>
4
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 cout << "Enter first integer\n"; // prompt
10 cin >> integer1; // read an integer
11 cout << "Enter second integer\n"; // prompt
12 cin >> integer2; // read an integer
13 sum = integer1 + integer2; // assignment of sum
14 cout << "Sum is " << sum << endl; // print sum
15
16 return 0; // indicate that program ended successfully
17 }
Enter first integer45Enter second integer72Sum is 117
Notice how cin is used to get user input.
Variables can be output using cout << variableName.
endl flushes the buffer and prints a newline.
![Page 42: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/42.jpg)
Adding Two Integers >> (Stream extraction operator)
When used with cin, waits for the user to input a value and stores the value in the variable to the right of the operator
The user types a value, then presses the Enter (Return) key to send the data to the computer
Example:
int myVariable;std::cin >> myVariable;
Waits for user input, then stores input in myVariable
![Page 43: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/43.jpg)
Adding Two Integers = (assignment operator)
Assigns value to a variable Binary operator (has two operands) Example:
sum = variable1 + variable2;
![Page 44: PROGRAMMING LANGUAGE](https://reader033.vdocuments.net/reader033/viewer/2022052603/55d30fabbb61ebd4308b45bc/html5/thumbnails/44.jpg)
-*-*-*- THE END -*-*-*-