What is difference between conditional construct and loops
Answers
Conditional Statement: It is a statement which checks for a condition. It is used for decision making.
types of conditional statements :
>if
>if-else
>nested if
For example: if(x>=0) Print “positive number”
here ‘if’ is a conditional statement..
loop: if you want a block of statements to execute multiple number of times, the You can go for loops. There are three types of loops:
>while
>do-while
>for
while: it is a loop in which you check the condition before entering the loop.
Ex:
x=0;
while(x!=10){
print x;
x++;}
here first it checks if x is not equal to 10. Only if this condition gets satisfied, then it will enter the loop.
do-while: we execute a block of statements once before checking the condition.
Ex:
x=0;
do{
print x;
x++;
}while(x!=10);
(observe the semi colon at the end of do while)
for: if you certainly know how many number of times you want to execute a block of statements, then you can you for loop.
ex: for(I=0;I<n;I++){
print i;
}
1. It runs only once.
2. compare two or more variables.
Ex: If, elseif, nested it and so on.
Loops:
1. Execute a multiple number of times.
2. Designed to repeat, while a certain condition is true.
Ex: While, for, do while and so on.