Professional Documents
Culture Documents
CS 111 Introduction To Computer Science: by Wessam El-Behaidy & Amr S. Ghoneim
CS 111 Introduction To Computer Science: by Wessam El-Behaidy & Amr S. Ghoneim
INTRODUCTION TO
COMPUTER SCIENCE
Fall 2018
3
Control
Structures
REPETITION (LOOPS)
Sequence Selection Repetition
COUNTER-CONTROLLED SENTINEL-CONTROLLED
Do .. While statement
COUNTER-CONTROLLED REPETITION
Procedure k=k + 1
k=k + 1 Yes
Exit loop k <= 10
for statement Exit
loop No
while statement Exit loop 5
Do .. while
statement
COUNTER-CONTROLLED REPETITION
Example 8:
START
Summation of two numbers for
10 iterations
for k = 1 to 10
Pseudocode
Read Num1,
For k = 1 to 10 Num2
Read Num1, Num2
Sum = Num1 + Num2
Compute Sum as Num1 + Num2
Write Sum
End for Write Sum
Yes
Pseudocode
Read Grade
Initialize k = 1
While k <= 20 Print
Yes
Grade
>= 60
Read Grade “Passed
”
If Grade >= 60 No
Print “Passed” k=k+1
k = k+1
End while
End 7
START
Example 10:
(READ DEFINED BY THE USER)
ReadStart
StudNo
Pseudocode K <= No
k <= 20
StudNo
Yes
Read the Number of students (StudNo)
Read Grade
initialize k = 1
While k <= StudNo
Print Yes Grade
Read Grade >=60
“Passed
If Grade >= 60 ”
Print “Passed” No
End if k=k + 1
k=k+1
End while
8
End
Example 11: Start
Read value
Add value to sum count = count + 1
(i.e. sum = sum + value(
count = count + 1 Yes
count <= 100
While (count <= 100)
print sum No
End 9
End Print sum
SENTINEL-CONTROLLED REPETITION
10
Start
Example 12: average = 0
count = 0
Read and compute the average of total = 0
a set of numbers. Sentinel = -1
Read num
Pseudocode
set average to zero No
Num ≠ Sentinel
Yes
total = total + num
count = count +1
Read num
Yes
average = total / count count > 0
11
No
Consists of?
Vocabulary- words Keywords
What for?
Sentences Instructions
How?
Grammar Syntax
Has a meaning?
Meaningful Correct Semantic
sentence
Different Languages? ? 12
Arabic, English,….
PROGRAMMING LANGUAGES
Instructions can be written in various programming languages.
Machine languages:
Any computer can directly understand only its own machine
language.
Consist of numbers (0’s and 1’s).
Assembly Languages:
English like abbreviations to represent elementary
operations.
Assemblers were developed to convert assembly-language
programs to machine language.
High-Level Languages:
Compilers convert high-level language programs into
machine language. 13
STANDARD C
14
STRUCTURED
PROGRAMMING
{
printf( "Welcome to C!\n" );
return 0;
}
16
TYPICAL C PROGRAM DEVELOPMENT
ENVIRONMENT
Source file
Editor Compiler
(.c , .cpp) Successful
Other
Error messages Object file Object file(s)
)Syntax error( Unsuccessful
Linker
Executable
Output Loader
file (.exe) 17
ERROR TYPES
Syntax error is a violation of the C grammar rules,
detected during program compilation
E.g.,a missing semicolon, an unclosed comment, spelling
mistakes, … etc.
Run-time error is an attempt to perform an invalid
operation, detected during program execution
E.g., Divide by zero, … etc.
Semantic error is an error caused by following an
incorrect algorithm
Not the required output.
18
THANKS