Computer Science

Do While Loop in C

The "Do While Loop in C" is a control flow statement that allows a specific block of code to be executed repeatedly as long as a specified condition is true. It differs from the "While Loop" in that it guarantees the execution of the block of code at least once before checking the condition. This loop is commonly used for tasks that require iteration until a certain condition is met.

Written by Perlego with AI-assistance

12 Key excerpts on "Do While Loop in C"

  • Book cover image for: An Object-Oriented Approach to Programming Logic and Design
    The statements within a loop constitute the loop body ; the body executes as long as the loop-controlling Boolean expression remains true. The same loop-controlling question is asked following each execution of the loop body; the structure is exited only when the test expression is false. You may hear programmers refer to looping as repetition or iteration . The loop structure in Figure 4-1 is also called a while loop because it fits the following statement: while testCondition continues to be true do someProcess endwhile In pseudocode for the while loop, the endwhile statement clearly shows where the looping structure ends. In Chapter 3, you learned to use endif to perform the same function for a selection structure. All loops can be written as while loops. Most programming languages support an additional loop format called the do-while loop. Appendix D discusses this loop. You encounter examples of looping every day, as in the following example: while you continue to be hungry take another bite of food endwhile or while an unread page remains in the reading assignment read another unread page endwhile In a business application, you might perform tasks like the following: while quantity in inventory remains low continue to order items endwhile or No Yes Figure 4-1 The loop structure 118 C H A P T E R 4 Looping Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it.
  • Book cover image for: Processing
    eBook - ePub

    Processing

    An Introduction to Programming

    5
    Repetition with a Loop: The  while    Statement    
    We have seen in the programs we have written that the order in which the statements are written makes a difference. This illustrates a key principle of computing known as sequence  .
    In Chapter  4, we saw that statements placed inside an if  or switch  statement are only performed if a certain condition is true. Such conditional programming illustrates a second key principle of computing known as selection  .
    In this chapter and the next, we will see that, sometimes, one or more statements need to be performed repeatedly   in a computer program. This will illustrate a third key principle in computing: repetition  .
    Human beings often find repetitive tasks to be tedious. In contrast, computers can provide an ideal way to perform repetitive tasks.  Repetition as Long as a Certain Condition Is True
    Like the if  statement, each repetition structure in this chapter will make use of a condition  . This condition will determine whether the repetition should continue  .
    Once again, let’ s consider a cooking recipe as an analogy to a computer program. Suppose a certain dough recipe says that you are to mix the ingredients and then repeatedly   add ½  cup of flour as long as   you can still stir the mixture with a spoon. A flowchart of this recipe might look like the following:
    Similarly, in computer programming, there are sometimes sets of statements that we would like to perform repeatedly  , but only as long as   a certain condition is true.
    Repetition with the while   Statement
    Processing provides several programming structures that can be used for creating repetition. One of the more versatile of these repetition structures is the while   statement. The basic form of the while
  • Book cover image for: Microsoft Visual C#: An Introduction to Object-Oriented Programming
    C H A P T E R 5 Looping Upon completion of this chapter, you will be able to: • Create loops using the while statement • Create loops using the for statement • Create loops using the do statement • Use nested loops • Accumulate totals • Understand how to improve loop performance • Appreciate looping issues in GUI programs Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 186 C H A P T E R 5 Looping In the previous chapter, you learned how computers make decisions by evaluating Boolean expressions. Looping allows a program to repeat tasks based on the value of a Boolean expression. For example, programs that produce thousands of paychecks or invoices rely on the ability to loop to repeat instructions. Likewise, programs that repeatedly prompt users for a valid credit card number or for the correct answer to a tutorial question run more efficiently with loop structures. In this chapter, you will learn to create loops in C# programs. Computer programs seem smart because of their ability to make decisions; looping makes programs seem powerful. Creating Loops with the while Statement A loop is a structure that allows repeated execution of a block of statements. Within a looping structure, a Boolean expression is evaluated. If it is true , a block of statements called the loop body executes and the Boolean expression is evaluated again. As long as the expression is true , the statements in the loop body continue to execute and the loop-controlling Boolean expression continues to be reevaluated. When the Boolean evaluation is false , the loop ends.
  • Book cover image for: Java Programming
    eBook - PDF
    C H A P T E R 6 Looping Upon completion of this chapter, you will be able to: Describe the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops Nest loops Improve loop performance Copyright 2019 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 284 Looping C H A P T E R 6 Learning About the Loop Structure If making decisions is what makes programs seem smart, looping is what makes programs seem powerful. A loop is a structure that allows repeated execution of a block of statements. Within a looping structure, a Boolean expression is evaluated. If it is true, a block of statements called the loop body executes and the Boolean expression is evaluated again. The loop body can be a single statement, or a block of statements between curly braces. As long as the loop-controlling, Boolean expression is true, the statements in the loop body continue to execute. When the Boolean evaluation is false, the loop ends. One execution of any loop is called an iteration. Figure 6-1 shows a diagram of the logic of a loop. In Java, you can use several mechanisms to create loops. In this chapter, you learn to use three types of loops: • A while loop, in which the loop-controlling Boolean expression is the first statement in the loop, evaluated before the loop body ever executes • A for loop, which is usually used as a concise format in which to execute loops • A do…while loop, in which the loop- controlling Boolean expression is the last statement in the loop, evaluated after the loop body executes one time.
  • Book cover image for: Microsoft® Visual C# 2015
    eBook - PDF

    Microsoft® Visual C# 2015

    An Introduction to Object-Oriented Programming

    C H A P T E R 5 Looping In this chapter you will:  Learn how to create loops using the while statement  Learn how to create loops using the for statement  Learn how to create loops using the do statement  Use nested loops  Accumulate totals  Understand how to improve loop performance  Learn about looping issues in GUI programs Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 190 C H A P T E R 5 Looping In the previous chapter, you learned how computers make decisions by evaluating Boolean expressions. Looping allows a program to repeat tasks based on the value of a Boolean expression. For example, programs that produce thousands of paychecks or invoices rely on the ability to loop to repeat instructions. Likewise, programs that repeatedly prompt users for a valid credit card number or for the correct answer to a tutorial question run more efficiently with loop structures. In this chapter, you will learn to create loops in C# programs. Computer programs seem smart due to their ability to make decisions; looping makes programs seem powerful. Creating Loops with the while Statement A loop is a structure that allows repeated execution of a block of statements. Within a looping structure, a Boolean expression is evaluated. If it is true , a block of statements called the loop body executes and the Boolean expression is evaluated again. As long as the expression is true , the statements in the loop body continue to execute and the loop-controlling Boolean expression continues to be reevaluated. When the Boolean evaluation is false , the loop ends.
  • Book cover image for: An Introduction to Programming with C++
    Although not a requirement, some programmers use a comment (such as //begin loop ) to mark the beginning of the do while statement because it makes the program easier to read and understand. The braces in a do while statement are not required when the loop body contains only one statement. However, a one-statement loop body is rare. H OW T O Use the do while Statement Syntax do EHJLQ ORRS { one or more statements to be processed one time, and thereafter as long as the condition is true } while ( condition ) Example 1 LQW KRXUV FRXW +RXUV ZRUNHG FLQ !! KRXUV GR EHJLQ ORRS ^ FRXW *URVV SD KRXUV HQGO HQGO FRXW +RXUV ZRUNHG FLQ !! KRXUV ` ZKLOH KRXUV ! Example 2 FKDU DQRWKHU GRXEOH QXPEHU GRXEOH VXP FRXW (QWHU D QXPEHU <1 FLQ !! DQRWKHU GR EHJLQ ORRS ^ FRXW 1XPEHU FLQ !! QXPEHU VXP QXP FRXW (QWHU DQRWKHU QXPEHU <1 FLQ !! DQRWKHU ` ZKLOH WRXSSHUDQRWKHU < Figure 8-4 How to use the do while statement the statement ends with a semicolon priming read update read semicolon priming read update read semicolon Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 253 The do while Statement The while clause in Example 1 indicates that the loop body instructions should be repeated as long as (or while) the value in the hours variable is greater than 0. The loop will stop when the second number entered by the user is either less than or equal to 0. When processing the while clause in Example 2, the computer temporarily converts the another variable’s value to uppercase and then compares the result to the uppercase letter Y.
  • Book cover image for: C# Programming
    eBook - PDF

    C# Programming

    From Problem Analysis to Program Design

    Using the While Statement Probably the simplest and most frequently used loop structure to write is the while statement. The general form of the while statement is while (conditional expression) statement(s); The conditional expression, sometimes called the loop condition , is a logical condi-tion to be tested. It is enclosed in parentheses and is similar to the expressions you use for selection statements. The conditional expression must return a Boolean result Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. Using the While Statement | 327 6 of true or false . An interpretation of the while statement is “ while condition is true , perform statement(s).” The statement(s) following the conditional expression makes up the body of the loop. The body of the loop is performed as long as the conditional expression evaluates to true . Like the selection construct, the statement following the conditional expres-sion can be a single statement or a series of statements surrounded by curly braces { }. Some programmers use curly braces to surround all loop bodies, even loop bodies consisting of a single statement. Consistently using curly braces increases readability. It also reduces the chance of forgetting to include the curly braces when the body contains multiple statements. You can usually kill an infinite loop by closing the window or pressing the Esc key. If that does not work, try the key combinations Ctrl+C or Ctrl+Break . Press the two keys simultaneously. The easiest way to do this is to hold down the Ctrl key and then press the second key.
  • Book cover image for: Python for Beginners
    • Kuldeep Singh Kaswan, Jagjit Singh Dhatterwal, B Balamurugan(Authors)
    • 2023(Publication Date)
    5 Iterative Control Structure
    DOI: 10.1201/9781003202035-5
    Looping is a program or script procedure that performs repetitive (or iterative) tasks. The loop is a sequence of instructions (a block) that is executed repeatedly while or until some condition is met or satisfied. Each repetition is called an iteration. All programming and scripting languages provide loop structures, and, like other languages, Python has several looping constructs [28 ].
    Algorithm design is frequently based on iteration and conditional execution.

    5.1 The While Loop

    The output of Program 5.1 (counting.py) counts up to five.
    print (5) print (4) print (3) print (2) print (1)
    Output 5 4 3 2 1
    How would you write the code to count to 10,000? Would you copy, paste, and modify 10,000 printing statements? You could, but that would be impractical! Counting is such a common activity, and computers routinely count up to very large values, that there must be a better way. What we would really like to do is print the value of a variable (call it count), then increment the variable (count += 1), repeating this process until the variable is large enough (count == 5 or maybe count == 10000). Python has two different keywords, while and for , that enable iteration [29 ].
    Program 5.2 (loopcounting.py) uses the keyword while to count up to five:
    count = 1 while count <= 5: print (count) count += 1
    Output 1 2 3 4 5
    The while keyword in Program 5.2 (loopcounting.py) causes the variable ‘count’ to be repeatedly printed then incremented until its value reaches five. The block of statements
    print(count) count += 1
    is executed five times. After each print of the variable count
  • Book cover image for: C++ for Engineers and Scientists
    5.9 Chapter Summary 1. A section of repeating code is referred to as a loop. A loop is controlled by a repetition state-ment that tests a condition to determine whether the code will be executed. Each pass through the loop is referred to as a repetition or an iteration. The tested condition must always be set explicitly before its first evaluation by the repetition statement. Within the loop, there must always be a statement that permits altering the condition so that the loop, after it’s entered, can be exited. 2. There are three basic type of loops: while , for , and do while . The while and for loops are pretest or entrance-controlled loops. In this type of loop, the tested condition is evalu-ated at the beginning of the loop, which requires setting the tested condition explicitly before loop entry. If the condition is true, loop repetitions begin; otherwise, the loop is not entered. Iterations continue as long as the condition remains true. In C++, while and for loops are constructed by using while and for statements. The do while loop is a posttest or exit-controlled loop, in which the tested condition is evaluated at the end of the loop. This type of loop is always executed at least once. As long as the tested condition remains true, do while loops continue to execute. 3. Loops are also classified according to the type of tested condition. In a fixed-count loop, the condition is used to keep track of how many repetitions have occurred. In a variable-condition loop, the tested condition is based on a variable that can change interactively with each pass through the loop. 4. In C++, a while loop is constructed by using a while statement. This is the most com-monly used form of this statement: while ( expression ) { statements ; } Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s).
  • Book cover image for: Programming Logic and Design, Introductory
    C H A P T E R 5 Looping Upon completion of this chapter, you will be able to: Appreciate the advantages of looping Use a loop control variable Create nested loops Avoid common loop mistakes Use a for loop Use a posttest loop Recognize the characteristics shared by all structured loops Describe common loop applications Appreciate the similarities and differences between selections and loops Copyright 2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Appreciating the Advantages of Looping Although making decisions is what makes computers seem intelligent, looping makes computer programming both efficient and worthwhile. When you use a loop, one set of instructions can operate on multiple, separate sets of data. Using fewer instructions results in less time required for design and coding, fewer errors, and shorter compile time. Recall the loop structure that you learned about in Chapter 3; it looks like Figure 5-1. As long as a Boolean expression remains true, the body of a while loop executes. Quick Reference 5-1 shows the pseudocode standards this book uses for the while statement. Figure 5-1 The loop structure No Yes QUICK REFERENCE 5-1 while Statement Pseudocode Standards while condition statements that execute when condition is true endwhile The while keyword starts the statement and precedes any statements that execute when the tested condition is true. The tested condition is a Boolean expression. It might be a comparison such as x > y, it might be a Boolean variable if the language supports that type, or it might be a call to a method that returns a Boolean value. (Chapter 9 in the comprehensive version of this book describes methods that return values.) The endwhile keyword ends the structure. After endwhile, the condition is tested again. Although many modern languages do not require indentation, the bodies of while loops in this book are indented.
  • Book cover image for: C++ Programming
    eBook - PDF

    C++ Programming

    From Problem Analysis to Program Design

    Due to electronic rights, some third party content may be suppressed from the eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional content at any time if subsequent rights restrictions require it. 5 while Looping (Repetition) Structure | 269 while Looping (Repetition) Structure In the previous section, you saw that sometimes it is necessary to repeat a set of statements several times. C11 has three repetition, or looping, structures that allow you to repeat a set of statements until certain conditions are met. This section discusses the first looping structure, called a while loop. The general form of the while statement is: while (expression) statement In C11, while is a reserved word. Of course, the statement can be either a simple or compound statement. The expression acts as a decision maker and is usually a logical expression. The statement is called the body of the loop. Note that the paren- theses around the expression are part of the syntax. Figure 5-1 shows the flow of execution of a while loop. FIGURE 5-1 while loop expression statement true false The expression provides an entry condition to the loop. If it initially evaluates to true, the statement executes. The loop condition—the expression—is then reevaluated. If it again evaluates to true, the statement executes again. The statement (body of the loop) continues to execute until the expression is no lon- ger true. A loop that continues to execute endlessly is called an infinite loop. To avoid an infinite loop, make sure that the loop’s body contains statement(s) that assure that the entry condition—the expression in the while statement—will eventually be false. Now that we know how to repeat statements using a while loop, next, we rewrite the program to determine the average number of calories burned in a week.
  • Book cover image for: C++ Programming
    eBook - PDF

    C++ Programming

    Program Design Including Data Structures

    May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 5 while Looping (Repetition) Structure | 269 while Looping (Repetition) Structure In the previous section, you saw that sometimes it is necessary to repeat a set of statements several times. C 11 has three repetition, or looping, structures that allow you to repeat a set of statements until certain conditions are met. This section discusses the first looping structure, called a while loop . The general form of the while statement is: while (expression) statement In C 11 , while is a reserved word. Of course, the statement can be either a simple or compound statement. The expression acts as a decision maker and is usually a logical expression. The statement is called the body of the loop. Note that the paren-theses around the expression are part of the syntax. Figure 5-1 shows the flow of execution of a while loop. FIGURE 5-1 while loop expression statement true false The expression provides an entry condition to the loop. If it initially evaluates to true , the statement executes. The loop condition—the expression —is then reevaluated. If it again evaluates to true , the statement executes again. The statement (body of the loop) continues to execute until the expression is no lon-ger true . A loop that continues to execute endlessly is called an infinite loop . To avoid an infinite loop, make sure that the loop’s body contains statement(s) that assure that the entry condition—the expression in the while statement—will eventually be false . Now that we know how to repeat statements using a while loop, next, we rewrite the program to determine the average number of calories burned in a week. Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.