Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Programming Concepts (Part A) Ping Hsu

2 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…

3 A math example… y = [ 3 × ( a × a + 7) ] / b + 4 a2a2 a 2 + 7 3 × (a 2 + 7) = 3a 2 + 21 (3a 2 + 21) / b (3a 2 + 21) / b + 4 Sequential Solving following Math conventions for correct answer.

4 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

5 An Empty EasyC program C program Flow chart Your program goes here.

6 C program Logic flow An instruction in C

7 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.

8 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.

9 Commonly Used Variable Types Variable Type DescriptionRange Int Stores integer values EX: 5, -3200 -32,768 to +32,767 Long Stores integer values with extended range EX: 56, 6000, -4,234,128 -2,147,483,648 to +2,147,483,647 float Stores values with decimal point EX: 1.245, -4.2341 [-10^+38, -10^-38] 0 [10^-38, 10^+38] char Stores characters* EX: A, B, @, # _

10 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).

11 Global Variable Declaration To declare a global variable, click on the tab “Global” in the Main program.

12 Global Variable Declaration (A close-up view)

13 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.

14 Assignment Operator: Examples: int A; // a variable named A of type integer int B; // a variable named B of type integer A = 10; // value 10 is assigned to variable A B = (24+16)/2; // 20 is assigned to variable B A = A + 15; // value of (A+15) is first evaluated and then assigned to A. So now A=(10+15)=25 B = A + B ; // Now A = 25, B = (25+20)=45 A = B – 40; // Now A=(45-40)=5, B=45 A = A * B; // Now A=(5*45)=225, B=45 B = A / 9; // Now A=225, B=25

15 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

16 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

17 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.

18 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”);}

19 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”);}

20 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

21 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

22 First Approach: It can be implemented by a single statement: int SUM ; // integer variable for the result SUM = 1+2+3+………………..+48+49+50;

23 Second Approach: int SUM ; //variable SUM is declared as integer SUM = 0 ; //SUM is assigned the value 0 SUM = SUM + 1 ; // Now SUM = (0 + 1) = 1 SUM = SUM + 2 ; // SUM = (1 + 2) = 3 SUM = SUM + 3 ; // SUM = (3 + 3) = 6 SUM = SUM + 49 ; //SUM = (1176 + 49) = 1225 SUM = SUM + 50; //SUM = (1225 + 50) = 1275

24 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.

25 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

26

27 Q3: What is the final value of A int 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

28 Solution Initially i = 0, A = 0. First iteration: condition 0<3 is true A = 0+0=0, i = 1 Second iteration : condition 1<3 is true A = 0+1=1, i = 2 Third iteration : condition 2<3 is true So A = 1+2=3, i = 3 Fourth iteration : condition 3<3 is false So the while loop stops. Final Value of A is 3.

29 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.

30 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 }

31


Download ppt "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."

Similar presentations


Ads by Google