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

not of type float 1. A switch expression may be of type bool, char, int, or long

ID: 3738697 • Letter: N

Question

not of type float 1. A switch expression may be of type bool, char, int, or long, but True or false? 2 A variable declared in the initialization statement of a For loop has global scope. True of false? 3. Any While loop can be directly rewritten as a Do-While merely by changing the state ment syntax and moving the exit condition to the end of the loop. True or false? 4. A Break statement is not allowed in a For loop, but a Continue statement is. True or false? 5. Which of the looping statements in C++ are pretest loops and which are posttest oops?

Explanation / Answer

1.True,Since most floating point numbers are slightly imprecise because of rounding off errors in their binary representation, equality of floating numbers are not easy to compare.

Consider the below example:-

#include <stdio.h>

int main()

{

   float y = 1.1;

   switch (y)

   {

       case 1.1: printf("Choice is 1"); //Compiler Error: switch quantity not an integer

                 break;

       default: printf("Wrong Choice ");

                break;

   }

   return 0;

}

2.False,it is decelared as local.

Consider the below example:-

#include<stdio.h>

int main()

{

for(int a=0;a<5;a++)

{

printf("%d",a);//local..output will be 01234

}

printf("%d ",a);//error, since a is locally declared

}

3.True,in the below example both will print the same output

#include<stdio.h>

int main()
{int a=3;

while(a<5)

{

printf("%d",a);// output is 3 4

a++;

}

printf(" ");

a=3; //changing the above while loop into do-while

do{

printf("%d",a);//output is 3 4

a++;

}while(a<5);//exit condition

}

4.False, break statement is allowed ,the interation of For loop will be stoped and come out of the loop.

example-

#include<stdio.h>

int main()

{

for(int a=0;a<5;a++)

{

printf("%d",a);//it will print only once i.e 0

break;

}

}

5.Pretest loop: This is where the condition necessary to continue looping is checked before the instructions within the loop are executed.

example- WHILE loop,

For loop- It's a pretest because the condition to run the loop comes before the actions, not after

The post-test loop:A posttest loop tests its condition after each iteration.
A posttest loop will always execute at least once.This loop places the condition at the end of the loop

example-Do while