You are on page 1of 19

CS 111

INTRODUCTION TO
COMPUTER SCIENCE
Fall 2018

By Wessam El-Behaidy & Amr S. Ghoneim


,Assistant Professors
Computer Science Department
LECTURE 4
;Problem Solving Concepts
Flow-Charts Continued … Programming Terminologies & C
… Introduced
OUTLINE
 This lecture covers:
 Repetition Control Structure
 Programming Terminologies
 Introduction to C Programming

3
Control
Structures

REPETITION (LOOPS)
Sequence Selection Repetition

 Repeating a series of instructions over and over until some


event occurs.
 For example: if we wish to read 100 numbers, and then
compute the average.
 Two types of repetition:
REP ETITION

COUNTER-CONTROLLED SENTINEL-CONTROLLED

 Three types of statements:


 For statement
 While statement 4

 Do .. While statement
COUNTER-CONTROLLED REPETITION

 Number of repetitions is known before it begins.


Enter loop
Enter loop
counter Enter loop
k = 1
k = 1
for k = 1 to 10
No Exit
k <= 10 loop Procedure
Procedure Yes

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

1. Solve the problem for one case.


2. Specify the statements to be End 6
repeated.
Example 9: START

Reads 20 students’ grades, and k = 1


write “passed” if a grade is
greater than or equal 60. k <= 20
No

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

Repeat Example 9 but


Reads the number of students k = 1

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

Summation of 100 values using


do .. while sum=0
count=1
Pseudocode
Read value
Initialize sum = 0
Initialize count = 1
Do sum = sum + value

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

 If we do not know how many times we want to do repetition.

 It is dependent on the data provided to the program.

 “Sentinel Value” to indicate “end of data entry”

 E.g., enter -1 to end …

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

Print average End


PROGRAMMING TERMINOLOGIES

Natural Languages Programming Languages

 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

 Standardized in 1989 by ANSI (American National


Standards Institute) known as ANSI C.
 ISO (International standard Organization) in 1990
which was adopted by ANSI and is known as C89 or
ANSI/ISO C.
 Standard C.
 What we will study?
 As part of the normal evolution process the standard
was updated in 1995 (C95) and 1999 ( C99).

14
STRUCTURED
PROGRAMMING

One Large Program  Structured Program The main


function
-- (solves a problem
-- as a whole).
--
--
--
--
-- A function
-- fn1 fn3 solves a sub
-- problem.
--
--
fn2 fn4

 Easier to test, debug, modify & reuse.

 Object Oriented Programming (OOP) concept  next course (PL2) 15


YOUR FIRST PROGRAM IN
C
#include <stdio.h>

int main( void ( Called function


Main function

{
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

You might also like