1) The document discusses various conditional and looping statements in C programming such as if-else, switch, while, for, do-while loops.
2) It also covers functions in C including defining functions, passing values to functions, and recursive functions.
3) Key topics covered include conditional branching statements, nested if-else, switch statements, break and default usage, looping constructs, multiple loop variables, and break/continue statements.
2. CONDITIONAL PROGRAM EXECUTION: APPLYING IF AND SWITCH
STATEMENTS, NESTING IF AND ELSE, USE OF BREAK AND DEFAULT WITH
SWITCH.
PROGRAM LOOPS AND ITERATIONS: USE OF WHILE, DO WHILE AND FOR
LOOPS, MULTIPLE LOOP VARIABLES, USE OF BREAK AND CONTINUE
STATEMENTS.
FUNCTIONS: INTRODUCTION, TYPES OF FUNCTIONS, FUNCTIONS WITH
ARRAY, PASSING VALUES TO FUNCTIONS, RECURSIVE FUNCTIONS.
UNIT-3 TOPICS
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
2
3. UNIT-3 CONDITIONAL PROGRAM EXECUTION
C supports two types of decision control statements.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
3
Selection/Branching
statement
Conditional type
if If-else If-else-if switch
Unconditional
type
4. CONDITIONAL BRANCHING STATEMENTS:
These statements helps to jump from one part to another part.
Whether a particular condition is satisfied or not.
It includes:-
---- if statement
---- if- else statement
---- if- else- if statement
---- Switch statement
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
4
5. IF STATEMENTS
The if statement allows the program to test the state of the program
variables using a Boolean expression.
Syntax
If (test expression)
{
Statement 1;
……………
Statement n;
}
Statement x;
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
5
Test
exp
Statement block
1
Statement x
FALSE
TRUE
6. IF-ELSE STATEMENT
The if-else statement expresses simplest decision making.
The syntax is
if (expression)
statement1
elseopt
Statement2
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
6
11. SWITCH STATEMENT
The switch statement is used to select one of several alternatives when the
selection is based on the value of a single variable or an expression.
switch (controlling expression) {
case label1:
statement1
break;
case label2:
statement2
break; ……..
case labeln:
statementn
break;
default:
statementd; }
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
11
If the result of this controlling expression
matches label1, execute staement1 and then break
this switch block.
If the result matches none of all labels, execute the
default statementd.
12. USE OF BREAK AND DEFAULT WITH SWITCH
Switch (exp)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
…………….
…………….
default:
default-block
break; } statement-x;
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
12
13. PROGRAM LOOPS AND ITERATIONS:
In looping, a sequence of statements are executed until some
conditions for termination of the loop are satisfied.
Two segments are:
------- body of the loop
------- control statement
Depending on the position of the control statement in the loop,
A control structure may be either—
-------- entry controlled loop or
-------- exit controlled loop.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
13
14. LOOP CONTROL STRUCTURES
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
14
True
Test
condition
?
Test
condition
?
Body of the
loop
Body of the
loop
False
False
True
EntryEntry
(b) Exit controlled loop(a) Entry controlled loop
15. USE OF WHILE:
THE WHILE STATEMENT IS USED TO REPEAT A COURSE OF
ACTION.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
15
16. THE WHILE STATEMENT
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
16
Format
While (test condition)
{
Body of the loop
Syntax
Statement x;
While (condition)
{
statement block;
}
Statement y;
WAP to calculate the sum
of first 10 nos.
int i= 1, sum= 0;
while(i<=10)
{
sum = sum +1;
i= i+1;
}
printf(“n sum =% d”, sum);
return 0;
}
OUTPUT
Sum= 55
17. DO WHILE
DO STATEMENT
The do statement is a variant of the while statement that tests its
condition at the bottom of the loop.
General Form of the do Statement-
do
statement
while (expr);
next statement
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
17
do
{
body of the loop
}
while (test-condition);
18. EXAMPLE OF DO-WHILE STATEMENT
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
18
int i =1;
do
{
printf(“n % d”, i);
i=i+1;
}
while(i<=10);
return 0;
}
The code will print nos
from 1 to 10.
19. COMPARISON B/W WHILE, DO, FOR
WHILE DO FOR
while (...) {
...
continue;
...
cont: ;
}
do {
...
continue;
...
cont: ;
}
for (...) {
...
continue;
...
cont: ;
}
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
19
21. SYNTAX OF FOR LOOP
for (initialization ; test-condition ; increment)
{
body of the loop
}
Example
for (i = 1; i<=n ; i++)
{
printf(“n %d”, i);
}
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
21
22. MULTIPLE LOOP VARIABLES
MLV are the loops which can be placed inside other loops.
Example:
………..
………..
while(……..)
{
for(………)
{ …….
……..
if (……..) goto end_of_program;
………
}
………
………
} end_of_program
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
22
Jumping out
of loops
23. USE OF BREAK AND CONTINUE STATEMENTS.
The break statement causes an exit from the innermost enclosing
loop or switch statement.
The continue statement causes the current iteration of a loop to
stop and the next iteration to begin immediately.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
23
24. DIFFERENCE B/W BREAK & CONTINUE
break continue
while (……)
{
if (condition)
break;
………..
}
……….
Transfers control out of the loop while.
Example
int i = 1;
while (i<= 10)
{
if (i==5)
break;
printf(“n % d”, i);
i=i+1; }
return 0; }
while (……)
{
……….
if (condition)
continue;
………..
}
Transfers control to the condition
expression of the while loop.
Example
int i = 1;
for (i=1; i<=10; i++)
{
if (i==5)
continue;
printf(“t % d”, i);
}
return 0; }
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
24
25. THE GOTO STATEMENT
GOTO is used to branch unconditionally from one point to another.
The general forms of goto & label statements are shown below:
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
25
goto label;
………….
………….
………….
label;
Statement;
label;
statement;
………….
………….
………….
goto label;
Forward jump Backward jump
26. FUNCTIONS:
A complex problem is often easier to solve by dividing it into several
smaller parts, each of which can be solved by itself.
This is called structured programming.
These parts are sometimes made into functions in C.
main() then uses these functions to solve the original problem.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
26
27. Main() Function Calls Func1()
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
27
main()
{
…………
func1();
…………
return 0;
}
func1()
{
Statement block;
}
Main
function
Function A Function B
Function B1 Function B2
Function c
28. DEFINITION OF FUNCTIONS:
A function definition shall include the following elements:
1. Function name;
2. Function type;
3. List of parameters;
4. Local variable declarations;
5. Function statements; and
6. A return statement.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
28
Function header
Function body
29. FUNCTIONS WITH ARRAY
Example, the call
largest (a, n)
Will pass the whole array a to the called function.
The largest function header might look like:
float largest (float array [ ], int size)
The declaration of the formal argument array is made as follows:
float array [ ];
The pair of brackets informs the compiler that the argument array is as
array of numbers.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
29
30. PASSING VALUES TO FUNCTIONS
Three rules to pass an array to a function:
A. The function must be called by passing only the name of the
array.
B. In the function definition, the formal parameter must be an array
type; the size of the array does not need to be specified.
C. The function prototype must show that the argument is an array.
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
30
31. RECURSIVE FUNCTIONS
Recursion: the ability of a subprogram to call itself.
Each recursive solution has at least two cases
base case: the one to which we have an answer
general case: expresses the solution in terms of a call to itself with a smaller
version of the problem.
For example, the factorial of a number is defined as the number times the product of all
the numbers between itself and 0:
N! = N * (N 1)!
8/11/2018ANKUR SRIVASTAVA ASSISTANT PROFESSOR JETGI
31