300 likes | 324 Views
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.
E N D
Introduction to Programming Using C Week 2 – Lesson 2 (Pages 19 to 23, 26 to 30 in IPC144 Textbook) IPC144
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
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)
Take-up Homework • REALITY CHECK (Previous lesson) • Question #2
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…
Walk-Thrus Instructor Demonstration: • Refer to walkThruDemo.c in the following location:http://matrix.senecac.on.ca/~murray.saul/ipc144/week2/
Practice • From what we have learned as of now, let’s try the previous lesson’sREALITY CHECK handout, Question #3 (Walk-thru)
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 */ }
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
Logic Visual Example #1 of if statement:
Logic Visual Example #1 of if statement: Is age > 65?
Logic Visual Example #1 of if statement: Is age > 65?TRUE
Logic Visual Example #1 of if statement: TRUE Is age > 65?TRUE Display “you can retire!”
Logic Visual Example #2 of if statement:
Logic Visual Example #2 of if statement: Is age > 65?
Logic Visual Example #2 of if statement: Is age > 65?FALSE
Logic Visual Example #2 of if statement: Is age > 65?FALSE FALSE
Logic if statement – A Visual Overview: TRUE Is age > 65? Display “you can retire!” FALSE
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…
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…
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
Logic if - else statement: TRUE Is age > 65? FALSE Display “you can retire!” Display “Back to work!”
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…
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
Logic Nested if statements: TRUE Is age > 65? TRUE FALSE Is fund > $1,000,000? Display “you can retire!” FALSE
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…
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
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”); }
Practice • From what we have learned as of now, let’s try the REALITY CHECK handout, Question #3 (Walk-thru)
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!!!