Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. When you write an if-block for idiotproofing, does the exit statement belong

ID: 3737550 • Letter: 2

Question

2. When you write an if-block for idiotproofing, does the exit statement belong before the if-block, inside the if-block or after the if-block? Therefore, should the exit statement be indented less than the if statement, the same as the if statement, or more than the if statement? 3. DESCRBE THE CONDITION of a while loop. ("The condition is a .. 4. Are the properties of the condition of a while loop the same as, or different from, the properties of the condition of an if block? 5. WHAT ARE THE STEPS that describe the execution of a while loop? 6. HOW does a while loop DIFFER from an if block?

Explanation / Answer

2.

The idiotproofing is usually done in a code to ensure that the input from the user is valid.

For idiotproofing the if block, the exit statement is written inside the if block.

Since the exit statement is inside the if block, it should have more indent then the if statement that is it should be more away from the margin then the if statement.

The following is an example depicting the same

if(x<y)

{

printf("x is smaller than y");

exit(program_failure_ code);

}

3.

The condition of a while loop that remains between the parentheses is a prerequisite that must be satisfied for the while loop to be repeatedly executed.

The statements In The While block will be executed as long as the condition remains true.

For example,

while(num>10)

{

num--;

}

Here the condition is num>10.

As long as the value of num is greater than 10, the statement inside the while love will continue to execute.

4.

The properties of the condition of a while block is much different than the properties of condition of an if statement.

This is because the code inside the if statement is executed only once if the condition is true. Hence the nature of the condition is checked only once and decided if it is true or false.

But the while loop is iterative in nature and repeatedly executes the code inside the while block until the condition remains true. The condition is checked in each run of the wild and based on the result, the code is executed until the given condition does not become false.

The following code snippets give a better explanation of the difference between the conditions in if statement and a while loop.

if(m-n>p)

{

// Execute only once if condition is true

}

while(m-n>p)

{

//This statement will be executed until the condition becomes false

}

While loop is generally used when we do not know how many times we have to loop through a condition, where are the if statement is used if the condition is to be checked only once.

5.

The following are the three steps that describe the execution of a while loop.

a) CHECK CONDITION

check if the given condition is true or false. If the condition is true, go to the next step, otherwise, terminate the loop if the condition is false.

b) EXECUTE STATEMENTS

when the condition is true, execute the statements within the while loop.

c) UPDATE THE CONDITION VARIABLE

after executing the statements within the while loop, also execute a statement to change the value of the variable that is being checked in the condition. Repeat again from Step a with the modified condition.

6.

The following is the difference between an if statement and a while loop:

An if statement executes the code inside the block if and only if the condition is true. The code is executed only once.

On the other hand a while loop executes the hot inside its block again and again as long as the condition remains true. The execution is stopped when the condition becomes false.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote