


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Xiang; Class: PROG CONCEPTS FOR ENGINEERING; Subject: Electrical Engineering; University: Montgomery College; Term: Unknown 1989;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



1
2
n while loop n do-while loop n for loop
3
Expression TrueStatement
False
Exit
Entry
while (expression) statement;
while (expression) { statement0; statement1; }
4
#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
#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
Expression
Statement
True
False
Exit
Entry
do statement; while (expression);
do { statement0; statement1; } while (expression);
7
for (expression1; expression2; expression3) statement;
Counter-Controlled Loops
expression1 -- initialization expression2 -- condition expression3 -- (increment) Expression
Expression
True
False
Exit
Entry
Statement (^) Expression
8
#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 ; }