Control Statements:-
1.) if()
{
}
else
{
}
If without else is possible but else without if is not possible.
Note :
if(a=1)
{
}
It will show error as only Booleans are allowed not integers like a=1.
2.
if()
{
}
if()
{
}
if()
{
}
3.
if()
{
}
else if()
{
}
else if()
{
}
Note:-
In ‘if-if-if’ all ‘if’ are independent so every ‘if’ statement is tested irrespective of other ‘if’ statements.
In case of ‘if- else if’ ‘if’ is evaluated until we get the first true condition after that no evaluation takes place.
Note:-If is not a loop but a control/decision statement.
4.if else nested
if()
{
if()
{
}
else
{
}
}
else
{
if()
{
}
else
{
}
}
5.switch:-Used in case of more than one choices
Switch(expression)
{
case label1:
case label2:
case label3:
default:
}
Note:-
In switch case with matching label will get executed.
Default can be placed anywhere but it will execute only if no matching takes place.
Execution will takes place sequentially until break or end of switch.
Eg.
Switch(ch)
{
default:
case 2:
{
}
case 3:
{
}
case 4:
{
}
}
Let if we get ch=2 then along with case 2;case 3 and case 4 will also execute until break/end of switch.
That’s why we generally place
case2
{
break;
}
-->

No comments:
Post a Comment