Introduction to Programming

Slides:



Advertisements
Similar presentations
The if-else Statements
Advertisements

CS 1 Introduction CS 1 Part 11. Hardware 1.Central Processing Unit (CPU) 2.Main Memory 3.Secondary Memory / Storage 4.Input Devices 5.Output Devices CS.
Jason Howard. Agenda I. How to download robotc II. What is tele-op used for? III. How to build a basic tele-op program IV. Getting the robot to drive.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Overview of Programming and Problem Solving ROBERT REAVES.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
C Programming for engineers Teaching assistant: Ben Sandbank Home page:
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
The switch Statement, DecimalFormat, and Introduction to Looping
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Lecture 2b Dr. Robert D. Kent.  Structured program development  Program control.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Controlling Program Flow with Decision Structures.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Introduction to Computing Systems and Programming Programming.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Selection (also known as Branching) Jumail Bin Taliba by
The switch Statement, and Introduction to Looping
EGR 2261 Unit 4 Control Structures I: Selection
Unit 2 Programming.
Selection CIS 40 – Introduction to Programming in Python
Introduction to Programming
Coding Concepts (Basics)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
ICT Programming Lesson 1:
Chapter 3: Selection Structures: Making Decisions
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Introduction to Programming MVRT 115

This is an introduction to programming in the C language. We will be working on Vex and NXT robots, and later on the large robot. In this class, we assume that you know little or nothing about programming in general. If you have programmed in C, C++, or Java, or if you were programming on the team last year, there will be a different class for you. In the advanced class, we will be working on the sensors and camera. We will talk about style, efficiency, testing and debugging.

Just the Basics A computer is an electronic machine that can do a limited number of things, like adding, subtracting, multiplying, dividing, or comparing two numbers. A computer can input numbers from keyboards, disk drives, and sensors. The computer can output numbers to a computer screen, a disk, a printer, or a motor. Input & output is collectively referred to as I/O. Computer instructions are done in sequence, one after the other until the last instruction is executed; then the computer stops. A set of computer instructions is known as a program. Numbers can be stored (& retrieved) from a computer’s memory. We say a program runs or executes. We can perform or execute an instruction

Programming Languges C C++ Java

Programming Languages Programming in machine language is tedious & error-prone for humans. Computer memory locations are numbers. E.g., the first 256 bytes of memory are locations 0 through 255. (Everything on computers starts counting at zero.) For example, to add two numbers in memory locations 150 and 200: Get the first number from memory location 150 Get the second number from memory location 200 Add the two numbers together Store the addition result in memory (maybe location 204)

Programming Languages Programming languages, like C & Java, make things easier: adding two numbers can be done in one line of C, and you don’t have to know memory locations!, e.g., sum = a + b; * A bit is a single binary digit: either 0 or 1. A byte is a set of 8 binary digits and can represent 256 different values.

The Most Important Thing The most important thing to know about programming is that computers do what you tell them to do. Not what you you’d like them to do. Not what you think they should do. You must know how to translate what you want into a form the computer can use. The syntax must be correct. The logic must be correct. When the computer does something “wrong,” it’s almost certain that you programmed it to do the “wrong” thing.

Basic Programming Form Every program must contain zero or more statements enclosed in curly brackets “{“ and “}” Every statement must end with a semicolon ; A program is a series of statements Statements are executed in order until we run out of statements. But… You can skip statements with “if” statements You can repeat statements with loops You can block groups of statements with “{“ and “}” This is useful for “if” and “while”

Syntax Syntax just means the way a statement is put together. Computer and human languages must have correct syntax, and every language has its own syntax . You understand “the dog my homework ate,” although it’s syntax is wrong for English. (However, the syntax would be right for German: “Der Hund meine Aufgaben frass.”) Computers never understand incorrect syntax!

A C Program main() { // a “//” is a comment int x = 3; // x is a variable int y, z; // y & z are variables x = x + 2; // assignment statement if (x < 6) // if statement { y = 2; // more assignment statements z = 5; }

Variables How does a computer get a program? You load it into memory You store programs and information (data) in memory. You don’t (usually) need to know where the program is in memory; the computer knows where to look. For example, your program might start at memory locations (the addresses of bytes of memory) 100 to 200 and your data at memory addresses 201 to 275. It’s difficult for humans to deal with numbered memory addresses, so you can name them. Named addresses are called variables. You must declare (name) a variable before you can use it. int x; names a variable x. Where is it in memory? Who cares; you can just call it x.

Assignment How do you put values into a variable? With an assignment statement. x = 3; // put a 3 in variable x The “=“ sign means put whatever is on the right side into the left side. The right side value is not changed. The right hand side of the “=“ can be an expression. x = y *3 + 48;

IF statement Sometimes you want to do something only in certain conditions, so you can use an “if” statement. If the expression inside the parentheses is true, then you do the statements between the “{“ and “}” otherwise you skip them. if (motor_speed > 255) { motor_speed = 255; }

In the line-following example, you were introduced to if statements The elementary form is if (condition) { … statements } The statements are performed if “condition” is true, otherwise the statements between { and } are skipped.

Defining philosophical truth is difficult; C programming, however, truth is easy to define. A condition is true if it evaluates to non-zero. A condition is false if it evaluates to zero This can take a little getting used to, but basically all C expressions must evaluate to a number

Let’s look at some conditions if (1) if (-1) if (0) if (x > 3) if ((x < 3 * v && r < 1) || 1) Because expressions evaluate to a number, you can (but shouldn’t) do some weird things: j = h > q; // if h > q then assign 1 to j, else assign 0 x = !x; // if x ≠ 0 assign 0 to x, else assign 1 x = !(!x);

Looking at the line-follower code, each of the 3 “if” statements can be true or false and may be executed in any combination

But if you want to test for exclusive conditions, use “else” clauses if (condition 1) // Order is important for logic and efficiency { ... } else if (condition 2) … else if (condition 47) else … // come here when all the other conditions are false

Switch Statement If you have many conditions, consider using a switch (case) construct #define FORWARD 483 switch (condition) { case 1: // must be number or constant expression … break; // if you leave out break, the next case executed: BAD! case FORWARD: break; default: }

Training Days Mondays: Engineering Wednesday: Electrical Thursdays: Media - We will also be working on the robot to prepare for CAL games on Thursday – This is an open build meeting - all members are encouraged to come and work with veteran members to prepare for competitions – we will let you know each week if we are meeting on Thursday – This week we are not. Fridays: Mechanical