Professional Documents
Culture Documents
Lec5 Introduction To C
Lec5 Introduction To C
Introduction To Computer
Programming
Saqib Rizwan
int i, sum;
Set sum to 0 sum = 0;
Set i to 1 i = 1;
While i 5 do Wouldn’t
while ( i <=it5be
) nice if we
Set sum to sum+i { could instead write
Set i to i+1 sum = sum + i;
End of While loop
our program
i = i + 1; in a
Print the value of sum } language more similar
to a<<pseudo-code?
cout sum;
6
Why C++
Compiler
C++
C++ Program
Program C++
C++ Compiler
Compiler Machine
Machine
Language
Language
(e.g.
(e.g. g++)
g++) Program
Program
int
int main()
main() {{
int
int i=1;
i=1;
.. .. .. 01001001
01001001
10010100
10010100
• C++ systems
– Program-development environment
– Language
– C++ Standard Library
10
Primary
6. Execute CPU
Memory
C++ Preprocessor
Preprocessing
Temporary
Temporary file
file
C++
C++ (C++
(C++ program)
program) C++
C++
Preprocessor
Preprocessor Compiler
Compiler
Executable
Executable
C++
C++ Program
Program Program
Program
16
1 // code1.cpp
2 // A first program in C++.
Single-line comments.
3 Function main
#include <iostream> returns an
4 integer { begins Preprocessor
value.
Left brace function directive to
5 // function main body. program
begins Function include input/output Statements
main appears
execution stream end with a
6 int main() header file <iostream>.
exactly once in every C++ semicolon ;.
7 { program..
8 cout << "Welcome to C++!\n";
9 Corresponding right brace }
10 return 0; //ends function
indicate body.
that program ended successfully
11 Stream insertion operator.
12 } // end function main
Welcome to C++!
20
1 // code3.cpp
2 // Printing multiple lines with a single statement
3 #include <iostream>
4
5 // function main begins program executionUsing newline characters to
6 int main() print on multiple lines.
7 {
8 std::cout << "Welcome\nto\n\nC++!\n";
9
10 return 0; // indicate that program ended successfully
11
12 } // end function main
Welcome
to
C++!
21
The Preprocessor
#define square(a) (a * a)
y = square(x);
z = square(y*x);
becomes y = (x * x);
Memory Concepts
• Variable names
– Correspond to actual locations in computer's memory
– Every variable has name, type, size and value
– When new value placed into variable, overwrites previous
value
– Reading variables from memory, nondestructive
29
Memory Concepts
Arithmetic
• Arithmetic calculations
– *
• Multiplication
– /
• Division
• Integer division truncates remainder
– 7 / 5 evaluates to 1
– %
• Modulus operator returns remainder
– 7 % 5 evaluates to 2
31
Arithmetic
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
34
Decision Making: Equality and
Relational Operators
• using statements
– Eliminate use of std:: prefix
– Write cout instead of std::cout
35
1 // code5.cpp
2 // Using if statements, relational
3 // operators, and equality operators.
4 #include <iostream>
5
6 using std::cout; // program uses cout
7 using std::cin; // program uses cin using statements eliminate
8 using std::endl; // program uses endl need for std:: prefix.
9
10 Declare
// function main begins program variables.
execution
11 int main()
12 {
13 Can write
int num1; // first number to be readand
cout cinuser
from
14 without to
int num2; // second number std:: prefix.
be read from user
15
16 cout << "Enter two integers, and I will tell you\n"
17
if structure compares values
<< "the relationships they satisfy: ";
18 cin >> num1 >> num2;
of num1 and num2
// read two integers
to test for
If condition is true (i.e.,
19 equality. values are equal), execute this
20 if ( num1 == num2 )
if structure compares
statement.values
21 cout << num1 << " is of num1
equal to " and If condition
num2
<< num2 <<toendl; is true (i.e.,
test for
22 inequality. values are not equal), execute
23 if ( num1 != num2 ) this statement.
24 cout << num1 << " is not equal to " << num2 << endl;
25
36
26 if ( num1 < num2 )
27 cout << num1 << " is less than " << num2 << endl;
28
29 if ( num1 > num2 )
30 cout << num1 << " is greater than " << num2 << endl;
Statements may be split over
31
32 if ( num1 <= num2 )
several lines.
33 cout << num1 << " is less than or equal to "
34 << num2 << endl;
35
36 if ( num1 >= num2 )
37 cout << num1 << " is greater than or equal to "
38 << num2 << endl;
39
40 return 0; // indicate that program ended successfully
41
42 } // end function main
Integral Types
Expressions
Precedence
Precedence
Operators Precedence
() highest (applied first)
* / %
+ -
< <= > >=
== !=
= lowest (applied last)
46
const
Division
7/2 3
7.0 / 2.0 3.5
7.0 / 2 3.5
7 / 2.0 3.5
51
Expression Value
3+6 9
3.4 - 6.1 -2.7
2*3 6
2 * 3.0 6.0
8/2 4
8.0 / 2.0 4.0
Mixed Mode Expressions
An expression that takes operands of different types.
52
Compound Expressions
avgTemp = FREEZE_PT + BOILING_PT / 2.0;
Expression int1
int1 = float1 + 8.2 ; 10
int1 = int (float1 + 8.2) ; 10
What’s the difference?
Any advantages?
CLARITY!
56
Type Casting
average = total / count ;
Control Structures
59
Control Structures
• Sequential execution
– Statements executed in order
• Transfer of control
– Next statement executed not next one in sequence
• 3 control structures
– Sequence structure
• Programs executed sequentially by default
– Selection structures
• if, if/else, switch
– Repetition structures
• while, do/while, for
61
Control Structures
• C++ keywords
– Cannot be used as identifiers or variable names
C ++ Keywords
Control Structures
• Flowchart
– Graphical representation of an algorithm
– Special-purpose symbols connected by arrows (flowlines)
– Rectangle symbol (action symbol)
• Any type of action
– Oval symbol
• Beginning or end of a program, or a section of code (circles)
• Single-entry/single-exit control structures
– Connect exit point of one to entry point of the next
63
if Selection Structure
• Selection structure
– Choose among alternative courses of action
– Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
– If the condition is true
• Print statement executed, program continues to next statement
– If the condition is false
• Print statement ignored, program continues
– Indenting makes programs easier to read
• C++ ignores whitespace characters (tabs, spaces, etc.)
64
if Selection Structure
if ( grade >= 60 )
cout << "Passed";
• Diamond symbol (decision symbol)
– Indicates decision is to be made
– Contains an expression that can be true or false
• Test condition, follow path
• if structure
– Single-entry/single-exit
65
if Selection Structure
• if
– Performs action if condition true
• if/else
– Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
67
false true
grade >= 60
• Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
70
• Compound statement
– Set of statements within a pair of braces
if ( grade >= 60 )
cout << "Passed.\n";
else {
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
– Without braces,
cout << "You must take this course again.\n";
always executed
• Block
– Set of statements within braces