KEMBAR78
ZSM Programming Lecture 8 | PDF | Parameter (Computer Programming) | Source Code
0% found this document useful (0 votes)
15 views66 pages

ZSM Programming Lecture 8

This lecture introduces the fundamentals of programming, including definitions, types of programming languages, and key programming elements such as variables, loops, and functions. It emphasizes the importance of writing efficient and readable code, as well as the process of debugging and handling exceptions. Additionally, it covers file handling and the significance of proper output formatting in programming.

Uploaded by

leenkoma22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views66 pages

ZSM Programming Lecture 8

This lecture introduces the fundamentals of programming, including definitions, types of programming languages, and key programming elements such as variables, loops, and functions. It emphasizes the importance of writing efficient and readable code, as well as the process of debugging and handling exceptions. Additionally, it covers file handling and the significance of proper output formatting in programming.

Uploaded by

leenkoma22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 66

Lecture 7:

Introduction to
Programming
Lecture Content
Introduction
Definition of terms
Computer Programming
Types of Programming Languages
Programming Elements
Application in Mining
Introduction
•Programming is the act of writing instructions that make the
computer do something useful. It is an intensely creative activity,
involving aspects of art, engineering, and science.
• Good programs are written to be executed efficiently by computers,
but also to be read and understood by humans.
•The best programs are delightful in ways similar to the best
architecture, elegant in both form and function
Introduction
•Without programs, computers are useless.
•A program is like a recipe. It contains a list of ingredients (called variables) and a list of
directions (called statements) that tell the computer what to do with the variables.
•Computer programming is the process of writing, testing, debugging/troubleshooting, and
maintaining the source code of computer programs
•Once the code (language) of a program has been written, it must be executed (run, started).
You may need to type the name of the program to start it, or use a word like RUN and the name
of the program
•• This source code is written in a programming language like C++, JAVA, Perl etc
Compilers and Translators
•A compiler is a computer program that generates other programs. It
translates an input program written in a high-level language that is easier
for humans to create into a program in a machine-level language that can
be executed by the computer.
•Admiral Grace Hopper developed the first compilers in the 1950s.
•An alternative to a compiler is an interpreter. An interpreter is a tool that
translates between a higher-level language and a lower-level language, but
where a compiler translates an entire program at once and produces a
machine language program that can be executed directly, an interpreter
interprets the program a small piece at a time while it is running.
Programming Languages
•For programming computers, we want simple, unambiguous, regular,
and economical languages with powerful means of abstraction.
•A programming language is a language that is designed to be read
and written by humans to create programming programs that can be
executed by computers.
•It is difficult to simultaneously satisfy all desired properties since
simplicity is often at odds with economy. Every feature that is added
to a language to increase its expressiveness incurs a cost in reducing
simplicity and regularity
Types of Programming
Languages
•There are three types of programming language:
– Machine language (Low-level language)
– Assembly language (Low-level language)
– High-level language
•Low-level languages are closer to the language
•used by a computer, while high-level languages
•are closer to human languages.
Programming Languages
•C is a great way to learn how computers actually work in terms of
memory management, and is useful in high-performance computing
•C++ is great for game development.
•Python is awesome for science and statistics.
•Java is important if you want to work at large tech companies.
Language Difficulty
Machine Language
Machine language is a collection of binary digits or bits that the computer reads and
interprets. It is the “native” language of a computer
Assembly Language
Example:
• Machine language: – 10110000 01100001
•Assembly language: – mov a1, #061h
•Meaning: – Move the hexadecimal value 61 (97 decimal) into the
processor register named "a1".
•Computer don't understand the assembly code, so we need a way to
convert it to machine code, which the computer does understand.
•Assembly language programs are translated into machine language
by a program called an assembler
High Level Language
•High-level languages allow us to write computer code using
instructions resembling everyday spoken language (for example:
print, if, while) which are then translated into machine language to
be executed.
•Programs written in a high-level language need to be translated into
machine language before they can be executed.
• Some programming languages use a compiler to perform this
translation and others use an interpreter
Examples of High Level
Language
–C
– C++
– JAVA
– BASIC
– COBOL
– PASCAL
– PYTHON
High level languages
Here are some examples of an instruction to print the
word HI
• Logo - PR [HI]
• JavaScript - alert(“HI”);
• FORTRAN - PRINT “HI”
•BASIC - PRINT “HI”
• COBOL - DISPLAY ‘HI’.
•C++ - printf(“HI”);
•Pascal - WRITELN(‘HI’);
Writing a Program/Designing a
Program
•Decide what steps are needed to complete the task
•Write the steps in pseudocode (written in English) or as a flowchart
(graphic symbols)
•logical programming is developed through good precode planning
and organization.
•This is assisted by the use of pseudocode and program flowcharts
•Translate into the programming language
••Try out the program and “debug” it (fix if necessary)
Pseudocode
•Pseudocode is a method of describing computer algorithms using a combination of
natural language and programming language
•Like the instructions for a recipe, it must be in the right sequence
•It is a methodology that allows the programmer to represent the implementation of an
algorithm. Simply, we can say that it’s the cooked up representation of an algorithm.
•Pseudo code, as the name suggests, is a false code or a representation of code which
can be understood by even a layman with some school level programming knowledge.
•Algorithm: It’s an organized logical sequence of the actions or the approach towards a
particular problem. A programmer implements an algorithm to solve a problem.
Elements of a Program
All programming languages have certain features in common. For
example:
– Variables
– Commands/Syntax (the way commands are structured)
– Loops
– Decisions
– Functions
• Each programming language has a different set of rules about these
features.
Variables
•Variables are part of almost every program.
•A variable is a “place to put data” and is usually represented by a
letter or a word. (Think of a variable as a Tupperware container with
a label on it.)
•Variable names cannot contain spaces.
•Some programming languages have very specific limits on variable
names.
Variables
•Usually there are several ways to put information into a variable.
• The most common way is to use the equal sign (=).
•X = Y + 7 means take the value of Y, add 7, and put it
•into X.
•COUNT=COUNT + 2 means take the current value of COUNT, add
2 to it, and make it the new value of COUNT.
Variables
Sometimes you must specify the type of data that will be placed in a
variable.
• Here are some examples of data types:
– Numeric (numbers of all kinds)
– String (text, “strings of letters”)
– Integer (whole numbers)
– Long (large numbers)
– Boolean (true/false)
Data are a collection of discrete or continuous values that convey
information, describing the quantity, quality, fact, statistics, other basic units
of meaning, or simply sequences of symbols that may be further interpreted
formally.
◦ Types of data
Variables
Variables may be classified as global or local.
• A global variable is one that can be shared by all parts of a program,
including any functions or sub-programs.
• A local variable is one that is used only within a certain part of the
program, for example, only in one function or sub-program.
Commands/Syntax
Programming languages are truly languages.
• They have rules about grammar, spelling,
punctuation, etc.
• You need to learn the rules of a programming
language, just as you learned to speak and write
English.
Loops
•A loop is a repetition of all or part of the commands in a program.
•A loop often has a counter (a variable) and continues to repeat a
specified number of times.
•A loop may also continue until a certain condition is met (e.g., until
the end of a file or until a number reaches a set limit)
Loops
Decisions
•In programming, decisions are made using decision structures, which
are also known as conditional statements. These structures allow
programs to make decisions and change their behavior based on the
results of logical tests.
•A program often needs to decide whether something is true or false in
order to see which way to continue.
•Programs often use IF (or IF THEN or IF THEN ELSE) statements to
show a decision.
Decisions
•An IF statement always has a condition to check, often a comparison
between a variable and a number.
• The IF statement also must specify what to do if the condition/comparison is
true.
•These instructions (for “true”) may come after the word THEN, or they may
simply be listed.
•In an IF THEN statement, when the condition is false, the program simply
ignores the THEN commands and continues to the next line.
•• In an IF THEN ELSE statement, commands are given for both the true and
false conditions.
Decisions
How decision structures work

•The programmer specifies conditions to be evaluated


•The program evaluates the conditions using boolean expressions
•The program executes a block of code if the condition is true
•The program does not execute the block of code if the condition is
false.
Functions
•In most programming languages, small subprograms are used to perform
some of the tasks.
•These may be called functions, subroutines, handlers, or other such terms.
•Functions often have names (e.g., getName or CALCTAX)
•A function generally gets information from the main program, performs
some task, and returns information back to the program.
•Functions follow the same rules of syntax, etc. as the main program.
•JavaScript code is primarily made of a series of functions.
Debug
•To “debug” means to try a program, then fix any mistakes.
•Virtually no program works the first time you run it. There are just too many places
to make errors.
• When you are debugging a program, look for spelling and punctuation errors.
•Fix one error at a time, then try the program again.
•A debugger is a tool that lets you run the program in a controlled environment and
step through the code line by line.
•Setting breakpoints
•You can set markers in your code where you want the program to pause so you can
inspect the program's state.
INPUT AND OUTPUT;
Many programs are dependent on: Asking a user or player to input information
​What is your name? What is your bet? What is your guess
Enter two numbers
Being able to store the user's input for use by the program
​name = "Vicki"
guess = 15
num1 = 10 and num2 = 25
Processing the user's input or information
​Display it back to them
​It's nice to meet you Vicki
​Use their input in a calculation or other process. Results may or may not be displayed on screen.
Compare their guess to the actual number: "Your guess is too low. Try again."
total = num1 + num2
Assignments
•In programming, an "assignment" refers to the act of storing a specific value within a variable,
essentially "giving" a value to a variable name using an assignment operator (usually the equal
sign "="); it's a fundamental operation where you set or change the data held by a variable
within your code.
Key points about assignments:
•Syntax:Most programming languages use the syntax "variable_name = value" to assign a
value.
•Variable:The "variable_name" is a placeholder that can hold different values throughout the
program.
•Value:The "value" can be a number, text, a result of a calculation, or any data type supported
by the language.
Main Program
“Main program" refers to the core function called "main" which
serves as the entry point for program execution, meaning that when
you run a program, the code inside the "main" function is the first
part to be executed by the system; it's essentially where the program
starts running.
Logical operators
•A logical operator is a symbol or word used to connect two or more expressions such that the
value of the compound expression produced depends only on that of the original expressions
and on the meaning of the operator. Common logical operators include AND, OR, and NOT.
•Within most languages, expressions that yield Boolean data type values are divided into two
groups. One group uses the relational operators within their expressions and the other group
uses logical operators within their expressions.
•The logical operators are often used to help create a test expression that controls program flow.
This type of expression is also known as a Boolean expression because they create a Boolean
answer or value when evaluated. There are three common logical operators that give a Boolean
value by manipulating other Boolean operand(s). Operator symbols and/or names vary with
different programming languages
Logical Operators in Different
Languages
Language AND OR NOT

C++ && || !

C# && || !

Java && || !

JavaScript && || !

Python and or not

Swift && || !
Output Formatting
Proper output formatting plays a crucial role in programming. It improves the readability of the code and enhances
the user experience. Well-formatted output makes it easier for users to understand the information presented and
reduces the chances of misinterpretation. Additionally, formatted output adds a professional touch to your code
and makes it more presentable.
Basic Output Formatting Techniques: Using the print() Function
The print() function is the most basic way to display output in Python. It allows you to print strings, variables, and
expressions. For example:
Code
name = "John“ age = 25
print("My name is", name, "and I am", age, "years old.")
Output
My name is John and I am 25 years old.
Parameters
•In programming, a parameter is a variable that receives values passed into a
function or procedure. Parameters are also known as formal arguments.
•Parameters allow the values of local variables within a main program to be
accessed, updated, and used within multiple sub-programs.
•Functions can operate on varying input values.
•Avoids writing separate code blocks for similar tasks.
Return values
•A return is a value that a function returns to the calling script or
function when it completes its task. A return value can be any one of
the four variable types: handle, integer, object, or string.
•The result of a function is called its return value and the data type of
the return value is called the return type. Every function declaration
and definition must specify a return type, whether or not it actually
returns a value.
Lists and strings
A string is just a word or sentence. Notice that any time text is used it is surrounded by quotes '
'.
You can use either single or double quotes as long as the opening one is the same as the closing
one.
These quotes are not necessary for numbers or variables, only strings.
Dictionary
In programming, a dictionary is a data
structure that stores data in key-value
pairs. It's similar to a real-world
dictionary, where the words are the keys
and the definitions are the values.
Values and references
•There are two kinds of types in Visual Basic: Reference types and value
types.
•Variables of reference types store references to their data (objects), while
variables of value types directly contain their data.
•With reference types, two variables can reference the same object;
therefore, operations on one variable can affect the object referenced by the
other variable.
•With value types, each variable has its own copy of the data, and it is not
possible for operations on one variable to affect the other (except in the
case of the ByRef modifier on parameters).
Exceptions
•An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program's instructions.
•When an error occurs within a method, the method creates an object and hands it off to the
runtime system. The object, called an exception object, contains information about the error,
including its type and the state of the program when the error occurred. Creating an
exception object and handing it to the runtime system is called throwing an exception.
•A method throws an exception; the runtime system attempts to find something to handle it.
The set of possible "somethings" to handle the exception is the ordered list of methods that
had been called to get to the method where the error occurred. The list of methods is known
as the call stack .
File handling
•A file is a container in computer storage devices used for storing data.
•When a program is terminated, the entire data is lost. Storing in a file will
preserve your data even if the program terminates.
•If you have to enter a large number of data, it will take a lot of time to enter
them all.
•However, if you have a file containing all the data, you can easily access the
contents of the file using a few commands in C.
•You can easily move your data from one computer to another without any
changes.
Types of Files
1.Text Files
Text files are the normal .txt files. You can easily create text files using any simple text editors
such as Notepad. When you open those files, you'll see all the contents within the file as plain
text. You can easily edit or delete the contents. They take minimum effort to maintain, are
easily readable, and provide the least security and takes bigger storage space.

2. Binary files
Binary files are mostly the .bin files in your computer. Instead of storing data in plain text,
they store it in the binary form (0's and 1’s). They can hold a higher amount of data, are not
readable easily, and provides better security than text files.
Classes and objects
•In programming, a "class" is a blueprint or template that defines the characteristics and
behaviors of a type of object, while an "object" is a specific instance of that class, meaning it's a
concrete realization of the class with actual data values assigned to its properties and the ability
to perform actions defined by the class methods; essentially, a class is the design, and an object
is a thing created based on that design.
•Think of a class like a recipe for a cake - it outlines the ingredients (variables) and steps
(methods) needed to create a cake, but it's not the actual cake itself.
•Object as an instance:An object is like a specific cake baked using that recipe, with its own
unique characteristics like frosting color and size.
•Attributes and methods:
•A class defines attributes (data variables) that describe the object's state, and methods (functions)
that define what the object can do.
Classes and programming in
Python
Why are classes and objects
important?
Reusability:
•By defining a class, you can create multiple objects with similar
functionality, reducing code duplication.
Organization:
•Classes help structure complex programs by grouping related data and
behaviors together.
Modularity:
•Objects can be treated as self-contained units, making it easier to understand
and maintain code.
How to write Code
•Every software solution starts with an idea. If we talk about businesses that are willing to
acquire automation tools, the aim of software development and integration is to eliminate
specific daily issues, automate the processes and increase productivity overall.
•So, to choose suitable technologies the engineers need to clearly understand what type of
solution it is, what functions it needs to contain, what purpose it has.
•To sum up, the first step in writing a code is planning.
•The writing of code is divided into two options—frontend and backend development.
Front-end engineers are responsible for the visible part of the software program, while
back-end developers connect the actual back of the solution with the front part to make it
functional.
•Commonly, engineers create source code with comments on what each piece is
accountable for to be used in the next writing code step.
How to write code
•The next stage is source code compilation, in which details depend on the programming
environment. Usually, engineers use special compiling tools that convert the source code
into executable code or final software, making each function of the solution work
according to the machine languages of user computers
•This stage also helps define the code bugs and errors that prevent the code to be
converted into an executable file.
•The executable code is not a completed program, but you can open the final code file in
suitable programming environments, meaning operating systems
•After the program code is edited, it is ready to be tested in order to reveal any possible
bugs meaning system errors. Commonly, the software systems go through various
manual and automated tests that check if all functions work stable and conduct various
scenarios to approve if the system is completely viable
How to write code
•Any type of software solution, including its code, requires constant
maintenance and regular upgrades. With years, technologies, and
programming languages change, so the program code can become
irrelevant and outdated.
•This process is named legacy code, and it needs to be refactored. This
process entails code revision, changing, and fixing. Also, writing
code needs constant technical support in case the system breaks down
and requires quick solutions.
Summary of developing a
program
Problem solving principles:
1. Completely understand the
problem
2. Devise a plan to solve it
3. Carry out the plan
4. Review the results
Developing a Program:
1. Analyze the problem
2. Design the program
3. Code the program
4. Test the program
Applications of programming in
Mining
Mine Planning and Design:
Programming is used to create software for 3D modeling, mine design, and
optimization. Tools like AutoCAD, Surpac, and Datamine use algorithms to help
design mining operations, create mine plans, and simulate scenarios to optimize
resource extraction and reduce costs.
Geospatial Data Analysis:
Software development plays a key role in analyzing geospatial data. This includes the
use of geographic information systems (GIS) and remote sensing to assess mineral
deposits, map terrain, and predict geological features. Programming is involved in
creating algorithms that process and visualize data from satellite imagery or drones.
.
Applications of Programming in
Mining
•Real-Time Monitoring Systems:
Sensors deployed on mining equipment generate large amounts of data.
Programming is essential to design real-time monitoring systems that track the
condition of machines, energy consumption, and environmental factors. These
systems help predict failures, schedule maintenance, and improve overall efficiency
by processing and analyzing this data.
•Data Analytics and Predictive Maintenance:
Mining companies use data analytics and machine learning to predict equipment
failure and improve operational efficiency. Programming is used to develop models
that analyze historical data from machinery to forecast potential breakdowns and
optimize maintenance schedules, reducing downtime and maintenance costs.
Practice questions to test your
understanding of Introduction to
Programming
What is Programming? How do we write Code? How do we get Information from
Computers?
What can Computers Do? What are Variables? How do we Manipulate
Variables?
What are Conditional Statements? What are Array’s? What are Loops?
What are Errors? How do we Debug Code? What are Functions?
How can we Import Functions How do we make our own Functions? What are ArrayLists
and Dictionaries?
How can we use Data Structures? What is Recursion? What is Pseudocode?

Choosing the Right Language? Applications of Programming in Mining


1.Watch youtube video
https://youtu.be/zOjov2OZ0E?si=lcvs
6GXn6XBPpjbd

2. Subscribe to Freecode Camp for


free coding lessons,

3. I recommend you start with Python

You might also like