Lecture Slides for Repetition, While Loop, Do While Loop, For Loop | EE 114L, Study notes of Electrical and Electronics Engineering

Material Type: Notes; Professor: Xiang; Class: PROG CONCEPTS FOR ENGINEERING; Subject: Electrical Engineering; University: Montgomery College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 03/11/2009

koofers-user-gf5
koofers-user-gf5 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
EE114:
Programming Concepts for Engineers
Lecture 5
2
Overview
nRepetition
nwhile loop
ndo-while loop
nfor loop
3
while loop
Expression Statement
True
False
Exit
Entry
while (expression)
statement;
while (expression) {
statement0;
statement1;
}
4
Example 1
#include <stdio.h>
int main( void )
{
int x = 0;
while( x <= 10 ) {
printf( "%d\t%d\t%d\n", x, x*x, x*x*x );
x++;
}
return 0;
}
000
111
24 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
\t --- Tab character, create a fixed number of spaces.
pf3
pf4

Partial preview of the text

Download Lecture Slides for Repetition, While Loop, Do While Loop, For Loop | EE 114L and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

1

EE114:

Programming Concepts for Engineers

Lecture 5

2

Overview

n Repetition

n while loop n do-while loop n for loop

3

while loop

Expression TrueStatement

False

Exit

Entry

while (expression) statement;

while (expression) { statement0; statement1; }

4

Example 1

#include <stdio.h> int main( void ) { int x = 0; while( x <= 10 ) { printf( "%d\t%d \t%d\n", x, xx, xx*x ); x++; } return 0; }

0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000

\t --- Tab character, create a fixed number of spaces.

5

Example 2: Sentinel-Controlled Loops

#include <stdio.h> int main( void ) { int grade; int counter=0; printf("Enter grade (-1 to end): "); scanf("%d", &grade); /* input first grade / while( grade != -1 ) { counter ++; /increase the counter / printf("Enter grade (-1 to end): "); scanf("%d", &grade); / get next grade / } printf("You entered %d grades.\n", counter); /display counter*/ return 0; }

A sentinel value is used to terminate the loop (such as -1). Sentinel-controlled loops are also called indefinite loops.

6

do-while loop

Expression

Statement

True

False

Exit

Entry

do statement; while (expression);

do { statement0; statement1; } while (expression);

7

for loop

for (expression1; expression2; expression3) statement;

Counter-Controlled Loops

expression1 -- initialization expression2 -- condition expression3 -- (increment) Expression

Expression

True

False

Exit

Entry

Statement (^) Expression

8

Example 3

#include <stdio.h> int main() { int i, n, sum=0; /initialize sum to 0 is important / printf ("Enter value of n: "); scanf ("%d", &n); / input value of n/ for ( i = 1; i <= n; i++ ) /*loop from 1 to n / sum = sum + i ; /add i to sum */ printf( "Sum of integers from 1 to %d is %d\n", n , sum ); return 0; }

Write a program to calculate the sum of the integers from 1 to n. The value of n is entered from the keyboard.

13

/Sample Code for Example 5, Lecture 5/

#include <stdio.h>

int main( void ) { int i, j ;

for( i = 1; i <= 5; i++) { /* 5 rows / for( j = 1; j <= 5; j ++ ) { / 5 columns / printf( "%dx%d=%d\t", i, j, ij) ; /display 25 times/ } printf(" \n") ; /* Control 5 displays per row */ } return 0 ; }