Computer Science, asked by nihar9379, 3 months ago

what is algorithm or pseudo code of do while loop?​

Answers

Answered by kunalranjan8
0

Answer:

In most computer programming languages, a do while loop is a control flow statement that executes a block of code at least once, and then either repeatedly executes the block, or stops executing it, depending on a given boolean condition at the end of the block.

Do While loop flow diagram

The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed, the do-while loop is an exit-condition loop. This means that the code must always be executed first and then the expression or test condition is evaluated. If it is true, the code executes the body of the loop again. This process is repeated as long as the expression evaluates to true. If the expression is false, the loop terminates and control transfers to the statement following the do-while loop. In other words, whereas a while loop sets the truth of a statement as a condition precedent for the code's execution, a do-while loop provides for the action's ongoing execution subject to defeasance by the condition's falsity, which falsity (i.e., the truth of the condition's negation) is set as a condition subsequent.

It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that allows termination of the loop.

Some languages may use a different naming convention for this type of loop. For example, the Pascal language has a "repeat until" loop, which continues to run until the control expression is true (and then terminates) — whereas a "while" loop runs while the control expression is true (and terminates once the expression becomes false).

Answered by SoumyodeepHazra
0

Answer:

The "While" Loop

A "While" Loop is used to repeat a specific block of code an unknown number of times, until a condition is met. For example, if we want to ask a user for a number between 1 and 10, we don't know how many times the user may enter a larger number, so we keep asking "while the number is not between 1 and 10". If we (or the computer) knows exactly how many times to execute a section of code (such as shuffling a deck of cards) we use a for loop.

The While Loop

The while loop is used to repeat a section of code an unknown number of times until a specific condition is met. For example, say we want to know how many times a given number can be divided by 2 before it is less than or equal to 1. If we know a specific number, such as 32, we can say 5 times, but for a given symbolic variable "NUMBER" which represents any number in the world, how many times is not known a priori (before hand). In this case, we could use a while loop to determine that answer:

The "pseudocode" for such an algorithm is: while the number is bigger than one keep dividing it by two. additionally, keep a count of how many times we do the division.

PseudocodeMatlabC, C++, or Java Actionscript

get our number

set our initial count to 0

while our number is greater than 1

divide the number by 2

increase our count by 1

end

Why While Loops?

Like all loops, "while loops" execute blocks of code over and over again.

The advantage to a while loop is that it will go (repeat) as often as necessary to accomplish its goal.

Generic Syntax:

while ( condition is true )

do something

% Note: the "something" should eventually result

% in the condition being false

end

Infinite loops:

If the action inside the loop does not modify the variables being tested in the loops condition, the loop will "run" forever. For example:

while ( y < 10 )

x = x + 1;

end

while ( true )

printf('hello');

end

Example 1: How to assure proper input

Ask the user to input a value.

while the input is incorrect.

ask the user to input another value.

go back to line 2 (the while)

MatlabC

% MATLAB

%

% Using a while loop to ask the user to input a number

% between 1 and 10 (inclusive).

%

% Variables:

% value : variable to store the input

%

value = input ('Please Enter a Number between 1 and 10 (1-10)');

while ( value < 1 || value > 10)

fprintf('Incorrect input, please try again.\n');

value = input ('Enter a Number between 1 and 10 (1-10)');

end % while

Design Pattern:

A design pattern is the syntax that you have to memorize in order to do well in programming and on tests.

The design pattern for a while loop is:

MatlabC, Java, or Actionscript

while ( some condition is true )

% Do this code

% Something here should modify the condition above

end

Similar questions