Examples Using Loops, Lecture Slide - Computer Science, Slides of C programming

Loop Statements, Examples

Typology: Slides

2010/2011

Uploaded on 10/05/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #10:
Examples Using Loops
Http://flint.cs.yale.edu/cs112/
2
Review: Loop Statements
qwhile statement
qdo statement
qfor statement
while ( condition )
statement1;
do {
statement list;
} while ( condition );
statement
true
true
condition
evaluated
false
false true
true
condition
evaluated
statement
false
false
for (initialization; condition; increment )
statement;
statement
true
true
condition
evaluated
false
false
increment
initialization
3
Outline
rAdmin. and review
ØExample programs using loops
4
Example 1
rWrite a program to get a sequence of
integers from the user until the user inputs 0.
Print the largest number.
5
Example 2
rWrite a program to get a sequence of
integers from the user until the user inputs 0.
Print the second largest number.
6
Example 3
rGet a number from 1 to 15 from the user and
print a triangle using ‘*’.
*
***
*****
*******
*********
***********
*************
***************
Example: user inputs 8. See Triangle.cs

Partial preview of the text

Download Examples Using Loops, Lecture Slide - Computer Science and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #10:

Examples Using Loops

Http://flint.cs.yale.edu/cs112/

Review: Loop Statements

q while statement

q do statement

q for statement

while ( condition ) statement1;

do { statement list; } while ( condition );

statement

truetrue

condition

evaluated

falsefalse

truetrue

condition evaluated

statement

falsefalse

for (initialization; condition; increment ) statement;

statement

truetrue

condition

evaluated

falsefalse

increment

initialization

Outline

r Admin. and review

ÿ Example programs using loops

Example 1

r Write a program to get a sequence of

integers from the user until the user inputs 0.

Print the largest number.

Example 2

r Write a program to get a sequence of

integers from the user until the user inputs 0.

Print the second largest number.

Example 3

r Get a number from 1 to 15 from the user and

print a triangle using ‘*’.

Example: user inputs 8. See Triangle.cs