You are on page 1of 19

Program Data and Actions

Introduction to Programming
Objectives

After completing this lesson, you should be


able to do the following:
• Understand how data are represented in
programs
• Know how to use the basic input and output
functions in a program
• Explain how programs are executed in a
sequential manner
• Use variables in a program
• Perform operations on data

Introduction to Programming
Program Data and Actions

• Every program consists of two components:


– A sequence of actions
– The data that is acted upon

Introduction to Programming
Simple C Programs
Source code Output
#include<stdio.h>
Program A

Hello world.
void main(void)
{
printf(“Hello world.”);
}

#include<stdio.h>
Hello world.
Program B

void main(void) I’m so happy!


{
printf(“Hello world.\n”);
printf(“I’m so happy!”);
}

Introduction to Programming
Sequential Execution

Source code Output

#include<stdio.h>
Hello world.
Program C

void main(void) I’m so happy!


{ How about you?
printf(“Hello world.\n”);
printf(“I’m so happy!\n)’
printf(“How about you?”);
}

Introduction to Programming
Data and Instructions
Computer Memory

Program

Instructions

Data

Introduction to Programming
Variables

grade

Computer 95
memory

int grade;
scanf(“%d”, &grade);

Introduction to Programming
A Program Using A Variable
/* A C program that accepts an integer value and
displays it back to the screen */
#include <stdio.h>
int main(void)
{
int grade;
printf(“Enter the student’s grade: ”);
scanf(“%d”,&grade);
printf(“The grade of the student is %d.”, grade);
return 0;
}
Output
Enter the student’s grade: 95
The grade of the student is 95.
Introduction to Programming
Identifiers

• Identifiers are used to uniquely reference


various objects in a program like variable
names, keywords, constants and function
names among others
• Example: x, y , sum

Introduction to Programming
Identifiers

The creation of identifiers are governed by the


following rules:

1. It consists only of letters (A-Z, a-z), digits (0-


9) and underscores ( _ )
2. It cannot start with a digit
3. It does not contain blank spaces

Introduction to Programming
Valid Identifiers

• Here are some examples of valid identifiers:


x score
room201 courseCode
TRUE student_number
_time Nickname

Introduction to Programming
Identifiers

• There is no restriction on the length of an


identifier
• On some compilers, there is a maximum of
31 characters only
• It is also recommended that it is not a
redefinition of an identifier in a C standard
library such as printf and scanf

Introduction to Programming
C Keywords
• auto • else • register • union
• break • enum • return • unsigne
• case • extern • short d
• char • float • signed • void
• const • for • sizeof • volatile
• continu • goto • static • while
e • if • struct
• default • int • switch
• do • long • typedef
• double Introduction to Programming
Identifiers

• C is a case-sensitive language
• In C, even though two identifiers are spelled
the same, if the case of each corresponding
letter doesn’t match, C regards them as two
different names
• For example, the identifiers student and
Student are considered as two different
identifiers

Introduction to Programming
Invalid Identifiers

• The following identifiers are invalid:

• 1st /* should not start with a digit */


• m&m /* uses illegal character & */
• first year /* space not allowed */

Introduction to Programming
Assignment Statements

• A statement that assigns a value to a variable


is called an assignment statement and has
the format:
variable = expression;

• For example:
x = 100;
y = 200;
x = y;
Introduction to Programming
Swapping Values
/* A C program that swaps the values of two

variables */
#include <stdio.h>
int main(void)
{
int x, y, temp;
printf(“Enter two numbers: ”);
scanf(“%d %d”,&x, &y);
temp = x;
x = y;
y = temp;
printf(“Value of x: %d\n”, x);
printf(“Value of y: %d”, y);
return 0;
Introduction to Programming
}
Performing Calculations on Data
/* A C program that adds and subtracts the values of
two variables */
#include <stdio.h>
int main(void)
{
int x, y;
printf(“Enter two numbers: ”);
scanf(“%d %d”,&x, &y);

printf(“Sum of %d and %d is %d\n”,x,y,x+y);


printf(“Difference of %d and %d is %d”,x,y,x-y);

return 0;
}
Introduction to Programming
Summary

• A program consists of instructions and data


• The computer executes the instructions in sequential
order
• A variable stores data such as an integer value
• The scanf() function can be used to let a user enter a
value for a variable while the printf() function can
display the values of variables
• The assignment statement can be used to assign a
value to a variable specially when performing
arithmetic expressions

Introduction to Programming

You might also like