Statements - Govt College Ropar

advertisement
Asstt. Prof Mandeep Kaur
Computer Dept
TOPIC: Control Statements in C language
Branching(Selective)Statements
The if statement
The else-if statement
The else-if construct
The nested if statement
The switch statement
Looping Statements
The for statement
The while statement
The do-while statement
Jumping(Breaking control)Statements
The break statement
The continue statement
The goto statement
Decision Statements in C
 The statements used to alter the flow of the program are known as decision
statements.
 These are used to check that whether the particular statement is true or not.
Following is the list of decision statements often used:
1. The if Statement:
Explanation: if the test expression is true, then statement-block will
execute,otherwise it will jump to statement-x.
Syntax of if Statement:
if (test expression)
{
statement –block;
}
statement-x;
//Program
to implement if statement
 #include<stdio.h>














#include<conio.h>
void main()
{
clrscr();
int a,b,big;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
big=a;
if(big>b)
big=b;
printf("%d is greater",big);
getch();
}
2. The if-else statement:
 It is a two way branching statement
 Explanation: if the test condition is true, then true-block statements
will execute otherwise false-block statements will execute.
Syntax for if-else statement
if (test expression)
{
True-block statement (s);
}
else
{
False-block statement(s);
}
//Program to implement if else statement














#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int year;
printf("Enter year\n");
scanf("%d",&year);
if(year%4==0)
printf("leap year");
else
printf("not a leap year");
getch();
}
3.Nested if statements:
 When an if statement appers within anoter if statement, the statement
is called nested if statement.
Syntax
If<expression-1>
If(expression-2)
<Statement-set-1>
Else
<Statement-set-3>
If(expression-3)
<Statement-set-3>
Else
If<Statement-set-4>
 Void main()
 {
int a,b,c;
 printf(“Enter three integer numbers”);
 scanf(“%d%d%d”,&a,&b,&c);
 if(a>b)
 {
 If(a>c)
 printf(“|nGreatest number=%d”,a);
 else
 printf(“|nGreatest number=%d”,c);
 }
 else
 {
 If(b>c)
 printf(“|nGreatest number=%d”,b);
 else
 printf(“|nGreatest number=%d”,c);
 }
 getch();
 }
Program to implement the use of nested if statement.
4. The if-else-if ladder
Explanation: as soon as the true condition is found the statement associated with it is executed and the
control is transferred to statement-x. When all the n conditions become false the default statement
will be executed.
if(condition 1)
statement -1;
else if(condition 2)
statement -2;
else if (condition 3)
statement-3;
………………
………………
………………
………………
else if(condition n)
statement-n;
else
default-statement;


























#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int day;
printf("Enter number between 1-7\n");
scanf("%d",&day);
if(day==1)
printf("Monday");
else if(day==2)
printf("Tuesday");
else if(day==3)
printf("Wednesday");
else if(day==4)
printf("Thursday");
else if(day==5)
printf("Friday");
else if(day==6)
printf("Saturday");
else if(day==7)
printf("Sunday");
else
printf("Enter correct no");
getch();
}
//Program to show use of if-else-if ladder
4. The switch statement
Syntax
switch(expression)
{
case 1:
statement -1;
break;
case 2:
statement -2;
break;
case 3:
statement -3;
break;
….…..
…………
case n:
statement -n;
break;
default:
statement;
}
statement-x;
//Program to implement switch statement


















#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x;
float a,b;
printf("Enter first no\n");
scanf("%f",&a);
printf("Enter second no\n");
scanf("%f",&b); printf("@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("Algebric Operations\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("1. summation\n");
printf("2. difference\n");
printf("3. Product\n");
printf("4. Diviosion\n"); printf("@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("Enter the no as per required\n");
printf("@@@@@@@@@@@@@@@@@@@@@@@\n"); scanf("%d",&x);
switch(x)
{


















case 1:
printf("Sum=%.2f",a+b);
break;
case 2:
printf("Differene=%.2f",a-b);
break;
case 3:
printf("Product=%.2f",a*b);
break;
case 4:
printf("Division=%.2f",a/b);
break;
default:
printf("Enter correct no");
break;
}
getch();
}
Loop Control Statements in C
 A Loop is a block of statements which are repeatedly executed for
certain number of times. Steps in Loop
 a) Expression 1: Initialization e.g. if i is any variable then in this we
have to start the variable. Let us say i=1
 b) Expression 2: Test Condition in this the condition is checked
again and again. And if this condition satisfies then it will execute
further otherwise not. Let us say i<=5.
 c) Expression 3: Increment or Decrement in this the value is either
increased or decreased in each step. Let us say i++.
Following
are the loops used in C Language:
 for loop syntax
for(expression-1; expression-2;expression-3) statement;
 while loop syntax
expression-1; while(expression-2)
{
statement;
expression-3;
}
2.The condition is tested before executing the body of the loop.
3.Body of while loop is executed only if the condition is true.
 do- while
expression-1;
do
{
statement;
expression-3;
}
while(expression-2);
2.The condition is tested after executing the body of the loop.
3.Body of the do loop is executed at least once even if the condition is
false.
//Program showing use of for loop












#include<stdio.h>
#include<conio.h>
void main()
{
int x,i;
clrscr();
printf("Enter any natural no\n");
scanf("%d",&x);
for(i=1;i<=x;i++)
printf("I=%d\n",i);
getch();
}
//Program showing use of while loop













#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n=1;
while(n<=10)
{
printf("\n%d",n);
n++;
}
getch();
}
//Program showing use of do while loop














#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n=1;
do
{
printf("\n%d",n);
n++;
}
while(n<=10);
getch();
}
Jumping Statements:
 break statement:-The break statement is used to terminate
loop or to exit from switch case structure. The break can be used
within a for loop, a while loop, a do-while loop or a switch
statement to transfer the control out of the block/loop. The
statements enclosed in a pair of braces is called a block.
Syntax
break;
when break statement is encountered within the loop/block the
control the control is transferred to block/statement
immediately following loop/block, that is, loop is terminated.
// Program showing use of break statement.













Void main()
{
int i,j;
clrscr();
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
break;
printf(“\n%d %d\n”i,j);
}
}
}
The continue statement
 The continue statement can be used within a while , a
do-while or a for statement. The continue statement
transfers the control to the beginning of the loop, for
next iteration, after by passing the statements which
one yet executed.
Syntax
continue;
// Program showing use of continue statement.













Void main()
{
int i,j;
clrscr();
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
if(i==j)
continue;
printf(“\n%d %d\n”i,j);
}
}
}
The goto statement
 The goto statement is used to alter the normal executin of
program execution by transferring control to and other point
of the program.
Syntax
goto label;
 where label is an identifier used to label the target statemnt to
which control will be transferred. The target statement must
be labelled and the lebel must be followed by a colon.
label : statement
a)unconditional
statement:
 The unconditional goto
goto statement
is used to transfer the control from
one part of the program to the other part without checking any
condition.Normally prefered to use the unconditional goto statement
in program as it may lead to a very complicated problem like an infinite
loop.








// Program showing use of unconditional goto statement.
Void main()
{
start:
printf(“I am doing MCA”);
goto start;
}
 The conditional goto
statement
is used to transferthe control of the execution
b)conditional
goto
statement:
from one part of the program to the other. This transfer of control is based on
certain condition.
// Program showing use of conditional goto statement.
 Void main()
 {
 int n=10;
 clrscr();
 loop:
 printf(“%d”,n);
 n--;
 if(n>0)
 {
 goto loop;
 printf(“FIREEEEEE”);
 }
 }


Download