Control Statements PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

CONTROL

STATEMENTS
-if Statement
• Four types of –if
• if
• Block if or if else
• Nested if
• Multi alternative if
Simple if statement
• General form
• if (logical expression)
{
statements;
}
• If true, the statements executed, if false, the statement
are skipped

False true
logic

statements
Example
Write a program to find the value y using simple if
statement
if-else/ block if statement
• General form
• if (logical expression) False true
{
logic
statements;
} statement statement
else s s
{
statements;
}

• If true, the statements executed, if false, else statement


will be executed
Example
Write a program to find the value y using simple if
statement
Nested if statements
• Combination of block if statements
• Example
…..
• Z= x+y if x>5, y>1 if(x>2 && y>1)
• Z=x*y if x>5, y<=1 {z=x+y;}
• Z=x-5 if x<5 if(x>5 && y<=1)
{z=x*y;}
if(x<5)
{z=x-5;}
…..
if (testExpression1)
{
// statements to be executed if testExpression1 is true
}
else if(testExpression2)
{
// statements to be executed if testExpression1 is false and
testExpression2 is true
}
else if (testExpression 3)
{
// statements to be executed if testExpression1 and
testExpression2 is false and testExpression3 is true
}..
else
{
// statements to be executed if all test expressions are false
}
Multi alternative if statement
The goto statement
• goto statement is used for jumping from one position to
other place within main or some other function
• Format:
……
Label:

goto Label;
#include<stdio.h>
#include<conio.h>

int main()
{
int y, x;
Start:
printf(“\nEnter value x:");
scanf("%d",&x);
if (x<2)
{y=2*x+1;}
if (x>=2)
{y=15*x-2;}
printf("y=%d",y);
goto Start;
getch();
}
The -while statement
• Used for repeated execution of group statements.
• The repetition of the loop is defined only on a logical
expression
while(logical expression)
{
statements;
}
False true
logic

statements
Example
Example
• Write a program to evaluate x! , where x is positive integer

/* Calculate factorial using while loop */

#include<stdio.h>
int main()
{
int a,f,i;
printf("Enter a number: ");
scanf("%d",&a);
f=1;
i=1;
while(i<=a)
{
f = f * i;
i++;
}
printf("Factorial: %d",f);
return 0;
}
do-while statements
• Similar to while statement, but one difference is that the
logical condition which appears in this statement testes at
the end of the loop
do
{
Statements;
}while(logical expression); statement
s

False true
logic
while(logical expression)
{
statements;
}
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}
Exercise:
• Write a program to find sum of some integers until
sum of the integers is just greater than or equal to 500

#include <stdio.h>
int main()
{
int m, sum;
printf("Enter a number: ");
scanf("%d",&m);
do {
sum+=m;
}while(sum<500);
printf("sum is %d", sum);
return 0;
}
break statement
• Used to terminate loops and break the control of the
statements
• Generally used in while, do-while, for and switch
statements
• General form
break;
#include<conio.h>
#include<stdio.h>
int main()
{ int count=0,x;
do
{ scanf(“%d”,&x);
if (x<0) break;
count++;
}while (count<10);
printf(“count= %d”,count);
getch();
}
-continue statement
• Generally used within the loop to bypass a portion of the
loop
• The continue statement causes the loop to be continued
with next iteration after skipping any statement in between
• General form
continue;
Find the sum only for 10 positive number
#include<stdio.h>
#include<conio.h>
int main()
{ int sum,x, count=0;
do
{ scanf(“%d”,&x);
if (x<0) continue;
count++;
}while (count<10);
printf(“count= %d”,count);
getch();
return 0;
}
-switch statement
• This statement is mainly used when a particular statement is
selected from a group statements based on the value of an
identifier/ expression
• General form
switch(expression)
{
case expression1:
Statements;
break;
case expression2:
Statements;
break;
default:
statements;
}
for loop
• One or more variable are used to control the loop
• These variables are called the control variable
for(exp1;exp2;exp3)
{
Statements;
}
// exp1- initial value of control variable
//exp2- logical expression
//exp3- unary expression or assignment statement
Example
• Write a program to evaluate x! , where x is positive integer
using for loop

#include <stdio.h>

int main()
{
int i, f=1,a;
printf("Enter a number: ");
scanf("%d",&a);
for(i=1;i<=a;i++)
f=f*i;
printf("Factorial of %d is: %d", a, f);
return 0;
}
Nested for statement
• for loop may contain one or more for loops within its
range.
• When one for loop is nested inside the another, the inner
for loop must be entirely contained within the range of the
outer for loop
• Must be no overlapping of statement and each loop must
have unique control variable
Comma operator for -for
• Comma can be used if expression or statement contained
more than one statement. It can be separated by comma
operator
for(A=0,J=0;A<=10, J>=5; A++,J--)
{statements;

}
Exercise
#include <stdio.h>
int main()
{
int x,y;
x=0;
y=1;
do
{
printf("%d ",x);
x=x+y;
printf("%d ",y);
y=y+x;
} while( y < 100 );

return(0);
}

Output

You might also like