SlideShare a Scribd company logo
1 of 22
Chapter 5
Repetition
and Loop
Statements
Instructor:
Kun-Mao Chao
( 台大資工 趙坤
茂 )
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-2
Repetition in Programs
• In most software, the statements in the program
may need to repeat for many times.
– e.g., calculate the value of n!.
– If n = 10000, it’s not elegant to write the code as
1*2*3*…*10000.
• LoopLoop is a control structure that repeats a group
of steps in a program.
– Loop bodyLoop body stands for the repeated statements.
• There are three C loop control statements:
– whilewhile, forfor, and do-whiledo-while.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-3
Flow Diagram of Loop Choice Process
e.g., calculate the value of n!
e.g., read the content in a file
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-4
Comparison of Loop Choices (1/2)
Kind When to Use C Structure
Counting loop We know how many loop
repetitions will be needed
in advance.
while, for
Sentinel-
controlled loop
Input of a list of data ended
by a special value
while, for
Endfile-
controlled loop
Input of a list of data from
a data file
while, for
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-5
Comparison of Loop Choices (2/2)
Kind When to Use C Structure
Input validation
loop
Repeated interactive input
of a value until a desired
value is entered.
do-while
General
conditional
loop
Repeated processing of
data until a desired
condition is met.
while, for
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-6
The while Statement in C
• The syntax of while statement in C:
while (loop repetition conditionloop repetition condition)
statement
• Loop repetition conditionLoop repetition condition is the condition
which controls the loop.
• The statement is repeated as long as the loop
repetition condition is truetrue.
• A loop is called an infinite loopinfinite loop if the loop
repetition condition is always true.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-7
An Example of a while Loop
Statement
Loop repetition condition
Loop control variableLoop control variable is the variable whose value controls
loop repetition.
In this example, count_emp is the loop control variable.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-8
Flowchart for a while Loop
Loop repetition condition
Statement
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-9
Compound Assignment Operators
(1/2)
• The loop body usually consists of statements of
the form: variable = variable op expression.
– e.g., count_emp = count_emp + 1;
• C provides compound assignment operatorscompound assignment operators
which enable a more concise notation for this
kind of statements.
– ““variable op = expression”variable op = expression” is the same to
“variable = variable op expression.”
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-10
Compound Assignment Operators
(2/2)
Simple Assignment
Operators
Compound Assignment
Operators
count_emp =
count_emp + 1;
count_emp += 1;
time = time -1; time -= 1;
product = product *
item;
product *= item;
total = total /
number;
total /= number;
n = n % (x+1); n %= x+1;
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-11
The for Statement in C
• The syntax of for statement in C:
for (initialization expressioninitialization expression;
loop repetition conditionloop repetition condition;
update expressionupdate expression)
statement
• The initialization expressioninitialization expression set the initial value of the
loop control variable.
• The loop repetition conditionloop repetition condition test the value of the loop
control variable.
• The update expressionupdate expression update the loop control
variable.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-12
An Example of the for Loop
Loop repetition condition
Initialization Expression
Update Expression
count_emp is set to 0 initially.
count_emp should not exceed the value of number_emp.
count_emp is increased by one after each iteration.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-13
Increment and Decrement Operators
• The statements of increment and decrement are
commonly used in the for loop.
• The increment (i.e., ++++) or decrement (i.e., ----)
operators are the frequently used operators
which take only one operand.
• The increment/decrement operators increase or
decrease the value of the single operand.
– e.g., for (int i = 0; i < 100; i++i++){ … }
– The variable i increase one after each iteration.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-14
Comparison of Prefix and Postfix
Increments
The value of the expression (that uses the ++/-- operators)
depends on the position of the operator.
The value
of j is
increased
The value
of j is not
increased
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-15
Sentinel-Controlled Loops
• Sometimes we may not know how many times
the loop will repeat.
• One way to do this is to choose a sentinel valuesentinel value
as an end marker.
– The loop exits when the sentinel valuesentinel value is read.
• If the user wish to exit the loop, he or she has to
input the sentinel valuesentinel value.
– It is similar to the “logout” function in many
applications.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-16
An Example of Sentinel-Controlled
while Loops
If the user wish to exit the loop,
he or she has to input -99.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-17
An Example of Endfile-Controlled
Loops
• fscanffscanf is a function used to read file.
• EOFEOF stands for the special value of end-file returned by
fscanffscanf.
• This loop repeats until reading the end of the file.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-18
Nested Loops
• Nested loops consist of an outer loopouter loop with one
or more inner loopsinner loops.
• e.g.,
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){
…
}
}
• The above loop will run for 100*50 iterations.
Inner loop
Outer loop
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-19
The do-while Statement in C
• The syntax of do-while statement in C:
do
statement
while (loop repetition conditionloop repetition condition);
• The statement is first executed.
• If the loop repetition conditionloop repetition condition is true, the
statement is repeated.
• Otherwise, the loop is exited.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-20
An Example of the do-while Loop
/* Find even number input */
do{
printf(“Enter a value: ”);
scanf(“%d”, &num);
}while (num % 2 !=0)
This loop will repeat if the user
inputs odd number.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-21
Homework #4 (1/2)
• Write a program that prompts the user to input
an integer n.
• Draw a triangle with n levels by star symbols.
For example,
n = 3,
*
**
***
• After drawing the triangle, repeat the above
process until the user input a negative integer.
Copyright ©2004 Pearson
Addison-Wesley. All rights
reserved. 5-22
Homework #4 (2/2)
• An usage scenario:
Please input: 2
*
**
Please input: 3
*
**
***
Please input: -9
Thank you for using this program.

More Related Content

Similar to Chapter05

Similar to Chapter05 (20)

Week04
Week04Week04
Week04
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5eEo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
 
Cso gaddis java_chapter4
Cso gaddis java_chapter4Cso gaddis java_chapter4
Cso gaddis java_chapter4
 
Control structure
Control structureControl structure
Control structure
 
Loops and Files
Loops and FilesLoops and Files
Loops and Files
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Programming
ProgrammingProgramming
Programming
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
C loops
C loopsC loops
C loops
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 
Loops c++
Loops c++Loops c++
Loops c++
 
prt123.pptx
prt123.pptxprt123.pptx
prt123.pptx
 
M C6java6
M C6java6M C6java6
M C6java6
 
Deeksha gopaliya
Deeksha gopaliyaDeeksha gopaliya
Deeksha gopaliya
 
final pl paper
final pl paperfinal pl paper
final pl paper
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
 
Looping in c language
Looping in c languageLooping in c language
Looping in c language
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

Chapter05

  • 2. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-2 Repetition in Programs • In most software, the statements in the program may need to repeat for many times. – e.g., calculate the value of n!. – If n = 10000, it’s not elegant to write the code as 1*2*3*…*10000. • LoopLoop is a control structure that repeats a group of steps in a program. – Loop bodyLoop body stands for the repeated statements. • There are three C loop control statements: – whilewhile, forfor, and do-whiledo-while.
  • 3. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-3 Flow Diagram of Loop Choice Process e.g., calculate the value of n! e.g., read the content in a file
  • 4. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-4 Comparison of Loop Choices (1/2) Kind When to Use C Structure Counting loop We know how many loop repetitions will be needed in advance. while, for Sentinel- controlled loop Input of a list of data ended by a special value while, for Endfile- controlled loop Input of a list of data from a data file while, for
  • 5. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-5 Comparison of Loop Choices (2/2) Kind When to Use C Structure Input validation loop Repeated interactive input of a value until a desired value is entered. do-while General conditional loop Repeated processing of data until a desired condition is met. while, for
  • 6. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-6 The while Statement in C • The syntax of while statement in C: while (loop repetition conditionloop repetition condition) statement • Loop repetition conditionLoop repetition condition is the condition which controls the loop. • The statement is repeated as long as the loop repetition condition is truetrue. • A loop is called an infinite loopinfinite loop if the loop repetition condition is always true.
  • 7. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-7 An Example of a while Loop Statement Loop repetition condition Loop control variableLoop control variable is the variable whose value controls loop repetition. In this example, count_emp is the loop control variable.
  • 8. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-8 Flowchart for a while Loop Loop repetition condition Statement
  • 9. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-9 Compound Assignment Operators (1/2) • The loop body usually consists of statements of the form: variable = variable op expression. – e.g., count_emp = count_emp + 1; • C provides compound assignment operatorscompound assignment operators which enable a more concise notation for this kind of statements. – ““variable op = expression”variable op = expression” is the same to “variable = variable op expression.”
  • 10. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-10 Compound Assignment Operators (2/2) Simple Assignment Operators Compound Assignment Operators count_emp = count_emp + 1; count_emp += 1; time = time -1; time -= 1; product = product * item; product *= item; total = total / number; total /= number; n = n % (x+1); n %= x+1;
  • 11. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-11 The for Statement in C • The syntax of for statement in C: for (initialization expressioninitialization expression; loop repetition conditionloop repetition condition; update expressionupdate expression) statement • The initialization expressioninitialization expression set the initial value of the loop control variable. • The loop repetition conditionloop repetition condition test the value of the loop control variable. • The update expressionupdate expression update the loop control variable.
  • 12. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-12 An Example of the for Loop Loop repetition condition Initialization Expression Update Expression count_emp is set to 0 initially. count_emp should not exceed the value of number_emp. count_emp is increased by one after each iteration.
  • 13. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-13 Increment and Decrement Operators • The statements of increment and decrement are commonly used in the for loop. • The increment (i.e., ++++) or decrement (i.e., ----) operators are the frequently used operators which take only one operand. • The increment/decrement operators increase or decrease the value of the single operand. – e.g., for (int i = 0; i < 100; i++i++){ … } – The variable i increase one after each iteration.
  • 14. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-14 Comparison of Prefix and Postfix Increments The value of the expression (that uses the ++/-- operators) depends on the position of the operator. The value of j is increased The value of j is not increased
  • 15. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-15 Sentinel-Controlled Loops • Sometimes we may not know how many times the loop will repeat. • One way to do this is to choose a sentinel valuesentinel value as an end marker. – The loop exits when the sentinel valuesentinel value is read. • If the user wish to exit the loop, he or she has to input the sentinel valuesentinel value. – It is similar to the “logout” function in many applications.
  • 16. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-16 An Example of Sentinel-Controlled while Loops If the user wish to exit the loop, he or she has to input -99.
  • 17. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-17 An Example of Endfile-Controlled Loops • fscanffscanf is a function used to read file. • EOFEOF stands for the special value of end-file returned by fscanffscanf. • This loop repeats until reading the end of the file.
  • 18. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-18 Nested Loops • Nested loops consist of an outer loopouter loop with one or more inner loopsinner loops. • e.g., for (i=1;i<=100;i++){ for(j=1;j<=50;j++){ … } } • The above loop will run for 100*50 iterations. Inner loop Outer loop
  • 19. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-19 The do-while Statement in C • The syntax of do-while statement in C: do statement while (loop repetition conditionloop repetition condition); • The statement is first executed. • If the loop repetition conditionloop repetition condition is true, the statement is repeated. • Otherwise, the loop is exited.
  • 20. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-20 An Example of the do-while Loop /* Find even number input */ do{ printf(“Enter a value: ”); scanf(“%d”, &num); }while (num % 2 !=0) This loop will repeat if the user inputs odd number.
  • 21. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-21 Homework #4 (1/2) • Write a program that prompts the user to input an integer n. • Draw a triangle with n levels by star symbols. For example, n = 3, * ** *** • After drawing the triangle, repeat the above process until the user input a negative integer.
  • 22. Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 5-22 Homework #4 (2/2) • An usage scenario: Please input: 2 * ** Please input: 3 * ** *** Please input: -9 Thank you for using this program.