Programming Fundamentals

Similar documents
An Introduction to Python (TEJ3M & TEJ4M)

Advanced Batch Files. Ch 11 1

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Unit E Step-by-Step: Programming with Python

8. Control statements

Flow Control: Branches and loops

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Conditionals and Recursion. Python Part 4

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Introduction. C provides two styles of flow control:

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Flow Control. CSC215 Lecture

BasicScript 2.25 User s Guide. May 29, 1996

Lab: Supplying Inputs to Programs

The Turing Environment

Laboratory 5: Implementing Loops and Loop Control Strategies

(Refer Slide Time: 01:12)

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C: How to Program. Week /Mar/05

Chapter 2 - Introduction to C Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

PLD Semester Exam Study Guide Dec. 2018

Program Elements -- Introduction

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

6.096 Introduction to C++ January (IAP) 2009

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

bash Execution Control COMP2101 Winter 2019

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Course Outline. Introduction to java

The compiler is spewing error messages.

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

CS2900 Introductory Programming with Python and C++ Kevin Squire LtCol Joel Young Fall 2007

Perl Basics. Structure, Style, and Documentation

CS 112: Intro to Comp Prog

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

CSCI 1061U Programming Workshop 2. C++ Basics

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

15-122: Principles of Imperative Computation

Chapter 1 & 2 Introduction to C Language

CS102 Unit 2. Sets and Mathematical Formalism Programming Languages and Simple Program Execution

Fundamentals of Programming Session 4

Computer Components. Software{ User Programs. Operating System. Hardware

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Programming with C++ Language

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Step by step set of instructions to accomplish a task or solve a problem

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

(Refer Slide Time: 00:26)

Basics of Java Programming

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

LOOPS. Repetition using the while statement

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

printf( Please enter another number: ); scanf( %d, &num2);

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

5. Control Statements

Disk Operating System

PROGRAMMING FUNDAMENTALS

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

SPARK-PL: Introduction

SmallBasicOverview. RCHS Computer Club

Scope of this lecture. Repetition For loops While loops

Java Bytecode (binary file)

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

CS 115 Lecture 8. Selection: the if statement. Neil Moore

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Quiz 1: Functions and Procedures

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Programming Basics. Digital Urban Visualization. People as Flows. ia

Controlling Macro Flow

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Introduction to C# Applications

Pace University. Fundamental Concepts of CS121 1

Full file at

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

G Programming Languages - Fall 2012

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Quick Reference Guide

CPS122 Lecture: From Python to Java

COP 1220 Introduction to Programming in C++ Course Justification

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CS354 gdb Tutorial Written by Chris Feilbach

Vi & Shell Scripting

Full file at

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide

Transcription:

Programming Fundamentals Computers are really very dumb machines -- they only do what they are told to do. Most computers perform their operations on a very primitive level. The basic operations of a computer system is called the computer's instruction set. In order to solve a problem using a computer, we must express the solution to that problem in a language that the computer can understand - through the instruction set. A computer program is just a collection of the instructions necessary to solve a specific problem. The approach or method that we use to solve the problem is called an algorithm. To develop a program to solve a particular problem, we first express the solution to the problem in terms of an algorithm. With the algorithm in hand, we can then write the instructions necessary to implement the algorithm on a particular computer system. Example - Algorithms Problem: Algorithm #1: Algorithm #2: Develop a program that tests if a number is even or odd. Divide the number by two. If the remainder is zero, then the number is even. Otherwise (the remainder is one), the number is odd. Test the least significant bit of the number. If the bit is one, then the number is odd. Otherwise (the bit is zero), the number is even. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 1 of 11

Higher-Level Languages When computers were first developed, the only way they could be programmed was with binary numbers that corresponded directly to the machine instructions and locations in the computer's memory. (Machine language). Assembly language enabled the programmer to work with the machine on a slightly higher level. A special program called an assembler translates the assembly language programs from its symbolic format into machine language. Because a one-to-one correspondence exists between assembly language instructions and machine language instructions, assembly language is called a low-level language. The programmer must still learn the instruction set of the particular computer system in order to write a program. Assembly language programs are not portable -- they will not run on a different type of computer without being rewritten (machine-dependent). Operations of a higher-level language are much more sophisticated -- one statement would result in many different machine instructions being executed. Standardization of the syntax of a higher-level language mean that a program could be written to be machine independent -- a program could run on any machine that supported the language with few or no changes. An interpreter is a special program that translates the statements of a program developed in a higher-level language into machine language. This operation is done on-the-fly as the interpreter reads each high-level language statement, it executes one or more machine-language instructions. An compiler is a special program that translates the statements of a program developed in a higher-level language into machine language by producing a separate machine-language program called an executable. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 2 of 11

Operating Systems An operating system is a (machine-language) program that controls the entire operation of a computer system. All input/output (I/O) operations are channeled trough the operating system. The operating system must also manage the computer's resources and must handle the execution of machine-language (i.e., executable) programs. Writing and Running Interpreted Programs An interpreter is a program that analyzes a program developed in a particular computer language and then executes it on your particular computer system. The program that is to be interpreted is first typed into a file on the computer. A text editor must usually be used to enter a program into a file. Once the source program has been entered into a file, you can then run it through the interpreter for that particular language. The interpretation process is initiated by typing a special command. When this command is entered, the name of the file that contains the program must also be specified. Sometimes you only have to give the name of the program, and the operating system will automatically run the interpreter for it. Step 1: the interpreter examines each statement in the program and checks for syntax and semantic errors. Step 2: the interpreter translates each statement to a "lower" form -- usually directly to machine language. When the program is executed, each of the statements of the program is sequentially executed. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 3 of 11

If the program requests any data from the user, called input, the program will temporarily suspend its execution and wait for the user to enter the data. Results that are displayed by the program are called output. (Normally displayed on the screen or terminal.) If all goes well (and it probably won't the first time the program is executed), the program will do what it is supposed to do. If things go wrong, it will be necessary to go back and re-analyze the program's logic. This is called debugging -- you try to remove all the bugs (features?) from the program. In order to debug a program, the source code usually has to be changed. Then the entire process of interpreting and executing the program is repeated... The Programmer's Drinking Song "99 bugs in the code, 99 little bugs. Fix one bug, run it again,... 100 bugs in the code." E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 4 of 11

Structured Programming and Control Structures In this course, we teach structured programming, a disciplined approach to programming that results in programs that are easy to read and understand and are less likely to cause errors. We follow accepted program style guidelines (such as using meaningful names for identifiers) to write code that is adequately documented with comments and is clean and readable. Obscure tricks and programming shortcuts are strongly discouraged. Program maintenance involves modifying a program to remove previously-undetected bugs and to keep it up-to-date. It is not uncommon to maintain a program for five years or more, often after the programmers who originally coded it have left the company or have moved onto other positions. Control Structures Structured programming uses control structures to control the flow of statement execution in a program. The control structures of a programming language let us combine individual statements into a single program entity with one entry point and one exit point. We can then write a program as a sequence of control structures rather than a sequence of individual statements. There are three categories of control structures: 1. Sequence 2. Selection 3. Repetition (iteration) E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 5 of 11

Sequence In batch files, sequence is illustrated by statements (commands). Control flows from statement 1 to statement 2 and so on from the top of the batch file until the bottom. The command interpreter never goes backwards or skips over commands unless you use selection and repetition structures in your programs. Selection Selection structures allow the computer to make decisions in your programs. In all of our previous algorithms, we execute each algorithm step exactly once in order. Often, we are faced with situations which require two or more alternative courses of action, based on the input data. For example, a program to predict your health based on your resting heart rate. In batch files, we use if statements with logical (Boolean) expressions to form selection structures. Boolean expressions evaluate to either true or false. There are eight (8) forms of if statements. In each case, the if statement will cause the computer to execute a specified command if the Boolean condition evaluates to true. If the condition is false, then the command is ignored. In most cases, the command will be a goto. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 6 of 11

if "string1" == "string2" command if not "string1" == "string2" command string1 and string2 can be a combination of literal characters (i.e., letters, numbers, and symbols that you can type on the keyboard) and variable names. If string1 is identical to string2 (the same count of the same characters in the same order) then the condition is true and the command is executed or not. The quotes around each string are used as placeholders, just in case one of the strings is empty (i.e., contains no characters.) if errorlevel n command if not errorlevel n command n is the error level number from 0 to 255. Zero is used to indicate that no error has occurred (the previous command has completed successfully.) A non-zero value means something went wrong with the previous command. if exist file command if not exist file command if exist directory\nul command if not exist directory\nul command Here, both file and directory can be a simple filename, a combination of directory names and filenames, separated by backslashes, with optional drive letter and colon. Variables can also be used. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 7 of 11

Important: In the case of checking if a directory exists, the \nul must be added to the name of the directory in question. Batch files can only check for the existence of files; nul is the name of a special (imaginary) file that happens to exist in each directory. Repetition The third element of structured programming is repetition, which causes the computer to execute a sequence of operations several times. The only way to do repetition in batch files is with the goto command and a matching label that appears before the goto. Such a structure is called a loop. + -> :labelname loop statement(s) that are repeated +--- goto labelname The colon character (:) identifies a label, which the command interpreter remembers. Besides looping, goto is also used for two other special cases: branching and subroutine calls. Goto is related to selection statements in two ways: 1. The usual command for an if statement is a goto, to branch to a label when the condition is true. (This is called a conditional branch; a goto by itself is called an unconditional branch.) 2. An if statement is required to exit from a loop structure; otherwise, the batch file will contain an infinite (endless) loop. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 8 of 11

Examples 1. Sequence echo Hello echo Goodbye First Hello is output; then Goodbye. 2a. Selection - simple if command if "%1" == "/?" goto help Checks the first command-line parameter (%1) for a /? switch requesting help. If the condition is true (i.e., the left side is identical to the right side), the goto command is executed and branches to a label called help. If the first command-line parameter is not provided (i.e., empty) or anything other than /?, the goto is skipped. In both cases, the batch file resumes sequential processing: either after the :help label (the true case) or after the if statement (the false case). 2b. Selection using an if-else structure xcopy c:\ w:\ /e /i /d /c /r /y if errorlevel 0 goto copyok echo Error backing up files from drive C to drive W. goto end :copyok... There is no else keyword in batch files, instead an if statement, two labels and two goto statements are used. In the above example, the xcopy command is used to backup all files and directories on drive C that have changed since the last backup. The files are copied to drive W (presumably a network drive on a server or other PC.) E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 9 of 11

If the xcopy command succeeds, then it will set the errorlevel to zero. Otherwise, it will set errorlevel to a non-zero value (typically, but not necessarily, to 1). Since we cannot guarantee what the errorlevel will be set to when an error occurs, it is safer to check for errorlevel zero. If no errors occurred, the condition is true and the goto copyok command will skip over the echo and goto end statements, and sequence will resume after the copyok label. If an error occurred, the goto copyok command will be skipped and sequence will continue with the echo and goto end commands. (The end label is not shown, but it is somewhere well after the copyok label.) 3a. Repetition - endless loop :loop echo Batch files suck! goto loop This will repeat the echo statement over and over and over again, until: 1) the user presses either a Ctrl-C or Ctrl-Break key combination and opts to terminate the batch file; 2) the user closes the Command Prompt window; 3) the user ends the VDM process using the Task Manager; 4) the user logs off or shuts down Windows; 5) Windows crashes (e.g., Blue Screen of Death); 6) the power goes out and/or the UPS battery drains; 7) the PC suffers a hardware failure; 8) any or all of the above events in combination. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 10 of 11

3b. Repetition - conditional loop :getfile shift if "%1" == "" goto end copy %1 %todir% goto getfile :end In this case (from Lab #2), the batch file reads command-line parameters, representing files to be copied. The shift, if, and copy statements are repeated until the if condition is true, that is, there are no more files left to copy (%1 will be an empty string.) The goto end statement causes the loop to end. The end label is the last line in the batch file, and causes the batch file to end, as well. E:\2005F\ctec1863\week03\programming_batch_files.wpd Page 11 of 11