1 / 30

Introduction to Programming Using C - Arithmetic Operations, Walk-Thrus, and Logic

Learn about arithmetic operations with different data types, how to perform a walk-thru of a program, and the logic behind if and if-else statements. Practice coding examples and gain a deeper understanding of programming concepts.

Télécharger la présentation

Introduction to Programming Using C - Arithmetic Operations, Walk-Thrus, and Logic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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:http://matrix.senecac.on.ca/~murray.saul/ipc144/week2 • 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:http://matrix.senecac.on.ca/~murray.saul/ipc144/week2/

  7. Practice • From what we have learned as of now, let’s try the previous lesson’sREALITY 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 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 usethe “=” sign, sincethis would assign thevalue of num2 into num1! Since num1 is NOT equal to num2, thecondition is FALSE, so the statements inthe 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 requiredeg. 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 requiredsince 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 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 statementdoesn’t test a conditionbut executes if previouscondition tested is FALSE Again, similar to if statementyou use braces (code block) ifthe else statement executes morethan 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 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 > 1000000) printf(“You can retire!\n\n); Notice how indentingcode blocks makes thecode easier to read… Notice that the second ifstatement is the only statementcontained in the originalif statement – therefore nobraces 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 > 1000000) printf(“You can retire!\n\n);else if ( fund < 1000000) 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!!!

More Related