1 / 17

IPC144

Introduction to Programming Using C Week 4 – Lesson 1 (Pages 38 to 46 in IPC144 Textbook). IPC144. Agenda. Additional C programming Elements TRUE / FALSE values( 1 / non-1) for loop Examples Modularity: Concept / Purpose of functions Creating a Simple Function Examples.

tlin
Download Presentation

IPC144

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 4 – Lesson 1 (Pages 38 to 46 in IPC144 Textbook) IPC144

  2. Agenda Additional C programming Elements • TRUE / FALSE values( 1 / non-1) • for loop • Examples • Modularity: • Concept / Purpose of functions • Creating a Simple Function • Examples

  3. TRUE / FALSE Values • So far, we have learned how to test conditions with logic such as an if / else if statement, or test conditions with an indeterminate loops such as a while or do-while statement. • In the C programming language, the TRUE result from testing a condition returns the value 1, and any value other than 1 indicates a FALSE result. Since computers work with numbers very quickly, it makes sense that the 1 or non 1 values are used to indicate TRUE or FALSE respectively.

  4. TRUE / FALSE Values • Below is an example of a program that uses values in a while loop: int x = 1, count = 0;while(x){ printf (“This is a line\n”); if ( count > 3 ) x = 0; count = count + 1;} Since the value of x is 1 (TRUE)then while statement will loop. When count become “3” thennext time condition is checked, resultbecomes 0 (FALSE) thus exits loop. Each time loop occurs, value ofcount is increased by a unit.

  5. Practice • REALITY CHECK (Week 4 – Lesson 1) • Questions #1 (Walk-thru)

  6. Loops • There are 2 category of loops: • Determinant Loops (for() statement)‏ • The number of repetitions are known. For example, repeating display of "Hello" 4 times.... • Indeterminant Loops ( while() do-while() statements)‏ • The number of repetitions is unknown. • Repetitions depend on user input. For example, repeating display of "Hello" until user (when prompted) enters number 0 to exit...

  7. Loops Inside brackets are 3 items:- x is initially given a value.- x is compared to a condition- value of x is increased by 1 for loop syntax: int x; for ( x = 1; x < 5; x++ ){ statement(s);} If condition tests TRUE,then execute statement(s)in code block. If FALSE,exit for statement…

  8. Practice REALITY CHECK (Week 4 – Lesson 1) Questions #2 (Word Problem)

  9. Modularity / Functions • As you are starting to work on your assignment #1, you may notice how quickly the coding of even a simple program can get complex. • The major technique for dealing with this complexity is to subdivide a program into a series of smaller program, sometimes called modules or functions.

  10. Modularity / Functions • Perhaps without you knowing it, you have already been using these smaller programs (i.e. functions). • When you put at the top of your program, you are instructing #include<stdio.h> the compiler to link to the “Standard Input/Ouput library” containing such functions (statements to you) as printf() or scanf().

  11. Modularity / Functions • Just like printf() and scanf() functions, you can create your own function to help complete a task. • Some functions ( like printf() and scanf() ) can contain data within the bracket to be used by the function. • Other functions, can simply run without containing data within the bracket…

  12. Modularity / Functions Here is an example: #include<stdio.h>void title(){ printf (“****************************\n”); printf (“* Report Title *\n”); printf (“****************************\n”); } main(){title();} Just like variablesmust indicate thetype of data returnedfrom a function. If nodata returned, usevoid data-type… The function title() has alreadybeen defined, and when the main()program runs, the function is runby calling it by name…

  13. Modularity / Functions • Notice in the previous example, the function appears BEFORE the main() program. • The reason for this is that the function must be “defined” prior to it being executed in the main() program. • It is like the idea of forgetting to place #include<stdio.h> at the top of your program when using printf() or scanf() functions

  14. Modularity / Functions • Most programmers prefer functions to appear after the main() program. • This can be done by using function headers that give instructions to the compiler that functions are contained in the program • See the next slide how by using a function header can allow us to re-arrange the order of the main() program and title() function.

  15. Modularity / Functions Here is an example: A function header is used to indicatethat there is a function below the main()program. Notice the function headerends with a semi-colon… #include<stdio.h> void title(); main() { title(); } void title() { printf ("****************************\n"); printf ("* Report Title *\n"); printf ("****************************\n"); }

  16. Practice REALITY CHECK (Week 4 – Lesson 1) Questions #3 (Word Problem)

  17. Homework • TASK #1 • Complete lab #3 since it is due at the beginning of this week’s lab! • TASK #2 • Study for a quiz to be held in this week’s lab based on material taught last week • TASK #3 • Complete and code the solutions for today’s REALITY CHECK QUESTIONS, as well as “hide” and repeat the walk-thru questions • TASK #4 *** Highly Recommended *** • Read ahead in IPC144 Programming Notes (Modularity / Functions).

More Related