0% found this document useful (0 votes)
43 views

Lecture 2 - Basic of Programming

This document provides an overview of basic programming concepts including computer programs, algorithms, variables, data types, and basic programming constructs like sequence, selection, and iteration. It discusses topics like basic terms in programming, examples of algorithms for finding minimum/maximum values, calculating sums, and searching. The document also presents examples of basic programming problems and their solutions using concepts like variables, conditionals, and loops.

Uploaded by

Bob Long
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lecture 2 - Basic of Programming

This document provides an overview of basic programming concepts including computer programs, algorithms, variables, data types, and basic programming constructs like sequence, selection, and iteration. It discusses topics like basic terms in programming, examples of algorithms for finding minimum/maximum values, calculating sums, and searching. The document also presents examples of basic programming problems and their solutions using concepts like variables, conditionals, and loops.

Uploaded by

Bob Long
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

UNIT 1 -

PROGRAMMING
LECTURE 2 – BASICS OF PROGRAMMING
TOPICS

 Basic terms of programming  Basic algorithms


o Computer program o Min / max
o Compiler / interpreter o Sum
o Variables, constants, data types o Search
o Statements: conditional, loop

Unit 1 - Programming / Lecture 2 - Basics of programming 2


COMPUTER PROGRAM

Program is to complete a task when executed on a computer


The computer itself doesn’t know how to complete the task
o A programmer must instruct the computer to do the task
o And so, programmer need to write codes for the instructions
Computer’s vocabulary only includes 1s and 0s
o Programmers can write code in 1s and 0s (punch card) – low level language
o Or in high level language (C, C++, Java) and there must be compilers to compile
the high level language to machine language
Unit 1 - Programming / Lecture 2 - Basics of programming 3
EXAMPLE OF C# PROGRAM

http://csharp-station.com
Unit 1 - Programming / Lecture 2 - Basics of programming 4
STATEMENTS/COMMENTS

 Statement is instruction that programmer tells computer to do


o Every C# statement is terminated with semicolon ‘;’
 Comment is explanation / guide that programmer explain to reader
some block of code and comments will not be executed
o Comment a single line with //
o Comment a block with /*….......*/

Unit 1 - Programming / Lecture 2 - Basics of programming 5


HOW
PROGRAM IS
COMPILED &
EXECUTED

Unit 1 - Programming / Lecture 2 - Basics of programming 6


MEMORY
• To be able to complete a task,
program needs to have the
ability to store its data
0 0 0 0 0 0 0 0
• Program’s data is stored in
computer’s memory 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
• Computer Memory stores data 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
in a sequence of bits (each bit
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1
can either store 0 or 1)
• Even though the smallest unit is
a bit, smallest usable unit in
memory is a byte
• Each byte (8 bits) in the memory
has an unique location (its
counting index)

Unit 1 - Programming / Lecture 2 - Basics of programming 7


VARIABLE

Data
Variable

Variable has:
• Name: grade, age, salary
Variable is used to store data • Type: integer, float, string
• Data (value): 100, 10.5, “John”

Unit 1 - Programming / Lecture 2 - Basics of programming 8


VARIABLE

Data

Data
Variable

Variable is used to store data


“Variable” means it could store different data at
different time
Unit 1 - Programming / Lecture 2 - Basics of programming 9
CONSTANTS/LITERALS

Constants refer to fixed values


o These fixed values are also called literals
E.g.,
o Integer constants: 1, 2, 3,…
o Floating constants: 1.1, 2.3, 4.0,...
o Character constants: ‘a’, ‘b’, ‘c’,…
o String literals: “Hello, there”, “Hi, there”, ...
Variables are used to hold constant values
o But at different time it can hold different constant values
Unit 1 - Programming / Lecture 2 - Basics of programming 10
DATA TYPES

Variable must have a data type, which specifies


o Which kind of data it can contain
o Size of data to be stored in the memory (bytes)
o How to convert from them from/to binary format
Data types can be divided into
o Primitive data types e.g., int, float double, etc.
o Reference data types e.g., array, pointer, etc.

Unit 1 - Programming / Lecture 2 - Basics of programming 11


DATA TYPES

variable Data
Data

Unit 1 - Programming / Lecture 2 - Basics of programming 12


BASIC PROGRAMMING CONSTRUCTS
 Basic programming constructs are
o Sequence, selection, and iteration (looping)
 Sequence construct
o Instructions are executed in the same order as they appear
 Selection structure
o Control flow can be changed by evaluating conditions
 Iterative structure
o A group of instructions is executed repeatedly
o Until some condition is satisfied

Unit 1 - Programming / Lecture 2 - Basics of programming 13


SEQUENCE CONSTRUCT

 Problem: Given 2 numbers, calculate sum and print

 Exercise: Enter 3 grades, calculate average grade


Unit 1 - Programming / Lecture 2 - Basics of programming 14
CONDITIONAL CONSTRUCT

 Problem: Based on grade, decide to pass or fail

 Exercise: Based on age, decide


school (elementary, middle
school and high school)

Unit 1 - Programming / Lecture 2 - Basics of programming 15


LOOP CONSTRUCT

Unit 1 - Programming / Lecture 2 - Basics of programming 16


LOOP CONSTRUCT

Unit 1 - Programming / Lecture 2 - Basics of programming 17


LOOP CONSTRUCT

 Problem: Print 10 times the sentence “Hello World”

 Exercise
o Print numbers up to 10
o Print even numbers up to 10
o Print odd numbers up to given n

Unit 1 - Programming / Lecture 2 - Basics of programming 18


SIMPLE ALGORITHM: SWAP 2 NUMBERS

 Problem: Given 2 numbers a & b. Swap values of a & b

Unit 1 - Programming / Lecture 2 - Basics of programming 19


SIMPLE ALGORITHM: FIND MIN/MAX

 Problem: Given a, b. Find min, max


 Exercise: Given a, b, c. Find min (max)

Unit 1 - Programming / Lecture 2 - Basics of programming 20


SIMPLE ALGORITHM: CALCULATE SUM

 Problem: Calculate sum from 1-10


 Exercise: Calculate sum from 1-n
 Exercise: Calculate sum of even numbers from 1-n

Unit 1 - Programming / Lecture 2 - Basics of programming 21


SIMPLE ALGORITHM: SEARCH

 Problem: Find x in a collection of numbers


 Exercise: Find all even numbers in a collection of numbers
 Homework: Check if a collection of numbers contains only positive
numbers

Unit 1 - Programming / Lecture 2 - Basics of programming 22


SUMMARY

 Draw a mind-map to summarize the content of this lecture

Unit 1 - Programming / Lecture 2 - Basics of programming 23

You might also like