0% found this document useful (0 votes)
63 views12 pages

Unit 2 Answer

The key differences between a while loop and do-while loop are: 1. A while loop first checks the condition and then executes the body, while a do-while loop first executes the body and then checks the condition. 2. The body of a while loop may not execute at all if the condition is false from the beginning, but the body of a do-while loop will execute at least once. 3. The syntax of a while loop checks the condition after the loop body, while the syntax of a do-while loop checks the condition inside the loop parentheses.

Uploaded by

bolt spark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views12 pages

Unit 2 Answer

The key differences between a while loop and do-while loop are: 1. A while loop first checks the condition and then executes the body, while a do-while loop first executes the body and then checks the condition. 2. The body of a while loop may not execute at all if the condition is false from the beginning, but the body of a do-while loop will execute at least once. 3. The syntax of a while loop checks the condition after the loop body, while the syntax of a do-while loop checks the condition inside the loop parentheses.

Uploaded by

bolt spark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT II

What is the difference between while loop and do-while loop? Explain with syntax and example?

SR.N while loop do-while loop


O

1. While the loop is an entry control loop The do-while loop is an exit control loop
because firstly, the condition is checked, because in this, first of all, the body of the
then the loop's body is executed. loop is executed then the condition is
checked true or false.

2. The statement of while loop may not be The statement of the do-while loop must be
executed at all. executed at least once.

3. The while loop terminates when the As long as the condition is true, the compiler
condition becomes false. keeps executing the loop in the do-while
loop.

4. In a while loop, the test condition variable In a do-while loop, the variable of test
must be initialized first to check the test condition Initialized in the loop also.
condition in the loop.

5. In a while loop, at the end of the condition, In this, at the end of the condition, there is a
there is no semicolon. semicolon.
Syntax: Syntax:
while (condition) while (condition);

6. While loop is not used for creating menu- It is mostly used for creating menu-driven
driven programs. programs because at least one time; the loop
is executed whether the condition is true or
false.

7. In a while loop, the number of executions In a do-while loop, irrespective of the


depends on the condition defined in the condition mentioned, a minimum of 1
while block. execution occurs.

8. Syntax of while loop: Syntax of do-while loop:


while (condition) do
{ {
Block of statements; statements;
} }
Statement-x; while (condition);
Statement-x;

9. Program of while loop: Program of do-while loop:


Program of while loop: #include
#include
#include Void main()
#include {
Void main() int i;
{ clrscr();
int i; i = 1;
clrscr(); do
i = 1; {
while(i<=10) printf("hello");
{ i = i + 1;
printf("hello"); }
i = i + 1; while(i<=10);
} getch();
getch(); }
}

10. Flowchart of while loop: Flowchart of do-while loop:

While loop statement:


While loop statement is one of the easiest and most widely used looping constructs
in the C programming language.
In this statement, while it is a reserved word or keyword, the condition can be any
constant, variable, expression, and statement can be a single statement or a group
of statements. In the while loop, we have to perform three steps:
o Initialization
o Test Condition
o Increment and decrement

Initialization Value: In a while loop, the first step is initialization is used to set the initial value of the loop counter.
The loop counter may be an increment counter or decrement counter in while loop.

For Example: i = 1;

Test Condition: After the initialization step, the next step in the while loop is to check the test condition whether
the loop is executed or not. If the condition is true in the while loop, the loop's body is executed; otherwise, there is
no loop execution.

For Example: while (i<=10)

Increment and decrement: After checking the test condition in the while loop, increment and
decrement are used to increment and decrement the loop counter's value.

For Example: i = i + 1;

Syntax of while loop:


Initialization;  
while (condition)  
{  
Block of statements;  
}  
Statement-x;  

In a while loop statement, while keyword is used and followed by any good condition that has to
be tested enclosed in parentheses and it may contain any logical operator. Condition is followed
by a pair of curly brackets that specifies the set of statements that are to be executed while the
condition is true.

Example:
#include <stdio.h>  
#include <conio.h>  
Void main ()  
{  
int i;  
clrscr();  
i = 1;  
while (i<=10)  
{  
print ("hello");  
i = i + 1;  
}  
getch();  
}  

Flowchart of while loop statement:

do-while loop statement:


In the C programming language, the do-while loop statement is also similar to a while loop in the
C programming language. In this, first of all, the loop's body is executed then the condition is
checked. In the do-while loop, we have to perform three steps:
o Initialization
o Test Condition
o Increment and decrement

Initialization value: In a do-while loop, the first step is initialization and is used to set the initial
value of the loop counter. The loop counter may be an increment counter or decrement counter.

For Example: i = 1;

Test Condition: After the initialization step, the next step in the do-while loop is to check the
test condition whether the loop is executed or not.
For Example: while (i<=10);

Increment and decrement: After checking the test condition in the do-while loop, the next
step is to increment and decrement the loop counter's value.

For Example: i = i + 1;

Syntax of do-while loop:


1. do  
2. {  
3. Statements;  
4. }  
5. while (condition);  
6. Statement-x;  

Here, two keywords are used for the do-while loop statement is do and while. If the condition is true in
the do-while loop, the statement executes again, and if the condition returns false, execution stops, and
control passes to the next statement.

Example:
1. #include <stdio.h>  
2. #include <conio.h>  
3. Void main ()  
4. {  
5. int i;  
6. clrscr();  
7. i = 1;  
8. do  
9. {  
10. printf("hello");  
11. i = i + 1;  
12. }  
13. while(i<=10);  
14. getch();  
15. }  
Flowchart of do-while loop statement:

Difference between else if ladder and switch case


Else If Ladder:
else if statement can be defined as a control statement which controls the statement(s) to be
executed on the basis of some conditions. Whenever the else if statement is used, the compiler or
interpreter initially checks the condition whether it is true or false and if the condition is found to
be true then, the corresponding statements are executed. If the condition is found to be false, it
continues checking the next else if statement until the condition comes to be true or the control
comes to the end of the else if ladder.

The syntax of else if ladder can be represented as:

else if (condition-n)
statement-n;

else
1 if( condition-1)
2     statement-1;
3  
4 else if (condition-2)
5     statement-2;
6  
7 else if (condition-3)
8     statement-3;
9  
10 else if (condition-4)
11     statement-4;
12  
13 else if (condition-n)
14     statement-n;
15  
16 else
17     default statement;
Features
of else if ladder:

 It evaluates an expression and then, the code is selected based on the true value of
evaluated expression.
 Each else if has its own expression or condition to be evaluated.
 The variable data type used in the expression of else if is either integer or character.
 The decision making of the else if is dependent on zero or non-zero basis.

Switch Case:
The switch case statement is similar to the else-if ladder as it provides multiple branching or
multi-conditional processing. But, the basic difference between switch case and else if ladder is
that the switch case statement tests the value of variable or expression against a series of
different cases or values, until a match is found. Then, the block of code with in the match case is
executed. If there are no matches found, the optional default case is executed.

The syntax of switch case can be represented as:

Features of else if ladder:


It evaluates an expression and then, the code is selected based on the true value of evaluated

expression.
 Each else if has its own expression or condition to be evaluated.
 The variable data type used in the expression of else if is either integer or character.
 The decision making of the else if is dependent on zero or non-zero basis.
Switch Case:
The switch case statement is similar to the else-if ladder as it provides multiple branching or multi-
conditional processing. But, the basic difference between switch case and else if ladder is that the switch
case statement tests the value of variable or expression against a series of different cases or values, until a
match is found. Then, the block of code with in the match case is executed. If there are no matches found,
the optional default case is executed.
The syntax of switch case can be represented as:
switch(expression)
{
case constant-1
block-1;
break;

case constant-2
block-2;
break;

case constant-3
block-3;
break;

case constant-n
block-n;
break;

default:
default_block;
}

statement-x;
Explain goto ,break and continue with an example?

Break statement
Break statement is used to break the process of a loop (while, do while and for) and switch case.

 
Syntax: break;
 

Example 1

1  
2 while(test Expression)
3 {
4 // codes
5 if(condition for break){
6 break;
7 }
8 // codes
9 }
10  

Example 2

1  
2 For(int it, condition, upgrade
3 {
4 // codes
5 if(condition for break){
6 break;
7 }
8 // codes
9 }
10  

 
Continue Statement:
The Continue statement is used to continue the execution of the loop. It is used inside if condition. We
can use it with while, do while and for loop.
Syntax:

Let us take an example:


1  
2 #include <stdio.h>
3 void main(){
4 int i;
5 for(i=1;i<=5;i++){
6 if(i==2)
7 {
8 continue;
9 }
10 printf("%d \n",i);
11 }
12 }
13  

Output

1  
2 1
3 3
4 4
5 5
6  

 
Goto  Statement
Goto statement is used to transfer the unconditional program control to a labeled statement.
Syntax:

1  
2 Goto identifier;
3  

Example:
1  
2 #include <stdio.h>
3 void main(){
4 printf("Hello We are Learning C Language Tutorial\n");
5 goto label;
6 printf("How Are You?"); // skipped
7 printf("Are You Okey?"); // skipped
8 label:
9 printf("Hope you are fine");
10 }
11  

Output

You might also like