0% found this document useful (0 votes)
5 views

unit-2

The document discusses control structures in C programming, including decision-making statements (like if, if-else, and switch) and looping statements (such as for, while, and do-while). It explains the syntax and functionality of these control statements, along with examples for clarity. Additionally, it covers jump statements like break, continue, and goto, providing insights into their usage in controlling program flow.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

unit-2

The document discusses control structures in C programming, including decision-making statements (like if, if-else, and switch) and looping statements (such as for, while, and do-while). It explains the syntax and functionality of these control statements, along with examples for clarity. Additionally, it covers jump statements like break, continue, and goto, providing insights into their usage in controlling program flow.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT-2

CHAPTER 1: Decision control and lopping statements

Q) Explain about control structures/control statements?

1. A program is nothing but the execution of sequence of one or more instructions.


2. Sometimes it is desirable to alter the sequence of the statements in the program depending
upon certain circumstances. Those statements are called control statements or structures.
3. C supports mainly three types of control statements.

I. Decision making statements


1) Simple if Statement
2) if – else Statement
3) Nested if-else statement
4) else – if Ladder
5) switch statement
II. Loop control statements
1) for Loop
2) while Loop
3) do-while Loop
III. Unconditional control statements
1) goto Statement
2) break Statement
3) continue Statement

Q) Explain decision control/making statements (or) Conditional Statements.


C program executes program sequentially. Sometimes, a program requires checking of certain
conditions in program execution. C provides various key condition statements to check condition and
execute statements according conditional criteria. These statements are called as 'Decision Making
Statements' or 'Conditional Statements.'
Followings are the different conditional statements used in C.
1. If Statement
2. If-Else Statement
3. Nested If-Else Statement
4. Else if ladder.
5. Switch Case.

1.If Statement:
This is a conditional statement used in C to check condition or to control the flow of execution
of statements. This is also called as 'decision making statement or control statement.' The execution
of a whole program is done in one direction only.
Syntax:

if (condition)
{
Statements;
}
Statement X;

1
In this the condition is evaluated, that condition is true the statements in the curly braces executed
otherwise the statements are skipped and the execution will jump to statement X.
Example:
#include<stdio.h>
void main()
{
int m=40,n=40;
clrscr();
if (m == n)
{
printf("m and n are equal");
}
getch();
}

2. if else statement:

if else statement in C programming language is used to execute a set of statements if


condition is true it execute true block of statements otherwise execute false block of statements. Only
either if block or else block of code gets executed (not both) depending on the outcome of condition.

Syntax:
if(condition)
{
True block of statements;
}
else
{
False block of statements;
}

Example:
#include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter a value: ");
scanf("%d",&n);
if (n%2==0)
printf("It is even");
else
printf("It is odd");

}
getch();
}

2
3. Nested “if–else” Statement:
 Using of one if-else statement in another if-else statement is called as nested if-else control
statement.
 When a series of decisions are involved, we may have to use more than one if-else statement
in nested form.

Syntax:
if ( Test Condition1)
{
if ( Test Condition2)
{
Statement -1;
}
else
{
Statement -2;
}
}
else
{
if ( Test Condition3)
{
Statement -3;
}
else
{
Statement-4;
}
}

If Test Condition-1 is true then enter into outer if block, and it checks Test Condition-2 if it is true then
Statement-1 executed if it is false then else block executed i.e Statement-2.
If Test Condition -1 is false then it skips the outer if block and it goes to else block and Test
Condition-3 checks if it is true then Statement-3 executed, else Statement-4 executed.

Example:
# include<stdio.h> else
# include<conio.h> printf(“ %d”, c);
void main( ) }
{ else
int a,b,c; {
printf(“Enter Three Values:”); if (b>c)
scanf(“%d%d%d ”, &a, &b, &c); printf(“ %d ”, b);
printf(“\n Largest Value is:”) ; else
if(a>b) printf(“ %d ”, c);
{ }
if(a>c) getch( );
printf(“ %d ”, a); }

3
4. else-if ladder (or) if-else-if statement:
 if else ladder statement in C programming language is used to test set of conditions in
sequence.
 A multipath decision is a chain of if’s in which the statement associated with each else is an if.
 This construct is known as the else if ladder. The conditions are evaluated from the top of the
ladder to downwards. As soon as a true condition is found, the statement associated with it is
executed and the control is transferred to the statement-x
Syntax:
if(test_condition1)
{
statement 1;
}
else if(test_condition2)
{
statement 2;
}
else if(test_condition3)
{
statement 3;
}
else if(test_condition4)
{
statement 4;
}
——————————–
——————————– // Test Condition And Codes As Per Requirement.
——————————–
else // at last, we use only else.
{
statement x;
}

Program to accept gender and find whether he/she is eligible for voting or not
#include<stdio.h>
#include<conio.h>
void main()
{
char gender;
clrscr();
printf("enter gender");
scanf("%c",&gender);
if(gender=='m')
printf("the eligible age is 21");
else if(gender=='f')
printf("the eligible age is 18");
else
printf("check your gender");
getch();
}
4
5. Switch statement:
1. It is also called multi-way decision statement
2. A switch statement allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each switch case.
How it works:
 Switch case checks the value of expression/variable against the list of case values and when
the match is found , the block of statement associated with that case is executed
 Expression should be Integer Expression / Character
 Break statement takes control out of the case.
 Break Statement is Optional.
 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.
Syntax:
switch (expression)
{
case constant-1:
statement(s);
case constant-2:
statement(s);
- - - -
case constant-n:
statement(s);
default :
statement(s);
}
Example: #include<stdio.h>
void main()
{
int n;
clrscr();
printf("Enter a number(1-7)");
scanf("%d",&n);
switch(n)
{
case 1: printf("\n Sunday");break;
case 2: printf("\n Monday");break;
case 3: printf("\n Tuesday");break;
case 4: printf("\n Wednesday");break;
case 5: printf("\n Thursday");break;
case 6: printf("\n Friday");break;
case 7: printf("\n Saturday");break;
default: printf("Wrong input");
}
getch();
}
Advantages of switch statement:
1. Ease to debug.
2. easy to read and understand.
3. Ease of maintains.
4. Switch statement also be nested. 5. Executes faster.
5
Q) Explain looping statements (or) iterative statements?
Iterative statements are used to repeat the execution of a list of statements, depending on the value
of an integer expression.

Advantage with looping statement


 Reduce length of Code
 Take less memory space.
 Burden on the developer is reducing.
 Time consuming process to execute the program is reduced.

C programming supports 3 types of loops


1. while loop
2. do –while loop
3. for loop.

1. While loop:
 It is entry checking loop.
 In while loop First check the condition if condition is true then control goes inside the loop
body otherwise goes outside the body.

Syntax:
while (test expression)
{
Statements to be executed.
}

In the beginning of while loop, test expression is checked. If it is true, codes inside the body of while
loop, i.e., code/s inside parentheses are executed and again the test expression is checked and
process continues until the test expression becomes false.

Example:

#include <stdio.h>
void main()
{
int i = 1;
clrscr();
while ( i <=10 )
{
printf("Hello %d\n", i );
i = i +1;
}
getch();
}

2. do-while loop:-
 In do-while, the condition is checked at the end of the loop.
 The do-while loop will execute at least one time even if the condition is false initially.
 The do-while loop executes until the condition becomes false.
Syntax:
6
do
{
Body of the loop
Updation Expression; } while ( Test Condition);
}

Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf(“Hello %d”,i);
i=i+1;
}while(i<=10);
getch();
}

3. “for” loop:
If we know the exact number of times for repetition of block of statements, then for statement can be
used. It is an entry controlled loop.
Syntax:
for (initialization; test condition; increment)
{
body of loop
}
1. The initialization sets a loop to an initial value. This statement is executed only once.
2. The test conditions a relational expression that determines the number of iterations desired or it
determines when to exit from the loop.
->The for loop continues to execute as long as conditional test is satisfied.
-> When the condition becomes false the control of the program exits from the body of for loop
and executes next statements after the body of the loop.
3. The updation(increment or decrement operations) decides how to make changes in the loop.
The body of the loop may contain either a single statement or multiple statements.
Example:
Write a program to print number from 1 to 10;
void main()
{
int I;
clrscr();
for(i=0;i<=10;i++)
{
printf(“%d”,i);
}
getch();
}
Additional features:
7
1. More than one variable can be initialized at a time in the for statement
Example:
for(p=1, i=1; i<=10; i++)
2. The increment section also may have more than one part.
Example:
for(p=10, i=1; i<=10; i++,p--)
3. The test condition may have any compound relation use logical operators..
Example:
for(p=1; p<=10 && sum <100; p++)
{
------
-------
}
4. The programmer is also allowed to use expressions in the assignment statements of
initialization and increment sections.
Example:
for(x=(m+n)/2; x>10; x=x/2)
{
………
………
}
5. Both the initialization and the increment sections can be omitted in the for statement.
In such cases the sections are left blank, but the semi colons must remain.
Example:
m=5;
for ( ; m<=10; )
{
printf(“%d”,m);
m =m+5;
}
6. Time delays can be set up using null statements
Example:
for( a=1000; a>0; a--);
This loop is executed 1000 times without producing any output and thus causes a
time delay. This kind of statement is known as the null statement.

Nested loops:
C allows its users to have nested loop, i.e., loops that can be placed inside other loops.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%5d",j);
8
}
printf("\n");
}
getch();
}

Q) Explain jumps in loops (OR) explain break and continue statements?


Loops perform a set of operations repeatedly until the control variable fails to satisfy the test
condition. Sometimes it is necessary to skip some part of the loop or to leave from the loop. In that
cases c supports break and continue statements.

1. Break statement:
Break statement are used for terminates any type of loop e.g, while loop, do while loop or for loop.
The break statement terminates the loop body immediately and passes control to the next statement
after the loop. In case of inner loops, it terminates the control of inner loop only.
Break statement are mainly used with loop and switch statement. often use the break statement
with the if statement.
 with loop statement
 with switch case
 with if statement
Example:
#include <stdio.h>
void main()
{
int num =0;
while(num<=10)
{
if (num==7)
{
break;
}
num++;
}
printf("Out of while-loop");
}

Output: 0 1 2 3 4 5 6

2. Continue statement:
Continue statement are used to skips the rest of the current iteration in a loop and returns to
the top of the loop. The continue statement works like a shortcut to the end of the loop body
Example:

#include<stdio.h>
#include<conio.h>
void main()
{

9
int i=1;
clrscr();
for(i=1; i<=10; i++)
{
if(i==3) //if value of i is equal to 3, it will continue the loop
{
continue;
}
printf("%d\t ",i);
}//end of for loop

getch();
}

Output:
1 2 4 5 6 7 8 9 10

Q) Write about goto statement?

 A goto statement in C programming provides an unconditional jump from the 'goto' to a


labeled statement in the same function.
 Label is an identifier that specifies the place where the branch to be made.
 Label can be any valid variable name fallowed by colon(:).
 The label can be placed anywhere in the program either before or after the goto statement.
 If the label is placed after the goto statement it is called forwared jump.
 It the label is placed before the goto statement it is called backward jump.

goto label
………. Forward jump
……….
label:
statements:

label:
statements
……….
………. Backward jump
goto label
example:
#include <stdio.h>
void main ()
{
int a = 10;
LOOP:do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
10
goto LOOP;
}

printf("value of a: %d\n", a);


a++;

}while( a < 20 );
return 0;
}

Lab programs:
Lab 1. find out the given number is perfect number or not using c program
#include<stdio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("enter the nubmer\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
sum=sum+i;
}
if(sum==n)
printf("number %d is perfect number\n",n);
else
printf("number % d is not perfect \n",n);
getch();
}
Lab 2: write a program to check whether the given number is Armstrong or not.
#include<stdio.h>
void main()
{
int n,m,sum=0,r;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
m=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(m==sum)
printf("the number %d is armstrong",m);
else
printf("the number %d is not armstrong",m);
11
getch();
}

Lab3: Write a program to find the sum of individual digits of a positive number.
#include<stdio.h>
void main()
{
int n,r,sum=0;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("the individual digit sum is%d",sum);
getch();
}
Lab 4: write a program to generate Fibonacci series.
#include<stdio.h>
void main()
{
int n,i,f1=0,f2=1,f3;
clrscr();
printf("enter the range\n");
scanf("%d",&n);
printf("%d\n%d\n",f1,f2);
for(i=3;i<=n;i++)
{
f3=f1+f2;
printf("%d\n",f3);
f1=f2;
f2=d3;
}
getch();
}

12

You might also like