0% found this document useful (0 votes)
5 views

Programming

The document outlines different types of if statements, including general if statements, if-else statements, and nested loops. It explains how if statements control the flow of a program based on conditions and compares the advantages and limitations of switch statements versus if-else statements. Additionally, it provides examples and guidance on when to use each type of statement based on the complexity of conditions in programming.

Uploaded by

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

Programming

The document outlines different types of if statements, including general if statements, if-else statements, and nested loops. It explains how if statements control the flow of a program based on conditions and compares the advantages and limitations of switch statements versus if-else statements. Additionally, it provides examples and guidance on when to use each type of statement based on the complexity of conditions in programming.

Uploaded by

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

Types of if statement

• General if statement
• If --------- else statement
• Else if chain and nested loop
What is if statement?
The if statement either selects an action, if a condition is
true or skips the action if the condition is false.

“If it is not raining, we will go to school.”


If ( conditional expression) {
statement_1;
statement_2;
statement_3;
}
#include <stdio.h>
int main(){

int a=5, b=10;


if(a<b)
printf("a is less than b \n");

if(a>b)
printf("a is more than b \n");

return 0;
}

Output
a is less than b
It allows you to specify that
If else different actions are to be
statement performed when the condition is
true and when it’s false.
Flow chart

if(expression){

Statement_1;
else
Statement_2;
Input Output

a = 12 You entered an even number


Nested loop?

If we use if else as a compound statement in another if else,


then it is called nested loop.
If (expression_1){
If(expression_2)
statement_1;
else
Nested statement_2;
Loop }
else {
if(expression_3)
statement_x;
}
Else if chain

If (expression)
Statement_1;
else if (expression)
statement_2;
else if (expression)
statement_2;
-
-
else if (expression)
statement_2;
else
statement_x;
A switch statement is a type of selection control
Switch mechanism used to allow the value of a variable or
expression to change the control flow of program
statement execution via a multiway branch.
Keyword

1)Switch
2)Case
3)Break
4)Default
Flow diagram
Limitations of switch over if-else ladder

1.The variable expression are also not allowed


in cases, “case i+2:” is not allowed in switch,
but it is valid on if-else.

2.You cannot test a flat expression using switch.

3.You cannot use similar expressions for


multiple cases.

4.Switch statement must know the value inside


it during compilation.
Advantages of switch over if-else ladder

1.A switch statement works much faster than equivalent if-else


ladder.

2.It is more readable and in compare to if-else statements.

3.It is more manageable for having higher level of indentation than if.

4. Here multiple statements need not to be enclosed within a pair of


braces.
Where to use 1.If there are large number of
switch over if- compares for a condition in your
program, use switch over if-else
else ladder
ladder.
2.For more complex comparisons.
}
In case of simple and few
compares, if-else executes faster
Where to use if- and easy write. Thus as per
else ladder over program’s requirement, a
switch programmer should decide
himself where to use which one
condition control.

You might also like