c Programming-chapter 05
c Programming-chapter 05
PRESENTED BY
SHEYONA G
CHAPTER-05
Decision Making, Branching
and Looping
Decision making with IF statement,
Simple IF statement
Decision-making statements are used to control the flow of a program by evaluating condition
Decision-making statements are also known as conditional statements
a. if
b. if…else
d. Nested if
if statement
Syntax
if (condition)
{
statements ;
}
Next statement
Syntax
if(condition)
{
statements ;
}
else
{
statements ;
}
In looping a set of statements are executed until some conditions for termination of the loop
is encountered.
A program loop therefore consists of two segments one known as body of the loop and other
is the control statement.
The control statement tests certain conditions and then directs the repeated execution of the
statements contained in the body of the loop
They are two types of loop:
Exit-controlled loop:
In exit-controlled loop, the test is performed at end of the body of the loop and therefore the body
of the loop is executed unconditionally for first time. Exit- controlled loop is
Do...while loop
Entry-controlled loop: Exit-controlled loop:
While Statement:
The simplest of all looping structure in C is the while statement.
The general format of the while statement is:
Initialization;
while (test condition)
{
body of the loop
}
Next statement;
The given test condition is evaluated and if the condition is true then the body of the loop is
executed.
After the execution of the body, the test condition is once again evaluated and if it is true, the
body of the loop is executed once again.
This process of repeated execution of the body continues until the test condition finally
becomes false and the control is transferred out of the loop.
#include<stdio.h> OUTPUT:
void main() 1,2,3,4,5,6,7,8,9,10,…………………………..,100
{
int i=1;
while(i<=100)
{
printf("%d ",i);
i++;
}
}
The Do while statement:
The body of the loop is executed first and then the loop condition is checked , so the body of the loop is
executed at least once.
The syntax of the do while loop is:
Initialization;
do
{
body of the loop;
}
while(test condition);
Next statement;
The body of the loop is executed, and then test condition is evaluated. If the condition expression is true
then the body is executed again and this process continues till the conditional expression becomes false.
When the expression becomes false the loop terminates.
Example:
void main()
{
int i=10 ;
do
{
printf(“%d \t”,i);
i--;
}
while(i>=1);
}
Output:
10 9 8 7 6 5 4 3 2 1
The for Statement:
Syntax:
Break Statement
Break statement is used to jump out of a loop instantly, without waiting to get back to the
conditional test.
The keyword used is break .
When break is encountered inside any loop, control automatically passes to the first statement
after the loop. Break is usually associated with an ‘if’.
Syntax:
break;
void main()
{ Output
int i; 12
for(i=1; i<=5; i++) exiting the loop
{ Thank you
if(i==3)
{
printf(“exiting the loop\t”);
break;
}
}
printf(“\nThank you”);
getchar();
}
Continue Statement
'continue' statement is used to skip the current iteration of a loop &
move to the next one
A continue is usually associated with an if.
Syntax:
continue;
void main()
{
int i; Output:
for(i=1; i<=5; i++)
{ 1 2 skipping this part of the loop 4 5
if(i==3)
{
printf(“skipping this part of the loop\t”);
}
continue;
}
printf(“%d\t”,i);
getch();
}
Difference between break and continue
Goto statement:
Syntax:
goto label-name;
There are two types of goto statements. They are,
Forward jump
Backward jump
#include<stdio.h>
void main()
{
int a;
printf(“Enter a number\n”);
scanf(“%d”,&a);
if(a>0)
goto aa;
else
goto bb;
aa: printf(“Positive”);
goto cc;
bb:
printf(“Neagtive”);
cc: getch();
}
Assignments
Which amongst the following is used in for loop to separate closing square
commas semicolon
the expressions brace bracket
Which among the following is an exit controlled loop? do-while switch break while