Download presentation
Presentation is loading. Please wait.
Published byΠαρθενιά Δάβης Modified over 5 years ago
1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
(Pages 38 to 46 in IPC144 Textbook)
2
Agenda Additional C programming Elements
TRUE / FALSE values( 1 / non-1) for loop Examples Modularity: Concept / Purpose of functions Creating a Simple Function
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” then next time condition is checked, result becomes 0 (FALSE) thus exits loop. Each time loop occurs, value of count 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" 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 for loop syntax: int x; for ( x = 1; x < 5; x++ ) {
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”); } main() { title(); } Just like variables must indicate the type of data returned from a function. If no data returned, use void data-type… The function title() has already been defined, and when the main() program runs, the function is run by 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 indicate that there is a function below the main() program. Notice the function header ends with a semi-colon… #include<stdio.h> void title(); main() { title(); } void title() printf ("****************************\n"); printf ("* Report Title *\n");
16
Practice REALITY CHECK (Week 4 – Lesson 1) Questions #3 (Word Problem)
17
Homework TASK #1 TASK #2 TASK #3 TASK #4 *** Highly Recommended ***
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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.