1
Unit – 3
Question-1
What do you mean by control flow statements? Describe all the types of control
flow statements with proper syntax and example.
Or,
What do you mean by iterative statements? Give all the types of iterative
statements.
Or,
What do you mean by loop? What are the types of loop? Give syntax of all the
types of loops.
Or,
Write the syntax of following:
(a) for loop
(b) while loop
(c) do while loop
Answer-1
Iterative or control flow statements are used whenever we have requirement to
repeat a particular block of statements till the given condition satisfies. These
are of following types-
1. for loop or Universal loop
2. while loop
3. do while loop
for loop:
syntax:
for(initialize counter ; test counter ; increment/decrement counter)
{
----------------------
---------------------- Block of statements
----------------------
}
For example: WAP to print “HELLO WORLD” five
times.
#include<stdio.h>
void main( )
{
int i ;
for(i=1; i<=5; i++)
{
printf (“HELLO WORLD”);
}
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
2
Unit – 3
while loop:
syntax:
initialize counter ;
while(test counter)
{
----------------------
---------------------- Block of statements
----------------------
increment/decrement counter;
}
For example: WAP to print “HELLO WORLD” five times.
#include<stdio.h>
void main( )
{
int i ;
i = 1;
while(i<=5)
{
printf (“HELLO WORLD”);
i++ ;
}
}
do while loop:
syntax:
initialize counter ;
do
{
----------------------
---------------------- Block of statements
----------------------
increment/decrement counter;
} while(test counter);
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
3
Unit – 3
For example: WAP to print “HELLO WORLD” five times.
#include<stdio.h>
void main( )
{
int i ;
i = 1;
do
{
printf (“HELLO WORLD”);
i++ ;
} while(i<=5);
}
Question-2
What are the differences between while and do while loop?
Answer-2
while loop do while loop
1 It is an Entry level loop. It is an exit level loop.
2 It can not be evaluated even a It must be evaluated at least a
single time, if condition fails at single time, even if condition fails
starting. at starting.
3 Syntax: Syntax:
Initialize counter; Initialize counter;
while(Test counter) do
{ {
-------------- --------------
-------------- Block of statements -------------- Block of statements
-------------- --------------
Increment/Decrement counter; Increment/Decrement counter;
} } while(Test counter);
4 Example: Example:
int i=1; int i=1;
while(i<=5) do
{ {
printf(“Hello World”); printf(“Hello World”);
i++ ; i++ ;
} } while(i<=5);
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
4
Unit – 3
Question-3
Write short notes on following with example:
1. break statement
2. continue statement
ANSWER-3
break statement:
When break is encountered inside any loop, control automatically passes to the
first statement out of the loop. A break is usually associated with an if or
switch case statement.
Syntax: break;
For example:
#include <stdio.h>
void main( )
{
char c;
for(;;)
{
printf_s( "\nPress any key, Q to quit: " );
scanf_s("%c", &c);
if (c == 'Q' )
break;
else
{
printf(“\n%d”, i );
i++ ;
}
}
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
5
Unit – 3
continue statement:
When continue statement is encountered inside any loop, it skips all the
remaining statements after the continue statement and goes to the next
iteration of loop.
Syntax: continue;
For example:
#include <stdio.h>
void main( )
{
int i, j;
for(i=1; i<=2; i++)
{
for(j=1; j<=2; j++)
{
if(i==j)
{
continue;
}
printf(“\n%d %d”,i ,j);
}
}
}
Question-4
What is function or module? What are the elements or parts of the function?
What are the advantages of using functions? Explain with proper example.
Or,
What do you mean by modular programming? What are the advantages of it
over structured programming?
ANSWER-4
Function:
A function or module is a group of statements that can be used to perform
specific type of task. When program is too complicated then it may be divided
into smaller modules.
So, the programming based on modules or functions may be known as
modular programming.
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
6
Unit – 3
Any function in C has three basic elements or parts-
1. Function prototype or function declaration
2. Function call
3. Function definition
Main Program
Module1 Module2 Module N
Function prototype:
A function prototype tells the compiler the name of the function, the type of
data returned by the function, the number and sequences of arguments
(parameters).
Syntax:
ReturnType FunctionName(ArgumentList);
Function call:
Whenever you are required to call the function, you just give the name of that
function with arguments(in same type and sequence as declared).
Syntax:
FunctionName(ArgumentList);
Main or Calling
Program
Function Call
Function
----------------
----------------
----------------
return
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
7
Unit – 3
Function definition:
A function definition contains name of the function with its return type, the
argument list passed in parenthesis (if any), the body of the function which
contains executable statements.
Syntax:
ReturnType FunctionName(ArgumentList)
{
---------------------
---------------------Block of statements
---------------------
return ;
}
For example: WAP to find the sum of two integers using user defined function.
#include<stdio.h>
int sum(int, int); //Function prototype
void main( )
{
int a, b, s;
printf(“ Enter two numbers ”);
scanf(“ %d %d ”, &a, &b);
s = sum(a, b); //Function call
printf(“ Sum= %d ”, s);
}
int sum(int x, int y) //Function definition
{
int z ;
z=x + y ;
return (z);
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
8
Unit – 3
Advantages of functions:
Following are the advantages of using functions:
1. Reusability:
If a particular set of instructions has to be accessed many times in a
same program, then we can make a group of that set of instructions and
can say it a function or module.
2. Debugging is easier: since each module is smaller and clearer, the user
can easily locate the errors and correct them.
3. Build library: It allows the programmer to build a library of most
commonly used subprograms.
Question-5
What do you mean by return statement? Give the syntax.
Answer-5
return Statement:
The keyword return is used to return any value from the function called. This
statement terminates the function and returns a value to its caller.
Syntax: return; // without expression
or, return(expression); // with expression
Question-6
What are the types of functions?
Answer-6
Functions are of two types:
1. System defined functions
2. User defined functions
System defined functions:
Functions that are predefined in header files and we may use directly by calling
them, are known as system defined functions.
For example:
printf( ), scanf( ), clrscr( ), getch( ), etc.
User defined functions:
Functions that must be defined by the programmer or user whenever required
are known as user defined functions.
For example:
int sum(int a, int b)
{
int s;
s = a + b;
return (s);
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
9
Unit – 3
Question-7
Explain parameter passing mechanism in function.
Or,
Explain the following with example:
(a) Call by value
(b) Call by reference
Answer-7
Whenever a function calls another function with some arguments, their values
are passed to the called function’s arguments, this is known as parameter
passing. There are two methods of parameter passing-
1. Call by value
2. Call by reference
Call by value:
In this method, the actual values of arguments are passed from the calling
function to the called function, the values are copied into the called function. If
any changes are made to the values in the called function, there is no change
in the original values within the calling function.
For example:
Program to swap two integers using call by value method.
#include<stdio.h>
void swap(int, int);
void main( )
{
int a, b;
printf(“Enter two values”);
scanf(“%d %d”, &a, &b);
swap(a, b);
printf(“\nAfter swapping a=%d b=%d”, a, b);
}
void swap(int a, int b)
{
int t;
t=a;
a=b;
b=t;
printf(“\nAfter swapping a=%d b=%d”, a, b);
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
10
Unit – 3
Call by Reference:
In this method, the actual values of arguments are not passed, instead their
addresses are passed from the calling function to the called function, the
values are not copied into the called function, only, the memory locations are
referenced. If any changes are made to the values in the called function, the
original values get changed within the calling function.
For example:
Program to swap two integers using call by Reference method.
#include<stdio.h>
void swap(int*, int*);
void main( )
{
int a, b;
printf(“Enter two values”);
scanf(“%d %d”, &a, &b);
swap(&a, &b);
printf(“\nAfter swapping a=%d b=%d”, a, b);
}
void swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
printf(“\nAfter swapping a=%d b=%d”, *a, *b);
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
11
Unit – 3
Question-8
What is Recursion? Explain with example.
Answer-8
Recursion:
If a Function calls itself repeatedly until some specified condition is satisfied
then it is known as Recursion.
For example:
Program to find the factorial of a given number using recursion.
#include<stdio.h>
int Fact(int);
void main( )
{
int num, f;
printf(“Enter number to find the factorial”);
scanf(“%d ”, &num);
f = fact(num);
printf(“\nFactorial =%d ”, f);
}
int fact(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1));
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida
12
Unit – 3
Question-9
What is nested loop statement? Explain with an example.
Answer-9
Nested loop statement:
A loop statement within another loop statement is called nested loop
statement.
For Example: #include<stdio.h>
* void main( )
** { int i, j;
*** for(i=1; i<=5; i++)
**** {
***** for(j=1; j<=i; j++)
printf(“ * ”);
printf(“\n”);
}
}
Prepared By: Mr. Ashish Kr. Gupta, Assistant Professor, Dept. of CSE, I.T.S Engineering College, Greater Noida