You are on page 1of 35

Unit 1

C- Control statements

Dr. Deepika Bhatia 1


LOOP CONTROL:-GOOD FOR Repetitive
Structures/tasks.
Types:
Allows the programmer to specify that an action is to
be repeated for a certain number of times, while
some condition remains true.
There are three repetition structures in C: -
the while loop
the for loop
do-while loop.

Dr. Deepika Bhatia 2


For Loop
The syntax of for loop is
for(initialisation; condition;iteration)
{
//set of statements;
}

Program to print Hello 10 times

for(i=1;i<=10;i++)
{
printf(“Hello”);
}
Dr. Deepika Bhatia 3
Dr. Deepika Bhatia 4
Dr. Deepika Bhatia 5
Important points related to for loop

1. No braces required for a single statement in the body of for loop


2. No semicolon after for statement
3.loop counter Increment within the body of for loop also , in
spite of this the semicolon after condition is necessary
4.Initialisation is done before for loop, but still the semicolon
before condition is necessary
5.increment and comparison done at same statement
6. Multiple initialisation and increment allow but single condition

Dr. Deepika Bhatia 6


Nesting of for loops-loop inside loop-inner loop
and outer loop here

for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
printf(“i=%d\t j=%d\n”,i,j);
}

Dr. Deepika Bhatia 7


Question
1) WAP to find the factorial of a given number.
2) WAP to check whether thegiven number is prime
3) Print n natural number and find its sum
4) Find the value of one number raised to the power of
another.
5) Write a program to print the following pattern

1 1
12 2 3
123 4 5 6
1234

Dr. Deepika Bhatia 8


While loop is Entry controlled
loop
The syntax for while loop
initialisation; //part 1
while(test condition) //part 2
{
statements;
iteration; (increment/decrement) //part3
}
Example:

a=10; //part1
while(a !=1) //part2
{
printf(“%d”,a);
a- -; //part3
}
Dr. Deepika Bhatia 9
Do-While Loop –in the statement inside do is executed at least
once before condition is tested. So, it is an exit controlled
loop.
The syntax of do while loop
do
{
//set of statements
} while(condn);

Example:
i=1;
do
{
printf(“%d”,i); //1
i--; //0
}while(i!=0);

Dr. Deepika Bhatia 10


Do-while Vs while loop

1)Do-while will be executed minimum once, but while loop can have
minimum execution as 0 times.
2)Do-while is an exit control loop, but while are entry control loop.
3)While loop is not terminated by semi colon. But do-while is
terminated by semicolon.
4)Variable initialisation happens before the loop in case of while
loop,but may be done inside the loop body in case of do-while loop.

Syntax:
while(condition) do {
{
}while(condition);
}
Dr. Deepika Bhatia 11
Comparison of three loops

Dr. Deepika Bhatia 12


Few Example of loops
//WAP to print odd number upto 9
//WAP to print a table of 2 int i = 0;

for (i = 1; i <= 10; i += 2)


int num=1;
do {
{ printf("%d\n", i);
printf("%d\n",2*num); }
num++; Output is
}while(num<=10); 1
3
5
7
9
int i = 1;
while (i < 6)
{
printf(“Deep\n”); //printed Deep 5 times
i++; Dr. Deepika Bhatia 13
}
Jumping in/out of a loop: There are 2
ways:
1. Break and
2. goto statements
Break Statement is used to take the control out of a block.

for(i=0;i<=10;i++) //i=0,1,2…5 (not executed 6 to 10)


{
printf(“%d”,i); // 0 1 2 3 4 5
if(i==5)
break;
}

Dr. Deepika Bhatia 14


Goto Statement
Goto is used for unconditional/conditional branching. Jumps from
anywhere to anywhere within a function.
It requires a label (user-defined Identifier) where a branch is to be made.
It can have two forms. Syntax:

Dr. Deepika Bhatia 15


Example: Goto
#include<math.h>
#include<stdio.h>
int main()
{
float x,y;
read:
scanf(“%f”,&x);
if(x<0)
goto read;
y= sqrt(x);
printf(“%f%f”,x,y);
return 0;
}
Dr. Deepika Bhatia 16
To skip a part of a loop use Continue Statement: The continue
statement skips some lines of code inside the loop and continues with the next
iteration. It is mainly used for a condition so that we can skip some code for a
particular condition.
int i;
for(i=0;i<=10;i++)
{
for(i=0;i<=10;i++) if(i==5)
{ continue;
if(i!=5) printf(“%d”,i);
continue; //i=5 }
printf(“%d”,i); //output is 01234678910
5 is not printed on the console
} //Program output is: 5 only because loop is continued at i==5.
Continue is used to bypass(skip) a set of statements (written below
continue) for a particular iteration. It tells the compiler to “SKIP THE
FOLLOWING STATEMENTS AND CONTINUE WITH THE NEXT ITERATION”
Dr. Deepika Bhatia 17
Break Vs Continue
1.Break takes the control out of a particular block, but continue skips rest of the
statements for a particular condition.
2.Break exits the loop completely but in case of continue the loop keeps executing
with next iteration.
3. Break statement works in loop as well as switch statement. But, continue works
with loops.
Example:

int j; int j;
for(j=1;j<=3;j++) for(j=1;j<=3;j++)
{ {
if(j==2) if(j==2)
continue; break;
printf(“%d”,j); printf(“%d”,j);
} }
output is: 13 output is: 1
Dr. Deepika Bhatia 18
exit() – exit(int status)
• exit() is a C library function to jump out of a program.
• Integer value as argument
• Zero indicate normal termination
#include <stdio.h>
• Non-zero indicate abnormal termination #include <stdlib.h>
• Prototype in <stdlib.h>
int main () {
• Syntax: void exit(int status) printf("Start of the program....\n");
• //status is the value returned to
parent process printf("Exiting the program....\n");
exit(0);

printf("End of the program....\n");

return(0);
}
Output is
Start of the program....
Exiting the program....
Dr. Deepika Bhatia 19
Decision making and Branching in C
C program follows a normal sequence for statements execution.
But there are cases where we have to change order of execution/ to use
repeated statements/go to some other statements. For this we use
decision control statements like: (conditional operator ?: is done
earlier)

Dr. Deepika Bhatia 20


If statement
Different forms of if are given below:

Example: simple if

if( a < 20 )
{
printf("a is less than 20\n" );
}
Dr. Deepika Bhatia 21
Simple if statement

Dr. Deepika Bhatia 22


If ……else statement

if( a < 20 )
{
printf("a is less than 20\n" );
} else {
printf("a is not less than 20\n" );
Dr. Deepika Bhatia 23
}
Nesting of if…else statements

//this is nested if statements


if(x==20)
{
if(y==30)
{
printf("value x -20,value y 30.");
}
}

Dr. Deepika Bhatia 24


int main() else
{
{
if(num2 > num3)
int num1, num2, num3; {
printf("Enter three numbers: "); printf("Num2 is max.");
}
scanf("%d%d%d", &num1, &num2, &num3);
else
if(num1 > num2) {
{ printf("Num3 is max.");
}
if(num1 > num3)
}
{ return 0;
printf("Num1 is max.");
}
}
Output:
else Enter three numbers: 10
{ 20
printf("Num3 is max."); 30
} Num3 is max.
} Program to find GREATEST of 3 Numbers using nested if25else
Dr. Deepika Bhatia
Dr. Deepika Bhatia 26
Else if ladder- see book for flowchart

END

Dr. Deepika Bhatia 27


WAP to find greater of 2 numbers using if ELSE IF LADDER

if (a > b)
{
printf("\n a is greater than b");
}
else if (b > a)
{
printf("\n b is greater than a");
}
else
{
printf("\n Both are equal");
} Dr. Deepika Bhatia 28
Switch Statement in C
• Switch statement in C When you want to solve multiple option
type problems, for example: Menu like program, where one
value is associated with each option and you need to choose only
one at a time, then, switch statement is used.
• Switch statement is a control statement that allows us to choose
only one choice among the many given choices.

• The expression in switch evaluates to return an integral value,


which is then compared to the values present in different cases.
• It executes that block of code which matches the case value. If there
is no match, then default block is executed(if present,optional).

Dr. Deepika Bhatia 29


Switch statement- multiway decision

Expression is an integer expression or characters.


Value1---2 etc are case labels, are constants or constant
expression(evaluable to integral constants)
Break statements causes exit and transfers control to statement-x. it is
Dr. Deepika Bhatia 30
optional.
The following rules apply to a switch statement −

•The expression used in a switch statement must have an integral or


enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
•You can have any number of case statements within a switch. Each case
is followed by the value to be compared to and a colon.
•The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
•When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
•When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
•Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
•A switch statement can have an optional default case, which must appear
at the end of the switch. The default case can be used for performing a task
when none of the cases is true.Dr.
No break
Deepika is needed in the default case.31
Bhatia
char grade = 'B’;
Example switch(grade)
{
case 'A’ :
printf("Excellent!\n" );
break;
case 'B’ :
case 'C’ :
printf("Well done\n" );
break;
case 'D’ :
printf("You passed\n" );
break;
case 'F’ :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
Output is :
Output: Well done
ABC 32
Your grade is B
Rules for using switch statement

1.The expression (after switch keyword) must yield


an integer value i.e the expression should be an integer or a
variable or an expression that evaluates to an integer.
2.The case label values must be unique.
3.The case label must end with a colon(:)
4.The next line, after the case statement, can be any valid C
statement.

Dr. Deepika Bhatia 33


Difference between switch and if

•if statements can evaluate float conditions.


•switch statements cannot evaluate float conditions.
•if statement can evaluate relational operators.
•switch statement cannot evaluate relational operators i.e
they are not allowed in switch statement.

Dr. Deepika Bhatia 34


Thankyou!

Dr. Deepika Bhatia 35

You might also like