IPC144 Introduction to Programming Using C Week 4 – Lesson 1

Slides:



Advertisements
Similar presentations
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Python quick start guide
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
CPS120 Introduction to Computer Science Iteration (Looping)
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Lesson #5 Repetition and Loops.
User-Written Functions
REPETITION CONTROL STRUCTURE
ECE Application Programming
Lesson #6 Modular Programming and Functions.
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Lesson #6 Modular Programming and Functions.
Lecture 7: Repeating a Known Number of Times
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
Java for Beginners.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Topics Introduction to Repetition Structures
Agenda Control Flow Statements Purpose test statement
Lesson #6 Modular Programming and Functions.
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Java for Beginners University Greenwich Computing At School DASCO
Lesson #5 Repetition and Loops.
IPC144 Week 10 – Lesson 2 Working with Files
Functions, Part 1 of 3 Topics Using Predefined Functions
1) C program development 2) Selection structure
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Module 4 Loops.
Computer Science Core Concepts
Control Structures Part 1
Introduction to Repetition Structures
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
In C Programming Language
EPSII 59:006 Spring 2004.
Topics Introduction to Repetition Structures
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Chapter 4: Repetition Structures: Looping
ECE 103 Engineering Programming Chapter 18 Iteration
Lesson #5 Repetition and Loops.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Statements in C Programming
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Functions, Part 1 of 3 Topics Using Predefined Functions
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
CPS125.
Switch Case Structures
Presentation transcript:

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

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

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

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

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”); } 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…

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 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");

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

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