170 likes | 208 Views
Learn about TRUE/FALSE values in C, for loops, and the concept of functions. Explore examples and practice exercises to enhance your programming skills. Understand modularity by creating simple functions and organizing code efficiently.
E N D
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
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 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.
Practice • REALITY CHECK (Week 4 – Lesson 1) • Questions #1 (Walk-thru)
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 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…
Practice REALITY CHECK (Week 4 – Lesson 1) Questions #2 (Word Problem)
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 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 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>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…
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 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: 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"); }
Practice REALITY CHECK (Week 4 – Lesson 1) Questions #3 (Word Problem)
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).