IPC 144 Introduction to Programming Using C Week

  • Slides: 17
Download presentation
IPC 144 Introduction to Programming Using C Week 4 – Lesson 1 (Pages 38

IPC 144 Introduction to Programming Using C Week 4 – Lesson 1 (Pages 38 to 46 in IPC 144 Textbook)

Agenda Additional C programming Elements TRUE / FALSE values( 1 / non-1) for loop

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

TRUE / FALSE Values So far, we have learned how to test conditions with

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.

TRUE / FALSE Values Below is an example of a program that uses values

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

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

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

Loops There are 2 category of loops: • Determinant Loops (for() statement) • The

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

Loops for loop syntax: Inside brackets are 3 items: - x is initially given

Loops for loop syntax: Inside brackets are 3 items: - x is initially given a value. - x is compared to a condition - value of x is increased by 1 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…

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

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

Modularity / Functions As you are starting to work on your assignment #1, you

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.

Modularity / Functions Perhaps without you knowing it, you have already been using these

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().

Modularity / Functions Just like printf() and scanf() functions, you can create your own

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…

Modularity / Functions Here is an example: #include<stdio. h> Just like variables must indicate

Modularity / Functions Here is an example: #include<stdio. h> Just like variables must indicate the type of data returned from a function. If no data returned, use void data-type… void title() { printf (“**************n”); printf (“* Report Title *n”); printf (“**************n”); } main() { title(); } The function title() has already been defined, and when the main() program runs, the function is run by calling it by name…

Modularity / Functions Notice in the previous example, the function appears BEFORE the main()

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

Modularity / Functions Most programmers prefer functions to appear after the main() program. This

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.

Modularity / Functions Here is an example: #include<stdio. h> void title(); main() { title();

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

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

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

Homework TASK #1 TASK #2 Study for a quiz to be held in this

Homework TASK #1 TASK #2 Study for a quiz to be held in this week’s lab based on material taught last week TASK #3 Complete lab #3 since it is due at the beginning of this week’s lab! 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 IPC 144 Programming Notes (Modularity / Functions).