programming concepts (part a) ping hsu. what is a program? white cake recipe 1.preheat oven to 350...
Post on 20-Dec-2015
218 views
TRANSCRIPT
Programming Concepts(Part A)
Ping Hsu
What is a program?WHITE CAKE RECIPE
1. Preheat oven to 350 degrees F (175 degrees C). 2. Grease and flour a 9x9 inch pan.3. In a medium bowl, cream together the sugar and butter.4. Beat in the eggs.5. Stir in the vanilla. 6. Combine flour and baking powder, add to the creamed mixture and mix well. 7. Stir in the milk until batter is smooth. Pour or spoon batter into the
prepared pan. 8. Bake for 30 to 40 minutes in the preheated oven.
A computer program is an ordered list of instructions.
It’s like a recipe!
In Summary…
A math example…y = [ 3 × ( a × a + 7) ] / b + 4
a2
a2 + 7
3 × (a2 + 7) = 3a2 + 21
(3a2 + 21) / b
(3a2 + 21) / b + 4Sequential Solving following Math conventions for correct answer.
Consider the sequential execution of the above expressions to find the value of y:
• p = a x a;• q = p + 7;• r = 3 x q;• s = r / b;• y = s + 4;
y = [ 3 × ( a × a + 7) ] / b + 4
An Empty EasyC program
C programFlow chart
Your program goes here.
Your program goes here.
C program
Logic flow
An instruction in C
Variables• A “variable” is a place where we keep a value.
A = 10 ; // The value 10 is stored in A.
• Any statement after the sign “//” is NOT part of the program. It is a comment made by the programmer.
WSalary = 800 ; // Any statement can go here.
• A semicolon is required at the end of each C instruction.
Variables, cont.
Every variable in the program needs to be declared in the beginning of the program.
int speed ;
Declaration of the variable tells the program its name and its type
The word “int” indicates that the variable ‘speed’ is an integer variable.
Commonly Used Variable TypesVariable
TypeDescription Range
Int Stores integer valuesEX: 5, -3200
-32,768 to +32,767
Long Stores integer values with extended rangeEX: 56, 6000, -4,234,128
-2,147,483,648 to +2,147,483,647
float Stores values with decimal pointEX: 1.245, -4.2341
[-10^+38, -10^-38]0[10^-38, 10^+38]
char Stores characters*EX: A, B, @, #
_
• Global Variable: This variable is accessible from
anywhere within your program.
• Local Variable: This variable is only accessible from a
“local area” within your program (will be discussed later).
Global Variable Declaration To declare a global variable, click on the tab
“Global” in the Main program.
Global Variable Declaration (A close-up view)
Assignment Operator
• In C language “ = ” is an assignment operator, not an “ is equal to ” statement.
• A = 10; // means assign 10 to A.• A = B; // means assign the value of B to A• A = A+1; //means assign the value A+1 // back to A, i.e., increase A by 1.
Assignment Operator: Examples:int A; // a variable named A of type integerint B; // a variable named B of type integerA = 10; // value 10 is assigned to variable AB = (24+16)/2; // 20 is assigned to variable BA = A + 15; // value of (A+15) is first evaluated and then assigned to A. So now A=(10+15)=25B = A + B ; // Now A = 25, B = (25+20)=45A = B – 40; // Now A=(45-40)=5, B=45A = A * B; // Now A=(5*45)=225, B=45B = A / 9; // Now A=225, B=25
Q1: What is the value in B at the end of this program?
int A;int B;A = 12;B = 15;A = A + (B/3) ;B = A + B – 7 ;
(A)12, (B)15, (C)17, (D)20, (E)25
Decision Making• The ability to make decision is the most basic form of
intelligence.
• A linear (sequential) execution of instructions can only perform a simple task that does not involve decision making.
• The ‘if’ instruction gives a C program decision making ability
IF Statement: Simple Example
if (Number == 0) { PrintToScreen (“The Number is Zero”); }if (Number < 0) { PrintToScreen (“The Number is negative”); }
This == symbol is used for checking ‘equal’ condition, not for value assignment.
Only if this condition is true, this instruction is executed.
IF-Else Statement
Consider the following program:
If (score <60){ PrintToScreen(“You failed the test”);}
If (score == 60) // ‘==‘ means ‘if equal to’{ PrintToScreen(“You passed the test”);}
If (score > 60){ PrintToScreen(“You passed the test”);}
IF-Else Statement
• IF-else statement should be used where there are only two possible cases.
If (score <60){ PrintToScreen(“You failed the test”);}
else{ PrintToScreen(“You passed the test”);}
Q2: What is the value of A and B at end of this program?
A = 9 ;B = 12 ; if ((A + B) > 22) { A = B ; B = A; }else { B = A; A = B; }
(A) A=12, B=21(B) A=9, B=12(C) A= 9, B=9(D) A=12, B=12(E) A=12, B=9
While Statement• In C, the ‘while’ statement is useful for
repeating a set of instructions Suppose we have to add the first 50 positive
integers 1+2+3+4+……………………+48+49+50
First Approach: It can be implemented by a single
statement:
int SUM ; // integer variable for the resultSUM = 1+2+3+………………..+48+49+50;
Second Approach: int SUM ; //variable SUM is declared as integerSUM = 0 ; //SUM is assigned the value 0SUM = SUM + 1 ; // Now SUM = (0 + 1) = 1SUM = SUM + 2 ; // SUM = (1 + 2) = 3SUM = SUM + 3 ; // SUM = (3 + 3) = 6
SUM = SUM + 49 ; //SUM = (1176 + 49) = 1225SUM = SUM + 50; //SUM = (1225 + 50) = 1275
Third approach: Use “while” statement
This condition is checked first. If it is true, the block of instructions enclosed by the curly brackets { } is executed.
This block of instructions is executed repeatedly until the condition is not true.
While Statement - Example
• To find factorial of a number N.What is a factorial?Factorial of a number N is 1x2x3x………………x(N-1)xN
• Factorial (1) = 1• Factorial (5) = 1x2x3x4x5 = 120• Factorial (7) = 1x2x3x4x5x6x7 = 5040
Q3: What is the final value of Aint A;int i;A = 0;i = 0;while (i < 3) { A = A + i; i = i + 1; }
(A) 2(B) 3(C) 4(D) 6(E) 10
SolutionInitially i = 0, A = 0.First iteration: condition 0<3 is trueA = 0+0=0, i = 1Second iteration : condition 1<3 is trueA = 0+1=1, i = 2Third iteration : condition 2<3 is trueSo A = 1+2=3, i = 3Fourth iteration : condition 3<3 is falseSo the while loop stops.Final Value of A is 3.
Infinite Loop• In the above example programs we have
employed a condition checking in order to control the flow of execution. i.e., we have made the loop to repeat only a finite number of times.
• We can also make the loop to repeat infinite number of times.
In the following example program, the status of a bumper switch is continuously monitored and depending on its value the movement of the robot is decided.
while (1==1)
{ status = GetDigitalInput(5); // read bumper
if (status ==1 ) // bumper hits something
{ SetMotor(2,127);} // Stop the robot
else
{
SetMotor(2,0); // Move forward
}
}