Unit 2 Answer
Unit 2 Answer
What is the difference between while loop and do-while loop? Explain with syntax and example?
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.
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.
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;
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();
}
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;
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:
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.
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:
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