04 Es26 Lab - Program Data And Actions

  • Uploaded by: Wilmarc
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 04 Es26 Lab - Program Data And Actions as PDF for free.

More details

  • Words: 776
  • Pages: 19
Program Data and Actions

4

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 Program A

Source code

Output

#include<stdio.h> Hello world. void main(void) { printf(“Hello world.”); }

Program B

#include<stdio.h> void main(void) { printf(“Hello world.\n”); printf(“I’m so happy!”); }

Hello world. I’m so happy!

Introduction to Programming

Sequential Execution Source code

Output

Program C

#include<stdio.h> void main(void) { printf(“Hello world.\n”); printf(“I’m so happy!\n)’ printf(“How about you?”); }

Hello world. I’m so happy! How about you?

Introduction to Programming

Data and Instructions Computer Memory Program Instructions

Data

Introduction to Programming

Variables grade Computer memory

95

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 (09) 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 break case char const continu e default do double

• • • • • • • • •

else enum extern float for goto if int long

• • • • • • • • •

register return short signed sizeof static struct switch typedef

Introduction to Programming

• • • • •

union unsigne d void volatile while

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

Related Documents


More Documents from "Wilmarc"

Mscsgradapp
May 2020 8
09 Es26 Lab - Loops 2
May 2020 14
11 Es26 Lab - Arrays
May 2020 14
08 Es26 Lab - Loops
May 2020 6
13 Es 26 - Strings
May 2020 4