1. GANDHINAGAR INSTITUTE
OF TECHNOLOGY
SUBJECT : COMPUTER PROGRAMMING AND
UTILIZATION(2110003)
TOPIC : “Control Structures in C .”
Guided by : Prof. Archana Singh
MADE BY:-JAYDEV PATEL(150120119127)
KRUNAL PATEL(150120119128)
JAIMIN PATEL(150120119126)
2. CONTENT
• Introduction
• Decision Making Statement
i. If else statement
ii. Ladder if else
iii. Switch statement
• Looping
i. While loop
ii. do-while loop
iii. For loop
iv. Nesting loop
v. Flow breaking statement
3. Introduction
• The important and essential part of a
programming language is their control
structures.
1. Sequence
2. Decision
3. Looping
4. Decision making statement
• This is same as choosing one of the alternatives
out of the two alternatives available.
• There are various types of decision making
statements:-
i. If else statement
ii. Ladder if-else
iii. Switch
iv. goto
5. If-else statement
• The syntax of if else statement is:
if(condition)
statement 1;
else
statement 2;
7. If-else statement
• Here the condition is represented by relational or
logical expression.
• If condition is true, then statement 1 is executed and if
condition is false statement 2 is executed.
• A compound statement must be enclosedin pair of
braces{}.
• Enclosing in a single statement is not compulsory.
• The else part is is optional. The if statement without
else looks like:-
if(condition)
statement;
8. Write a program to print minimum of
two integers.
#include<stdio.h>
Void main()
{
Int x,y,min;
Printf(“enter two integers”);
Scanf(“%d %d”,&x,&y);
If(x<y)
min=x;
Else
min=y;
Printf(“minimum is:%dn”,min);
}
• Output:-
enter two integers
10 6
Minimum is :6
9. Ladder if-else
• The if else is used for two way decision where we select one of the
alternative.
• But for more than that c supports more multiple if-else or ladder if else for
this purpose.
• The syntax of multiple if else is:-
• If(condition 1)
statement1;
else if(condition2)
statement2;
.
.
else if(conditionN)
statementN;
else
default_statement;
10.
11. Ladder if else
• The condition1,condition2,…,conditionN are
represented in logical or relational
expression s .
• if the first condition is true than statement1
will be executed or the other conditions will
be evaluated till the correct condition is
found.
• If the true condition is not found than at last
the default statement will be executed.
12. Write a program to compute the
electricity bill.
#include<stdio.h>
Void main()
{
Int units;
Float ucharge,fcharge,gtax,meter_rent= 25.0,total;
Printf(“enter number of units consumed”);
Scanf(“%d”,units);
If(units<=50)
Ucharge=0.5*units;
Else if(units<=100)
Ucharge=50*0.5+(units-50)*0.75;
Else if(units<=200)
Ucharge=50*0.5+50*0.75+(units-100)*1.00;
Else
Ucharge=50*0.5+50*0.75+100*1.00+(units-200)*1.50;
Fcharge=0.40*ucharge;
Gtax=0.10*(ucharge+fcharge);
Total=ucharge+fcharge+gtax+meter_rent;
Printf(“total bill: %fn”,total);
}
14. Switch statement
• The switch in C is a multi-choice statement.
• It provides one choice for each value of variable expression.
• The syntax of switch is given below:
Switch(variable) {
Case label1: statement_block1;
break;
Case label2: statement_block2;
break;
.
.
default: default_block
15.
16. Switch statement
• Lebel1 ,label2,…. Show the possible value of
the variable or expression.
• After every statement block there is a break.
• Break sends control to the next switch
statement.
• The last choice is default which is chosen
when the value of expression does not match
to any of the expression.
• But default is optional.
17. Write a program to perform arithmetic
operations.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int choice,a,b;
Clrscr();
Printf(“1. add n”);
Printf(“2. subn”);
Printf(“3. muln”);
Printf(“4. divn”);
Printf(“Enter your choice:”);
Scanf(“%d”,&choice);
Printf(“enter two numbers”);
Scanf(“%d %d “,&a,&b);
Switch(choice) {
Case 1: printf(“answer=%dn”,a+b);
break;
Case 2: printf(“answer=%dn”,a-b);
break;
Case 3: printf(“answer=%dn”,a*b);
break;
Case4 : printf(“answer=%dn”a/b);
break;
Default: break;
}
}
19. Goto statement
• The goto statement in C programming is used
to transfer the control unconditionally from
one part of program to another.
• The goto statement can be dangerous to
programming languages.
• The syntax is
Goto label;
20. Write a program to print the sum of 1
to N using goto statement.
#include<stdio.h>
Void main()
{
Int i=1,sum=0,n;
Printf(“Enter value of N”);
Scanf(“%d”,&n);
next:
Sum= sum+I;
i+ =1;
If(I <= n )
Goto next;
Printf(“sum = %dn”,sum);
}
22. Looping
• A looping is an important control structure in
higher level programming language.
• The objective of looping is to provide
repetition of the part of the program i.e. block
of statements in a program, number of times.
23. Types of loop
• There are two types of loop:
1. Entry control
2. Exit control
• In entry control loop the condition is checked
first and then the body is executed, while in
exit control the body is excuted first and
then the condition is checked.
24.
25. While loop
• The syntax of while loop is:
While(condition)
{
body;
}
26.
27. While loop
• Here the condition is evaluated first and then
the body is executed. After executing the body
the condition is evaluated again and if it is
true body is executed again. This process is
repeated as long as the condition is true.
• While loop is a entry control loop.
• It is not necessary to enclose body of loop in
pair of {} if body contains single statement.
28. Write program to compute the sum of
following series.
1²+2²+…+10²
#include<stdio.h>
Void main()
{
Int sum=0,n=1;
While(n<=10)
{
sum = = n*n;
n++;
}
Printf(“sum of series :%dn”,sum);
}
30. Do-while loop
• The syntax of do-while loop is:
do{
Body;
} while (condition);
31.
32. Do-while loop
• In do-while first the body is executed and then
the condition is checked. If the condition is
true the body is again executed. The body is
executed as long as the condition is true.
• Do-while loop is a exit control loop.
• This ensures the body is atleast once executed
even if the condition is false.
33. For loop
• The syntax of for loop is :
For(<e1>;<e2>;<e3>)
{
body;
}
34.
35. For loop
• <e1> is initialization of variable by initial
values.<e1> executes only once when we
enter in loop. If they are more than one they
should be separated by commas.
• <e2> is the condition and the if it is true, then
the control enters into the body of the loop
and executes the statement in body.
• After executing the body of the loop, <e3> is
executed which modifies the variable.
36. Write a program to print the
multiplication table for a given number
N.
#include<stdio.h>
Void main()
{
Int I,n;
Printf(“enter a number”);
Scanf(“%d”,&n);
Printf(“Table->n”);
For(i=1,1<=10;i++)
Printf(“%4d*%2d=%4dn’,n,I,n*i);
}
40. Write a program to print the
triangle.for n=5
#include<stdio.h>
Void main()
{
Int n,l,j;
Printf(“enter number of lines”);
Scanf(“%d”,&n);
Printf(“triangle ->n”);
For(l=1;l<=n;l++)
{for(j=1;j<=1;j++)
Printf(“%d”,j);
Printf(“n”);
}
}