Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPC144 Introduction to Programming Using C Week 2 – Lesson 2

Similar presentations


Presentation on theme: "IPC144 Introduction to Programming Using C Week 2 – Lesson 2"— Presentation transcript:

1 IPC144 Introduction to Programming Using C Week 2 – Lesson 2
(Pages 19 to 23, 26 to 30 in IPC144 Textbook)

2 Agenda Arithmetic Operations with different data-types
Take-up REALITY CHECK – Question #2 How to Perform a Walk-Thru of a simple program REALITY CHECK - Walk Thrus (Question 3) Logic (Concept / Overview) Relational Operators (<, <=. >. >=, ==, !=) -if / if – else / Nested if / if – elseif – else statements examples

3 Arithmetical Operations
Refer to arithmeticDataTypes.c in the following location: What happens when using arithmetic operators involving variables of different numeric data-types (eg. ints and doubles)

4 Take-up Homework REALITY CHECK (Previous lesson) Question #2

5 Walk-Thrus In addition to being able to create a C program from a “word problem”, you will be required to be given a piece of code, and write the intended output as if it has been compiled and run. There are many reasons why you should learn to perform walk-thrus 50% of your tests and final exams consist of walkthrus Performing walk-thrus help “test to see” if you understand material Carefully performing steps in the program (like a computer does) give you a “tool” to help troubleshoot or solve a program that is not working. A walk-thru allows you to see the program “from the perspective of the computer running that program”. Refer to the slide show of a simple walk-thru demonstration…

6 Walk-Thrus Instructor Demonstration:
Refer to walkThruDemo.c in the following location:

7 Practice From what we have learned as of now, let’s try the previous lesson’s REALITY CHECK handout, Question #3 (Walk-thru)

8 Logic “What distinguishes programming from simply typing in formulae is the ability to tell the computer to do different things under different conditions…” IPC144 Subject Notes (Chapter 3) In C programming, the if statement is one way to instruct the computer to decide what should be done. The if statement tests some condition, and execute a statement or series of statements if the condition is true For example: if (test condition) { /* statements to execute if TRUE */ }

9 Logic Relational Operators
The simplest kinds of conditions to test in if statements involve the comparison of numeric amounts (using relational operators). Relational Operators > Greater than >= Greater than or equal to < Less than <= Less than or equal to == equal to != Not equal to

10 Logic Visual Example #1 of if statement:

11 Logic Visual Example #1 of if statement: Is age > 65?

12 Logic Visual Example #1 of if statement: Is age > 65? TRUE

13 Logic Visual Example #1 of if statement: TRUE
Is age > 65? TRUE Display “you can retire!”

14 Logic Visual Example #2 of if statement:

15 Logic Visual Example #2 of if statement: Is age > 65?

16 Logic Visual Example #2 of if statement: Is age > 65? FALSE

17 Logic Visual Example #2 of if statement: Is age > 65? FALSE FALSE

18 Logic if statement – A Visual Overview: TRUE Display “you can retire!”
Is age > 65? Display “you can retire!” FALSE

19 Logic Example: int num1 = 2, num2 =5; if (num1 == num2) { printf(“Hey!\n”); printf(“num1 and num2 are the same!\n\n”); } Note: Do not use the “=” sign, since this would assign the value of num2 into num1! Since num1 is NOT equal to num2, the condition is FALSE, so the statements in the if code block are NOT executed…

20 Logic Code Blocks If there is only 1 statement to be executed in the if statement, then braces are NOT required eg. if ( age >= 65 ) printf(“You can retire!\n\n); if ( age <65 ) { printf (“Sorry,\n”); printf (“You are %d years old…\n”, age); printf (“You cannot retire – back to work!\n\n); } Only 1 statement used… (no brace required….) Braces are required since more than 1 statement is used…

21 Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #1 Using the handout, plan and then code your program

22 Logic if - else statement: TRUE FALSE Display “you can retire!”
Is age > 65? FALSE Display “you can retire!” Display “Back to work!”

23 Logic Coding of if – else statement:
eg. if ( age >= 65 ) printf(“You can retire!\n\n); else printf(“Back to work!\n\n); Notice the else statement doesn’t test a condition but executes if previous condition tested is FALSE Again, similar to if statement you use braces (code block) if the else statement executes more than 1 statement…

24 Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #2 Using the handout, plan and then code your program

25 Logic Nested if statements: TRUE TRUE FALSE Display “you can retire!”
Is age > 65? TRUE FALSE Is fund > $1,000,000? Display “you can retire!” FALSE

26 Logic Coding of Nested if statements:
eg. if ( age >= 65 ) if ( fund > ) printf(“You can retire!\n\n); Notice how indenting code blocks makes the code easier to read… Notice that the second if statement is the only statement contained in the original if statement – therefore no braces are required…

27 Logic if – else if - else statements:
Another way of achieving the similar results of a nested-if statement, is using the else if statement… The else if statement is used to test another condition if the previous if or else if condition was FALSE. You can use more than one else if statement in your logic

28 Logic Example of if – else if – else statement:
eg if ( age >= 65 ) else if ( fund > ) printf(“You can retire!\n\n); else if ( fund < ) printf(“You may still need to work a few years!\n\n”); else { printf (“Forget about retiring for now\n”); pirntf (“… and get back to work!\n\n”); }

29 Practice From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 (Walk-thru)

30 Homework TASK #1 Come to the lab, and learn how to create, compile, run, and printout a simple C program. This is due in one week from when assigned in the lab. TASK #2 Complete REALITY CHECK (Last lesson) – Question #3 (Walk-thru). Complete today’s REALITY CHECK if we didn’t finish in class… TASK #3 You are starting to grow a large number of examples. For homework, practice coding, compiling and running these programs. Make small modifications to the programs, and perhaps, make programs with mistakes, compile and check for errors. Then DEBUG (correct) your program until you can run your program. Always remember to check your program when running for accuracy!!!


Download ppt "IPC144 Introduction to Programming Using C Week 2 – Lesson 2"

Similar presentations


Ads by Google