Writing a computer program is like creating a recipe
Sridhar NarayanDepartment of Computer Science
Program vs. a recipe - 1A recipe describes a culinary process that
produces a dishDifferent recipes produce different dishesThe same dish may have different recipes
(variations on a theme)A computer program describes a
computation process that produces one or more resultsDifferent programs describe different computationsThe same computation can be described by
different programs
Program vs. a recipe - 2A recipe is a script
Script is written by the author of the recipeAuthor envisions the dish to be produced and
creates the corresponding recipeA computer program is a script
Script written by the programmerProgrammer envisions the computational
process to be performed and creates the appropriate program
Program vs. a recipe - 3A chef follows a recipe
Chef may have some freedom to deviate from the script and improvise
In a computer program, the computer (i.e. CPU) follows the programCPU has no freedom to deviate from the
script. It follows it exactly as written.
Program vs. a recipe - 4The recipe must be written in a language the
chef understandsIf the author of the recipe writes in a foreign
language, the recipe must be translated before the chef can use it
A computer’s native language is machine languageIf the computer program is written in a foreign
language, i.e. non machine-language, it must be translated before it can be executed. A compiler is a computer program that performs this translation.
Program TranslationYou can write computer programs in any
language for which such a translator exists, or can be written
At this time, translators do not exist (nor is it known how to write them) for natural languages, i.e. English, Spanish, German, Russian etc.
Programming LanguagesA computer programming language is
typically English-like but is considerably more restrictive
Like natural languages, a computer programming language has a vocabulary and rules of grammar.
You have to be familiar with the vocabulary and the rules of a programming language before you can write computer programs in it.
The C programming languageJava, Python, Fortran, C, are all examples of
programming languages.In this class, you will be programming in a
language named C.
Program vs. a recipe - 5The recipe author may use a text editor (like
Notepad) or a word processor to write the recipe.Note that the word processor cannot write the
recipe on its own. However, it may help the process, for example by providing templates for recipes.
A programmer may use a text editor (like Notepad) to write the computer programNote that the editor cannot write the program
on its own. However, it may help the process, for example by providing templates for programs.
Program vs. a recipe - 6An Interactive Development Environment
(IDE) like Code::Blocks is an environment that allows a programmer to use a single interface to conveniently:Write the program using an editorCompile itDebug itExecute itRevise it
Program vs. a recipe - 7An involved recipe may be divided into many
parts. For example, preparation the night before, preparation right before you cook, etc.
A C program may be divided into many functions, each of which has a unique name.Execution of a C program always begins
with a function named main.Initially, all the programs you write will feature
only the main function. Later, you will write programs that define other functions besides main.
Program vs. a recipe - 8Recipe
Has many stepsChef follows the steps in sequence, starting with step 1Later steps often depend on the results of earlier steps
A C programHas many statementsComputer follows the statements in sequence, starting
with statement 1Statements that appear later in a program often
depend on the results of statements that appear earlier
Recipe exampleList of ingredients
3 potatoes1 cup olive oil1 teaspoon salt
Detailed description of what to do, in what order, with those ingredients1. Chop potatoes into 1 inch cubes2. Grease baking sheet with olive oil3. Spread potatoes evenly on baking sheet4. Roast potatoes at 300 degrees Fahrenheit for 10
minutes5. Season with the salt.
Computer program: ingredientsList of variables. Think of these as
ingredients for a computation.Each variable has a name: x, y, gradeEach variable has a type : int, float, doubleA type specifies what values can be assigned to
that variable. For example, int – integers, i.e. whole numbers without a
fractional component. For example, 10, 34, 23456. float – floating point numbers, i.e. numbers that
can have a fractional component. For example, 12.2, 345.102
Example: int a = 24;
Computer programA detailed description of what operations
need to be performed, in order, using the variables
A simple C program#include <stdio.h>#include <stdlib.h>
int main(void) {
int a, b, c;
a = 10;
b = 20;
c = a + b;
printf(“Here is the result\n”);
printf(“%i + %i = %i\n”, a, b, c);
return 0; //that says return zero
}