2. Condition Statements
• Conditional statements help us to make a decision based on certain
conditions.
• These conditions are specified by a set of conditional statements
having Boolean expressions which are evaluated to a Boolean value of
true or false.
3. Types of Conditional statements in C
• If statement
• If-Else statement
• Nested If-else statement
• If-Else ladder
• Switch statement
4. If statements
• The single if statement in C language is used to execute the code if a
condition is true.
• It is also called a one-way selection statement.
7. If-else statement
• The if-else statement in C language is used to execute the code if the
condition is true or false.
• It is also called a two-way selection statement.
10. Nested If-else statement
• The nested if...else statement is used when a program requires more
than one test expression.
• It is also called a multi-way selection statement.
• Nested if-else statements can be useful when we can have multiple
sources of expression and the values and based on the specific value,
we need to check nested conditions.
14. If..else If ladder
• The if-else-if statement is used to execute one code from multiple
conditions.
• It is also called a multipath decision statement.
16. #include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf("enter a number");
scanf("%d",&a);
if( a%5==0 && a%8==0)
{
printf("divisible by both 5 and 8");
}
else if( a%8==0 )
{
printf("divisible by 8");
}
else if(a%5==0)
{
printf("divisible by 5");
}
else
{
printf("divisible by none");
}
getch();
}
17. Switch Statement
• switch statement acts as a substitute for a long if-else-if ladder that is
used to test a list of cases.
• A switch statement contains one or more case labels that are tested
against the switch expression.
• When the expression match to a case then the associated statements
with that case would be executed.
18. • Synatx
Switch (expression)
{
case value1:
//Statements
break;
case value 2:
//Statements
break;
case value 3:
//Statements
case value n:
//Statements
break;
Default:
//Statements
}
21. Note
• The switch statement must be an integral type.
• Case labels must be constants.
• Case labels must be unique.
• Case labels must end with a colon.
• The break statement transfers the control out of the switch statement.
• The break statement is optional.
22. Looping Statments
• Loops in C programming language are a conditional concept used to
execute a line or block of code consecutively.
• In C programming, there are three loops: For Loop, While Loop, and
Do While Loop.
• Loops in C can also be combined with other control statements such
as the Break statement, Goto statement, and Control statement.
These loops can be used anywhere in the program, in either entry
control or exit control units.
23. Different Types of Loops
• There are 3 different types of Loops in C:
1) While Loop
2) Do While Loop
3) For Loop
24. While Loop
• The condition is evaluated before processing the loop’s body.
• Only the loop’s body is executed if the condition is true. Then the
control goes back to the beginning after completing the loop once.
• The control will go out of the loop if the condition is false.
• After completion of the loop, the control will go to the statement
immediately after the loop.
• If the condition is not true in the while loop, then loop statements
won’t get executed.
25. • Syntax
while (condition) {
statements;
}
Example
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
while(num<=5)
{
printf("%dn",num);
num++;
}
return 0;
}
26. Do While Loop
• In this loop, the statements the loop need to be executed at least
once.
• If the condition is true, it will again have executed the loop;
otherwise, it will exit it. It is known as an exit-controlled loop.
• The while loop is performed only when the condition is true, but
sometimes the statement must be conducted at least once, so the do-
while loop has to be used.
27. • Syntax
do {
statements
} while (expression);
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1;
do
{
printf ("%dn",2*num);
num++;
}
while(num<=5);
return 0;
}
28. For Loop
• It executes the set of statements until the time a particular condition
is accomplished. It is known as the Open-ended loop.
• In For loop, we can have more than one initialization or
increment/decrement, separated using a comma operator and one
condition.
29. • Syntax
for (initial value; condition; incrementation or decrementation )
{
statements;
}
Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
for(number=1;number<=5;number++)
{
printf("%dn",number);
}
return 0;
}
30. Control Statements
• Some loop control statements need to be used in loops for different
purposes and to achieve the end result.
• Below are the different statements that are used:
1) Break statement
2) Continue statement
3) Goto statment
31. Break statement
• The break statement is used to exit the loop immediately after
executing a particular statement for a specific condition.
• Syntax
While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ break;}
Statement 3; }
32. Continue Statement
• It generally skips the statements according to the condition.
• For a particular condition, it skips the current loop or statements and
enters into a new loop or condition.
• Syntax
While (Condition)
{ Statement 1; statement 2;
If (Condition)
{ continue;}
Statement 3; }
33. Goto statement
• It is used to transfer the protocol to a labeled statement.
• Syntax:
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
number=0;
repeat:
printf ("%dn",number);
number++;
if(number<=5)
goto repeat;
return 0;
}