C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.

Slides:



Advertisements
Similar presentations
VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
Advertisements

Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Introduction to C Programming
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
C OMPUTER P ROGRAMMING 1 Introduction to the C Language.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
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 Hints and Tips The preprocessor and other fun toys.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Programming with RAPTOR
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Bill Tucker Austin Community College COSC 1315
Topic: Python’s building blocks -> Variables, Values, and Types
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
Chapter 2, Part I Introduction to C Programming
Variables, Expressions, and IO
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

C OMPUTER P ROGRAMMING 1 Input and Variables

I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from peripheral devices to the processor e.g. input may come from the keyboard or from a disk file. Before we describe input statements, let us consider where to store the information to be read in. We must arrange to store the input so that it can be processed. To do this, we need to introduce the concept of variables.

I/O S TATEMENTS : I NPUT & V ARIABLES Variables can be used to store the data that we input. Sometimes, the data to be input may have many parts. This may require us to use many variables in our program. So, we need to be able to clearly identify each variable that we are going to use. But how do we identify this variable? The solution is easy: we give each variable a name which we use to identify it. So, a variable can be viewed as a named container for a value.

I/O S TATEMENTS : I NPUT & V ARIABLES The statements : char colour[80]; int age; define variables called colour and age. They tell the computer the names of the variables and that colour will hold characters (up to 80 of them), while age will contain a number (in this case a whole number or integer value).

I/O S TATEMENTS : I NPUT & V ARIABLES We call such statements declarations they declare the names of the variables and specify what kind of information the variables may contain e.g. characters or numbers. We use the term type to describe the kind information that may be stored in a variable e.g. in the declaration below int age; the variable age is of type int ( i.e. an integer variable)

I/O S TATEMENTS : I NPUT & V ARIABLES In C and Java, variables must be declared before they can be used. A common error is to omit the declaration of a variable, which results in a syntax error message when we attempt to use the undeclared variable. We can use any name we wish for variables with the exception of the reserved words mentioned earlier. It is a good idea to choose meaningful names for variables, because it make programs easier to understand. This is more difficult than it sounds but is a fundamental principle in writing readable programs

I/O S TATEMENTS : E XAMPLE Consider a program to prompt the user to enter their favourite colour, read this colour and display a message followed by the colour entered by the user. The program may be written in C as follows: /* colour.c: program to prompt for colour and display a message Author: Rem Collier Date: 28/02/2013 */ #include main() { char colour[80]; /* Variable to hold colour */ printf(“Enter your favourite colour: “); gets (colour); /* Read colour from keyboard */ printf(“Yuk! I hate %s“, colour ); }

I/O S TATEMENTS : E XAMPLE If we execute the program the following appears on the screen (the bolded text is that entered by the user - we will use this convention throughout the text). Enter your favourite colour: blue Yuk! I hate blue The first statement in the program declares a variable called colour. Its type is that of a list of characters. The printf() statement is used to display the message prompting for input.

I/O S TATEMENTS : E XAMPLE The statement gets(colour); handles the input from the keyboard. It reads text from the keyboard, for example the word blue may be entered, and it places the text in the variable colour.

I/O S TATEMENTS : E XAMPLE The statement printf(“Yuk ! I hate %s“, colour ); ! instructs the computer to display on the screen the message Yuk! I hate blue Note that %s does NOT appear on the screen, it has been replaced by the value of the variable colour i.e. blue in this example. It is the method used to tell printf() to display the value of a variable. The name of the variable to be displayed, follows the message. The %s indicates to printf() that the variable contains text (also referred to as a string ) so that printf() can display it appropriately. We use %d to display integers and %f to display real numbers

I/O S TATEMENTS : I NPUT & V ARIABLES We often represent a computer's memory as a list of boxes or containers. A variable as we have said may be regarded as a container or a box in memory. Pictorially, we can represent the colour variable as a location in memory (in fact it occupies 80 locations in memory): Memory blue Value of the variable Name of the variable colour

I/O S TATEMENTS : I NPUT & V ARIABLES We can use gets() to give values to string variables We can use printf() to display the value contained in any variable. We use the expression “ the value of a variable” to mean “the value contained in a variable” We take the phrase “the value of colour is blue” to mean “the value contained in the variable called colour is blue”. We will use the shorter form from now on.

I/O S TATEMENTS : I NPUT & V ARIABLES From the program above, we see that printf() has the ability to display messages enclosed in quotation marks the values of variables. For example: printf ( “What’s your favourite colour“ ); displays the string inside the quotation marks on the screen, and printf ( “%s“, colour ); displays the value of the variable colour, which in this example is also a string i.e. a list of characters. A message enclosed in quotes is called a string literal (or string) as it will never change when the program runs.

I/O S TATEMENTS : I NPUT & V ARIABLES Make sure you understand the difference between: printf( “colour“ ); and printf( “%s“, colour ); In the first case, a string is displayed, i.e. the word colour appears on the screen. In the second case, the value of a variable called colour is displayed. This could be anything, for example the word blue or whatever value the user has given the variable like red, pink and orange There is nothing to prevent you storing more than one word in a string variable.

D OCUMENTING YOUR P ROGRAMS The text between /* and */ is called a comment and is ignored by the computer. This text is used as documentation to help explain to someone reading the program, how the program works. Comments are a very important component of programs. This is because when you read your programs some time after writing them, you may find them difficult to understand, if there are no comments to explain what you were doing. They are even more important if someone else will have to read your programs (e.g. your tutor who is going to grade them!) NOTE: It is good practice to give the program’s filename, the authors name and the date on which the program was written as the first comment in any program.

D ELIBERATE E RROR Remove the end of comment characters */ that are in red on the 7th line of the program. Compile and run the program. What happens ? Why ? /* colour.c: program to prompt for colour and display a message Author: Rem Collier Date: 28/02/2013 */ #include main() { char colour[80]; /* Line 7: Variable to hold colour */ printf(“Enter your favourite colour: “); gets(colour); /* Line 10: Read colour from keyboard */ printf(“Yuk! I hate %s“, colour ); }

D ELIBERATE E RROR E XPLAINED When you remove the end of comment on line 7, the compiler treats the text on the following lines as part of the comment until it reaches line 10 and finds the end of comment characters there. This means that the printf() and gets() statements do NOT get executed. Thus the last printf() which tries to print the value of colour may behave unpredictably. It could even cause the program to crash. This is because you have not given colour a value and so printf() displays whatever happened to be in the variable colour. At a later stage we will explain why this could cause your program to crash.