Loops functions in C and some examples, Lecture notes of C programming

How to use for loops,while loops and do while loops in C

Typology: Lecture notes

2016/2017

Uploaded on 08/28/2017

byron-ramon-banzon
byron-ramon-banzon 🇵🇭

5

(1)

5 documents

1 / 53

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 4
Iterative Constructs
Functions
EEE 11
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35

Partial preview of the text

Download Loops functions in C and some examples and more Lecture notes C programming in PDF only on Docsity!

Lecture 4

Iterative Constructs

Functions

EEE 11

Preprocessor Directives

● Commands that give instructions to the C preprocessor

● A preprocessor is a program that takes text and performs

lexical conversions on it.

− Prepares data for the compiler/interpreter

● The conversions may include macro substitution, conditional

inclusion, and inclusion of other files.

● Preprocessor directives begins with a

− Two examples: #include directive #define directive

Preprocessor Directives

● #define directive

− Can be used for compile-time constants − Cannot be used for things that vary during program runtime. − Supports parameterized macros (more advanced usage)

Syntax: #define NAME expansion

Example: #define KMS_PER_MILE 1.

#define PI 3.

Programming Style: Constants

● Use the #define preprocessor for constants

● Poor code area = radius * radius * 3.14159; circumference = 2 * radius * 3.14159;

● Better code #define VAL_PI 3. ... area = radius * radius * VAL_PI; circumference = 2 * radius * VAL_PI;

− Makes code more readable − Easier to remember − Easier to maintain

Iterative/loop constructs

● Repetitions in program code

● Sample problems.

− Input handling ● Ask for another input if user gave an invalid input ● Prompt user if they want to give another input

− Repetitive tasks ● Compute pay for several employees (same formula) ● Counting number of inputs

Main components of a loop

● Loops involving a control variable:

− Initialization ● The controlling variable must be initially set to a known valid state. − Testing (the loop repetition condition) ● An expression involving the controlling variable must be evaluated to determine if the loop will continue or not. − Update ● The controlling variable must be updated in a way that the loop repetition condition will eventually terminate the loop

While syntax

● Standard while syntax:

initialization

while ( repetition condition ) {

loop body Update } − The loop repetition expression is evaluated to determine if the loop will execute the statement

i = 0; while (i <= 5) { i = i + 1; }

Sample 04-01 (while)

count_emp < 7

count_emp = 0

True

False Display “all employees processed”

END

Display pay

Input hours, rate

pay = hours*rate

count_emp = count_emp+

Sample 04-02 (for)

count_emp < 7

count_emp = 0

True

False Display “all employees processed”

END

Display pay

Input hours, rate

pay = hours*rate

count_emp = count_emp+

WHILE vs FOR loop

● while-loop

Initialization ;

while ( Test ) { body; Update ; }

● For-loop

for( Initialization ; Test ; Update ) { body; }

Multiple initializations

● Initialization and updates are not limited to one

/* Double counters */ int main (void) { int x; int y;

for ( x=0, y=0; x<1000 && y<2000; x+=1, y+=2) { printf("%d \t %d\n", x, y); } return (0); }

Analyzing loops

● Use tables

− Columns are variables/expressions/conditions − Rows are loop iteration

● What is the output?

Suggested columns: i, i<=5, 10-i

i = 0; while (i <= 5) { printf("%3d %3d\n", i, 10-i); i = i + 1; }

do-while statement

● Loops that must execute at least once are

implemented in C using a do-while statement.

● Both the for and while statements evaluate the

repetition condition BEFORE executing the

statement block.

do-while statement

● Standard Syntax:

initialization; do { Loop body update } while ( repetition condition );

● Loop body is executed at least once.

● Note the semicolon (;) at the end

do { printf("Enter the number 5: "); scanf("%d", &number); } while (number != 5);