Explain if-else,switch&loops,while for,do while
Answers
Answer:
the answer is::
Explanation:
if else
An if else statement in programming is a conditional statement that runs a different set of statements depending on whether an expression is true or false. A typical if else statement would appear similar to the one below (this example is JavaScript, and would be very similar in other C-style languages).
for example
var x = 1;
if (x === 1) {
window.alert("The expression is true!");
}
else {
window.alert("The expression is false!");
}
loop:
two type
1. Entry controlled loop
2. Exit controlled loop
In an entry controlled loop, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.
In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.
While Loop :
A while loop is the most straightforward looping structure. The basic format of while loop is as follows:
syntex:
while (condition)
{
statements;
}
example::
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
return 0;
}
Do-While loop ::
A do-while loop is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.
The basic format of while loop is as follows:
syntex::
do
{
statements
} while (expression);
example::
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
For loop ::
A for loop is a more efficient loop structure in 'C' programming. The general structure of for loop is as follows:
syntax:
for (initial value; condition; incrementation or decrementation )
{
statements;
}
example:
#include<stdio.h>
int main()
{
int number;
for(number=1;number<=10;number++) //for loop to print 1-10 numbers
{
printf("%d\n",number); //to print the number
}
return 0;
}
output:
1
2
3
4
5
6
7
8
9
10