IPC144 Introduction to Programming Using C Week 2 – Lesson 2

Slides:



Advertisements
Similar presentations
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Advertisements

Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Week 4 - Monday.  What did we talk about last time?  Wrapper classes  if statements.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Controlling Program Flow with Decision Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Discussion 4 eecs 183 Hannah Westra.
Control Structures I Chapter 3
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
CprE 185: Intro to Problem Solving (using C)
Chapter 2 - Introduction to C Programming
Chapter 4 MATLAB Programming
Programming Mehdi Bukhari.
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
IF statements.
Week 4 - Monday CS 121.
Programming 101 Programming for non-programmers.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Algorithms, Part 2 of 3 Topics Problem Solving Examples Pseudocode
Chapter 2 - Introduction to C Programming
Algorithm and Ambiguity
CS 240 – Lecture 11 Pseudocode.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Chapter 2 - Introduction to C Programming
Conditions and Ifs BIS1523 – Lecture 8.
Program Design Introduction to Computer Programming By:
IPC144 Week 10 – Lesson 2 Working with Files
1) C program development 2) Selection structure
During the last lecture we had a discussion on Data Types, Variables & Operators
Iteration: Beyond the Basic PERFORM
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Java Programming Control Structures Part 1
If selection construct
Unit 3 Test: Friday.
CS139 October 11, 2004.
Algorithm and Ambiguity
Computing Fundamentals
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Flowcharts and Pseudo Code
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Relational Operators.
Loops.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Chapter 3: Selection Structures: Making Decisions
Relational and Logical Operators
CprE 185: Intro to Problem Solving (using C)
Using C++ Arithmetic Operators and Control Structures
Review of Previous Lesson
Relational and Logical Operators
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
The while Looping Structure
Intro to Programming (in JavaScript)
Switch Case Structures
Presentation transcript:

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

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’s REALITY 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 Display “you can retire!” 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 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…

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…

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 FALSE Display “you can retire!” 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 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…

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 TRUE FALSE Display “you can retire!” 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 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…

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