際際滷

際際滷Share a Scribd company logo
CSC128 Fundamentals of Computer Problem Solving
In this chapter, you will:
 Learn about repetition (looping) control

  structures
 Explore how to construct and use count-

  controlled, sentinel-controlled, flag-controlled
 Discover how to form and use nested control

  structures




                                                     2
   Repetition allows you to efficiently use variables
   Can input, add, and average multiple numbers
    using a limited number of variables
   For example, to add five numbers:
     Declare a variable for each number, input the numbers
      and add the variables together
     Create a loop that reads a number into a variable and
      adds it to a variable that contains the sum of the
      numbers




                                                              3
   The general form of the while statement is:


    while is a reserved word
   Statement can be simple or compound
   Expression acts as a decision maker and is
    usually a logical expression
   Statement is called the body of the loop
   The parentheses are part of the syntax


                                                  4
   Infinite loop: continues to execute endlessly
     Avoided by including statements in loop body that assure
      exit condition is eventually false



                                                                 5
6
   Example 5-1
     Variable i  Loop Control Variable (LCV)
     Within the loop, i becomes 25 but is not printed because the
      entry condition is false.
     What would happen if expression i = i + 5; is omitted?
     What would happen if the statement i=0; is omitted?
     i=0;
      while (i <= 20)
      {
          i = i+5;
          cout << i <<  ;
      }
      Produce semantic error; we rarely want a condition to be true for
      i<=20 and yet produce results for i>20.
     Putting a semicolon at the end of the while loop (after the logical
      expression), then the action is empty. The statement within the
      braces do not form the body of the while loop.


                                                                            7
8
   The LCV must be properly initialized before the while loop.
   LCV should eventually make the expression evaluate to false by
    updationg or reinitializing the LCV in the body of the while loop.
   The form of writing the while loops.
    //initialize the LCV
    while (expression) //expression tests the LCV
    {
       .
       .
       .
       //update the LCV
       .
       .
       .
    }



                                                                         9
1.   What is the output for the following while statement. Use tracing table to trace
     the output.
     int n = 1;
     while (n <= 5)
     {
        n = n + 1;
        cout << n <<  ;
     }

2.   What is the output for the following while statement by using the data below as
     input for variable item. 10 3 4 2 5 11
     int item, data = 1; sum = 0;
     while (sum <= 250)
     {
        cin >> item;
        data = data * item;
        sum = sum + data;
        cout << sum << endl;
     }


                                                                                    10
3.   Write a program to display a series of number as
     shown below:
     40 38 36 34 32 30

3.   Write a program using while loop to find the sum
     of integers 73 through 415 inclusive..


4.   Write a program using while loop to display the
     even numbers of integers 1 through 20 inclusive.


                                                        11
/* Write a program to display
   a series of number as shown below:
                     40 38 36 34 32 30

    */
int i;

i=40;

while (i >= 30)
{
  cout << i << " " ;
  i=i-2;
}


                                          C++ Programming: From
                                    Problem Analysis to Program
                                          Design, Fifth Edition   12
C++ Programming: From
Problem Analysis to Program
      Design, Fifth Edition   13
/* Write a program using while loop
         to display the even numbers of
         integers 1 through 20 inclusive.
         */
int i;

i=1;

while (i <= 20)
{
  if (( (i % 2) == 0)|| (i == 1))
        cout << i << " " ;
  i=i+1;
}

                                                 C++ Programming: From
                                           Problem Analysis to Program
                                                 Design, Fifth Edition   14

More Related Content

What's hot (18)

Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic ExpressionsType Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
PVS-Studio
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
LLVM Overview
LLVM OverviewLLVM Overview
LLVM Overview
Constantin Lungu
Working of while loop
Working of while loopWorking of while loop
Working of while loop
Neeru Mittal
C programming part4
C programming part4C programming part4
C programming part4
Keroles karam khalil
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
Nested loops
Nested loopsNested loops
Nested loops
Neeru Mittal
Technical aptitude Test 1 CSE
Technical aptitude Test 1 CSETechnical aptitude Test 1 CSE
Technical aptitude Test 1 CSE
Sujata Regoti
Programming basics
Programming basicsProgramming basics
Programming basics
Bipin Adhikari
Technical aptitude test 2 CSE
Technical aptitude test 2 CSETechnical aptitude test 2 CSE
Technical aptitude test 2 CSE
Sujata Regoti
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
shashikant pabari
Loops in c
Loops in cLoops in c
Loops in c
baabtra.com - No. 1 supplier of quality freshers
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
Shameer Ahmed Koya
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheet
Ahmed Elshal
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
guptkashish
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
trupti1976
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
Type Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic ExpressionsType Conversion in C++ and C# Arithmetic Expressions
Type Conversion in C++ and C# Arithmetic Expressions
PVS-Studio
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
Working of while loop
Working of while loopWorking of while loop
Working of while loop
Neeru Mittal
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
Technical aptitude Test 1 CSE
Technical aptitude Test 1 CSETechnical aptitude Test 1 CSE
Technical aptitude Test 1 CSE
Sujata Regoti
Technical aptitude test 2 CSE
Technical aptitude test 2 CSETechnical aptitude test 2 CSE
Technical aptitude test 2 CSE
Sujata Regoti
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
Shameer Ahmed Koya
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Priyom Majumder
Basic c# cheat sheet
Basic c# cheat sheetBasic c# cheat sheet
Basic c# cheat sheet
Ahmed Elshal
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
guptkashish
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
trupti1976

Similar to 04a intro while (20)

CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
monicafrancis71118
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
06.Loops
06.Loops06.Loops
06.Loops
Intro C# Book
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
ssuser10ed71
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
Chapter 3
Chapter 3Chapter 3
Chapter 3
Amrit Kaur
Programming in C
Programming in CProgramming in C
Programming in C
Nishant Munjal
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
Munazza-Mah-Jabeen
Lecture 2
Lecture 2Lecture 2
Lecture 2
Mohammed Saleh
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
IIUM
P3
P3P3
P3
lksoo
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
IIUM
Advanced Computer Programming..pptx
Advanced Computer Programming..pptxAdvanced Computer Programming..pptx
Advanced Computer Programming..pptx
KrishanthaRanaweera1
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
Pooja Gupta
Lecture 1
Lecture 1Lecture 1
Lecture 1
Mohammed Saleh
Lecture 1
Lecture 1Lecture 1
Lecture 1
Mohammed Saleh
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
monicafrancis71118
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
ssuser10ed71
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
msharshitha03s
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
Munazza-Mah-Jabeen
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
IIUM
P3
P3P3
P3
lksoo
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
IIUM
Advanced Computer Programming..pptx
Advanced Computer Programming..pptxAdvanced Computer Programming..pptx
Advanced Computer Programming..pptx
KrishanthaRanaweera1
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
rohassanie
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
02 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_0202 iec t1_s1_oo_ps_session_02
02 iec t1_s1_oo_ps_session_02
Pooja Gupta

More from hasfaa1017 (8)

Present perfect tense & past perfect tense
Present perfect tense & past perfect tensePresent perfect tense & past perfect tense
Present perfect tense & past perfect tense
hasfaa1017
Verbs
VerbsVerbs
Verbs
hasfaa1017
Verbs
VerbsVerbs
Verbs
hasfaa1017
Presentation bel 260
Presentation bel 260Presentation bel 260
Presentation bel 260
hasfaa1017
Presentation1 svagr
Presentation1 svagrPresentation1 svagr
Presentation1 svagr
hasfaa1017
Pronouns
PronounsPronouns
Pronouns
hasfaa1017
Bel260 writing1
Bel260 writing1Bel260 writing1
Bel260 writing1
hasfaa1017
Bel260 writing1
Bel260 writing1Bel260 writing1
Bel260 writing1
hasfaa1017
Present perfect tense & past perfect tense
Present perfect tense & past perfect tensePresent perfect tense & past perfect tense
Present perfect tense & past perfect tense
hasfaa1017
Presentation bel 260
Presentation bel 260Presentation bel 260
Presentation bel 260
hasfaa1017
Presentation1 svagr
Presentation1 svagrPresentation1 svagr
Presentation1 svagr
hasfaa1017
Bel260 writing1
Bel260 writing1Bel260 writing1
Bel260 writing1
hasfaa1017
Bel260 writing1
Bel260 writing1Bel260 writing1
Bel260 writing1
hasfaa1017

04a intro while

  • 1. CSC128 Fundamentals of Computer Problem Solving
  • 2. In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct and use count- controlled, sentinel-controlled, flag-controlled Discover how to form and use nested control structures 2
  • 3. Repetition allows you to efficiently use variables Can input, add, and average multiple numbers using a limited number of variables For example, to add five numbers: Declare a variable for each number, input the numbers and add the variables together Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers 3
  • 4. The general form of the while statement is: while is a reserved word Statement can be simple or compound Expression acts as a decision maker and is usually a logical expression Statement is called the body of the loop The parentheses are part of the syntax 4
  • 5. Infinite loop: continues to execute endlessly Avoided by including statements in loop body that assure exit condition is eventually false 5
  • 6. 6
  • 7. Example 5-1 Variable i Loop Control Variable (LCV) Within the loop, i becomes 25 but is not printed because the entry condition is false. What would happen if expression i = i + 5; is omitted? What would happen if the statement i=0; is omitted? i=0; while (i <= 20) { i = i+5; cout << i << ; } Produce semantic error; we rarely want a condition to be true for i<=20 and yet produce results for i>20. Putting a semicolon at the end of the while loop (after the logical expression), then the action is empty. The statement within the braces do not form the body of the while loop. 7
  • 8. 8
  • 9. The LCV must be properly initialized before the while loop. LCV should eventually make the expression evaluate to false by updationg or reinitializing the LCV in the body of the while loop. The form of writing the while loops. //initialize the LCV while (expression) //expression tests the LCV { . . . //update the LCV . . . } 9
  • 10. 1. What is the output for the following while statement. Use tracing table to trace the output. int n = 1; while (n <= 5) { n = n + 1; cout << n << ; } 2. What is the output for the following while statement by using the data below as input for variable item. 10 3 4 2 5 11 int item, data = 1; sum = 0; while (sum <= 250) { cin >> item; data = data * item; sum = sum + data; cout << sum << endl; } 10
  • 11. 3. Write a program to display a series of number as shown below: 40 38 36 34 32 30 3. Write a program using while loop to find the sum of integers 73 through 415 inclusive.. 4. Write a program using while loop to display the even numbers of integers 1 through 20 inclusive. 11
  • 12. /* Write a program to display a series of number as shown below: 40 38 36 34 32 30 */ int i; i=40; while (i >= 30) { cout << i << " " ; i=i-2; } C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12
  • 13. C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13
  • 14. /* Write a program using while loop to display the even numbers of integers 1 through 20 inclusive. */ int i; i=1; while (i <= 20) { if (( (i % 2) == 0)|| (i == 1)) cout << i << " " ; i=i+1; } C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14