You are on page 1of 32

ASSIGMENT

PROGRAMMING PRINCLES AND ALGORITHM


UNIT-3

1. Describe the different types of control statements.


Ans. The control flows from one instruction to the next
instruction until now in all programs. This control flow
from one command to the next is called sequential control
flow. Nonetheless, in most c programs the programmer
may want to skip instructions or repeat a set of
instructions repeatedly when writing logic. This can be
referred to as sequential control flow. The declarations in c
let programmers make such decisions which are called
decision-making or control declarations
Types of control statements in c
C also supports an unconditional set of branching
statements that transfer the control to another location in
the program. Selection declarations in c.
1. if statements

2. Switch statement
3. Loop statements
1. If statements
if statement enables the programmer to choose a set of
instructions, based on a condition. When the condition is
evaluated to true, a set of instructions will be executed and
a different set of instructions will be executed when the
condition is evaluated to false. We have 4 types of if
statement which are:
1. If..else
2. Nested if
 If…else statement
In this statement, there are two types of statements
execute. First, if the condition is true first statement will
execute if the condition is false second condition will be
executed.
Syntax:
If(condition)
{
statement(s);
}
else
{
statement(s)
}
statement
 Nested if
If the condition is evaluated to true in the first if statement,
then the condition in the second if statement is
evaluated and so on.
Syntax:
If(condition)
{
if(condition)
{
statement(s);
}
else
{
statement(s)
}
}
2. Switch statement
C offers a selection statement in several ways as if the
program becomes less readable when the number of
conditions increases. C has a multi-way selection
statement called the switch statement that is easy to
understand to resolve this problem. The switch declaration
is easy to understand if more than 3 alternatives exist. The
command switches between the blocks based on the
expression value. Each block will have a corresponding
value.
Syntax:
Switch(expression)
{
case label1:
statement(s);
break;
case label2:
statement(s);
break;
case label3;
statement(s);
break;
….
Case labeln:
statement(s);
break;
default:
statement(s);
break;
}
Using the case keyword every block is shown and the
block label follows the case keyword. The default block
and the break statement are optional in a switch
statement.
 loop statements
The programmer may want to repeat several instructions
when writing c programs until some requirements are met.
To that end, c makes looping declarations for decision-
making. We have three types of loops,
1. For loop
2. While loop
3. do while loop

1. For loop
In the for loop, the initialization statement is executed only
one time. After that, the condition is checked and if the
result of condition is true it will execute the loop. If it is
false, then for loop is terminated. However, the result of
condition evaluation is true, statements inside the body of
for loop gets executed, and the expression is updated.
After that, the condition is checked again. This process
goes on until the result of the condition becomes false.
When the condition is false, the loop terminates.
Syntax:
For( initialization statement; condition)
{
//statements inside the loop
}
2. While loop
In c, the while loop is a guided entry loop. The body of the
while loops is only performed if the condition is valid. The
loop structure is not executed if the condition scores to
incorrect.
The while loops are usually used when several instructions
have to be repeated for an indefinite time.
Syntax:
While(condition)
{
//statements inside the loop
}
3. Do while loop
Unlike while loop, the body of the do is the difference
between while and … while loop is guaranteed to be done
once at a time.
Syntax:
Do
{
//statements inside the loop
}
while(condition);

2. Describe the proper syntax & example of the following


conditional constructs: if…..else and switch….case.
Ans. if….else statement
Syntax
a)
Example
Switch…case statement
A general syntax of how switch-case is implemented in a
'C' program is as follows:
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x;
Example

#include <stdio.h>

int main() {

int num = 8;

switch (num) {

case 7:
printf("Value is 7");

break;

case 8:

printf("Value is 8");

break;

case 9:

printf("Value is 9");

break;

default:

printf("Out of range");

break;

return 0;

}
Output:

Value is 8
3. Describe difference b/w if….else and switch…case.

Ans. The following are the differences


between if-else and switch statement are:
 Definition

if-else
Based on the result of the expression in the 'if-
else' statement, the block of statements will be
executed. If the condition is true, then the 'if' block
will be executed otherwise 'else' block will execute.
Switch statement
The switch statement contains multiple cases or
choices. The user will decide the case, which is to
execute.

 Expression

If-else
It can contain a single expression or multiple
expressions for multiple choices. In this, an
expression is evaluated based on the range of
values or conditions. It checks both equality and
logical expressions.
Switch
It contains only a single expression, and this
expression is either a single integer object or a
string object. It checks only equality expression.

 Evaluation

If-else
An if-else statement can evaluate almost all the
types of data such as integer, floating-point,
character, pointer, or Boolean.
Switch
A switch statement can evaluate either an integer
or a character.

 Sequence of Execution

If-else
In the case of 'if-else' statement, either the 'if'
block or the 'else' block will be executed based on
the condition.
Switch
In the case of the 'switch' statement, one case
after another will be executed until
the break keyword is not found, or the default
statement is executed.

 Default Execution

If-else
If the condition is not true within the 'if' statement,
then by default, the else block statements will be
executed.
Switch
If the expression specified within
the switch statement is not matched with any of
the cases, then the default statement, if defined,
will be executed.

 Values

If-else
Values are based on the condition specified inside
the 'if' statement. The value will decide either the
'if' or 'else' block is to be executed.
Switch
In this case, value is decided by the user. Based on
the choice of the user, the case will be executed.
 Use

If-else
It evaluates a condition to be true or false.
Switch
A switch statement compares the value of the
variable with multiple cases. If the value is
matched with any of the cases, then the block of
statements associated with this case will be
executed.

 Editing

If-else
Editing in 'if-else' statement is not easy as if we
remove the 'else' statement, then it will create the
havoc.
Switch
Editing in switch statement is easier as compared
to the 'if-else' statement. If we remove any of the
cases from the switch, then it will not interrupt the
execution of other cases. Therefore, we can say
that the switch statement is easy to modify and
maintain.
 Speed

If-else
If the choices are multiple, then the speed of the
execution of 'if-else' statements is slow.
Switch
The case constants in the switch statement create
a jump table at the compile time. This jump table
chooses the path of the execution based on the
value of the expression. If we have a multiple
choice, then the execution of the switch statement
will be much faster than the equivalent logic of 'if-
else' statement.

4. Describe the different types of iteration (loop) in C.

Ans. 'C' programming language provides us with three types of


loop constructs:

1. The while loop

2. The do-while loop

3. The for loop


While Loop in C

A while loop is the most straightforward looping structure.


Syntax of while loop in C programming language is as follows:

While (condition) {

Statements;

}
It is an entry-controlled loop. In while loop, a condition is
evaluated before processing a body of the loop. If a condition is
true then and only then the body of a loop is executed. After
the body of a loop is executed then control again goes back at
the beginning, and the condition is checked if it is true, the
same process is executed until the condition becomes false.
Once the condition becomes false, the control goes out of the
loop.

After exiting the loop, the control goes to the statements which
are immediately after the loop. The body of a loop can contain
more than one statement. If it contains only one statement,
then the curly braces are not compulsory. It is a good practice
though to use the curly braces even we have a single statement
in the body.

In while loop, if the condition is not true, then the body of a


loop will not be executed, not even once. It is different in do
while loop which we will see shortly.
Following program illustrates while loop in C programming
example:

#include<stdio.h>

#include<conio.h>

Int main()

Int num=1; //initializing the variable

While(num<=10) //while loop with condition

Printf("%d\n",num);

Num++; //incrementing operation

Return 0;

Output:

2
3

10

The above program illustrates the use of while loop. In the


above program, we have printed series of numbers from 1 to
10 using a while loop.

1. We have initialized a variable called num with value 1. We


are going to print from 1 to 10 hence the variable is
initialized with value 1. If you want to print from 0, then
assign the value 0 during initialization.
2. In a while loop, we have provided a condition (num<=10),
which means the loop will execute the body until the value
of num becomes 10. After that, the loop will be
terminated, and control will fall outside the loop.
3. In the body of a loop, we have a print function to print our
number and an increment operation to increment the
value per execution of a loop. An initial value of num is 1,
after the execution, it will become 2, and during the next
execution, it will become 3. This process will continue until
the value becomes 10 and then it will print the series on
console and terminate the loop.

\n is used for formatting purposes which means the value will


be printed on a new line.

Do-While loop in C

A do...while loop in C is similar to the while loop except that the


condition is always executed after the body of a loop. It is also
called an exit-controlled loop.

Syntax of do...while loop in C programming language is as


follows:

Do {

Statements

} while (expression);

As we saw in a while loop, the body is executed if and only if


the condition is true. In some cases, we have to execute a body
of the loop at least once even if the condition is false. This type
of operation can be achieved by using a do-while loop.
In the do-while loop, the body of a loop is always executed at
least once. After the body is executed, then it checks the
condition. If the condition is true, then it will again execute the
body of a loop otherwise control is transferred out of the loop.

Similar to the while loop, once the control goes out of the loop
the statements which are immediately after the loop is
executed.

The critical difference between the while and do-while loop is


that in while loop the while is written at the beginning. In do-
while loop, the while condition is written at the end and
terminates with a semi-colon (;)

The following loop program in C illustrates the working of a do-


while loop:

Below is a do-while loop in C example to print a table of


number 2:

#include<stdio.h>

#include<conio.h>

Int main()

Int num=1; //initializing the variable

Do //do-while loop
{

Printf("%d\n",2*num);

Num++; //incrementing operation

}while(num<=10);

Return 0;

Output:

10

12

14

16

18
20

In the above example, we have printed multiplication table of 2


using a do-while loop. Let's see how the program was able to
print the series.

1. First, we have initialized a variable 'num' with value 1.


Then we have written a do-while loop.
2. In a loop, we have a print function that will print the series
by multiplying the value of num with 2.
3. After each increment, the value of num will increase by 1,
and it will be printed on the screen.
4. Initially, the value of num is 1. In a body of a loop, the print
function will be executed in this way: 2*num where
num=1, then 2*1=2 hence the value two will be printed.
This will go on until the value of num becomes 10. After
that loop will be terminated and a statement which is
immediately after the loop will be executed. In this case
return 0.

For loop in C

A for loop is a more efficient loop structure in 'C' programming.


The general structure of for loop syntax in C is as follows:

For (initial value; condition; incrementation or

decrementation )
{

Statements;

 The initial value of the for loop is performed only once.


 The condition is a Boolean expression that tests and
compares the counter to a fixed value after each iteration,
stopping the for loop when false is returned.
 The incrementation/decrementation increases (or
decreases) the counter by a set value.

Following program illustrates the for loop in C programming


example:

#include<stdio.h>

Int main()

Int number;

For(number=1;number<=10;number++) //for loop to

print 1-10 numbers

{
Printf("%d\n",number); //to print the

number

Return 0;

Output:

10
The above program prints the number series from 1-10 using
for loop.

1. We have declared a variable of an int data type to store


values.
2. In for loop, in the initialization part, we have assigned
value 1 to the variable number. In the condition part, we
have specified our condition and then the increment part.
3. In the body of a loop, we have a print function to print the
numbers on a new line in the console. We have the value
one stored in number, after the first iteration the value
will be incremented, and it will become 2. Now the
variable number has the value 2. The condition will be
rechecked and since the condition is true loop will be
executed, and it will print two on the screen. This loop will
keep on executing until the value of the variable becomes
10. After that, the loop will be terminated, and a series of
1-10 will be printed on the screen.

In C, the for loop can have multiple expressions separated by


commas in each part.

For example:

For (x = 0, y = num; x < y; i++, y--) {

Statements;

}
Also, we can skip the initial value expression, condition and/or
increment by adding a semicolon.

For example:

Int i=0;

Int max = 10;

For (; i < max; i++) {

Printf("%d\n", i);

Notice that loops can also be nested where there is an outer


loop and an inner loop. For each iteration of the outer loop, the
inner loop repeats its entire cycle.

Consider the following example, that uses nested for loop in C


programming to output a multiplication table:

#include <stdio.h>

Int main() {

Int i, j;

Int table = 2;

Int max = 5;
For (i = 1; i <= table; i++) { // outer loop

For (j = 0; j <= max; j++) { // inner loop

Printf("%d x %d = %d\n", i, j, i*j);

Printf("\n"); /* blank line between tables */

}}

Output:

1x0=0

1x1=1

1x2=2

1x3=3

1x4=4

1x5=5

2x0=0

2x1=2
2x2=4

2x3=6

2x4=8

2 x 5 = 10

The nesting of for loops can be done up-to any level. The
nested loops should be adequately indented to make code
readable. In some versions of 'C,' the nesting is limited up to 15
loops, but some provide more.

The nested loops are mostly used in array applications which


we will see in further tutorials.

Break Statement in C

The break statement is used mainly in the switch statement. It


is also useful for immediately stopping a loop.

We consider the following program which introduces a break to


exit a while loop:

#include <stdio.h>

Int main() {

Int num = 5;

While (num > 0) {


If (num == 3)

Break;

Printf("%d\n", num);

Num--;

}}

Output:

4
Continue Statement in C

When you want to skip to the next iteration but remain in the
loop, you should use the continue statement.

For example:

#include <stdio.h>

Int main() {

Int nb = 7;

While (nb > 0) {

Nb--;
If (nb == 5)

Continue;

Printf("%d\n", nb);

}}

Output:

So, the value 5 is skipped.

5. Describe difference b/w while and do…while loop.


Ans.
while do-while
Condition is checked first then Statement(s) is executed atleast
statement(s) is executed. once, thereafter condition is checked.
It might occur statement(s) is At least once the statement(s) is
executed zero times, If condition is executed.
false.
No semicolon at the end of while. Semicolon at the end of while.
while(condition) while(condition);
If there is a single statement, Brackets are always required.
brackets are not required.
Variable in condition is initialized variable may be initialized before or
before the execution of loop. within the loop.
while loop is entry controlled loop. do-while loop is exit controlled loop.
while(condition) do { statement(s); }
{ statement(s); } while(condition);
BASIS FOR BREAK CONTINUE
COMPARISON
Task It terminates It terminates only the current
the execution iteration of the loop.
of remaining
iteration of the
loop.
Control after 'break' 'continue' resumes the contro
break/continue resumes the the program to the next iterat
control of the that loop enclosing 'continue'
program to the
end of loop
enclosing that
'break'.
Causes It causes early It causes early execution of th
termination of next iteration.
loop.
Continuation 'break' stops 'continue' do not stops the
the continuation of loop, it only st
continuation of the current iteration.
loop.
Other uses 'break' can be 'continue' can not be execute
used with with 'switch' and 'labels'.
'switch', 'label'.
6. Describe the following exit statements:i)break & continue,
ii) return and exit.
return statement
The return statement terminates the execution of function
and it returns the control to the calling function. It calls the
constructor as well as the destructor. It returns an integer
value for “int main()”.
The following is the syntax of return statement.
return expression;
Here,
expression − The expression or any value to be returned.
The following is an example of return statement.
exit()
The function exit() is used to terminate the calling function
immediately without executing further processes. As exit()
function is called, the process gets terminated. It calls the
constructor of class only. It is declared in “stdlib.h” header
file in C language. It does not return anything.
The following is the syntax of exit()
void exit(int status_value);
Here,
status_value − The value which is returned to parent
process.
The following is an example of exit().

You might also like