2. 2
Conditional operators in details
Decision making structures like IF, IF-ELSE
AND SWITCH
Loop control structures like FOR, WHILE, DO-
WHILE
Implementation of break and continue.
Nested structures.
Unconditional branching (GOTO statement).
3. 3
INTRODUCTION:-
Normally the statement of a program are
executed from first to last.
Sometimes it is necessary to alter the sequence of
execution of statements.
Based on certain conditions or we may require
some statements to be executed repeatedly until
some condition is satisfied.
This involves decision making and looping.
4. 4
THREE MECHANISM ARE:-
if statement:Many program we require testing
of some condition at some point in the program
and take action.
Syntax: if(condition)
{
Statements;
}
Expressio
n
statements
F
T
5. 5
if-else statement:Many program require testing of
some condition at some point in the program and
selecting one of the alternative at some point and
selecting one of the alternative path depending upon
the result is known as branching.
General Form is:
if(expression)
statement1 ;
else
statement 2;
Expressi
on
statement1 statement2
T F
6. 6
Ex. Check given number is even or odd. Example of if-else
statement
Solution:
#include<stdio.h>
main( )
{
int no;
printf(“n Enter any number”);
scanf(“%d”,&no);
if (no %2==0)
{
printf(“ number=%d is even number”,no);
}
else
{
printf(“n number =%d is odd number”,no);
}
}//main
7. 7
NESTED IF–ELSE STATEMENT
NESTED “IF-ELSE” STATEMENTS:-
Nested “if-else” statements, the if or the else can
contain another if or if-else statement. This provides a
programmer with a lot of flexibility in programming.
Nesting could take one of several form as illustrated as
below:
1] if statement inside else:-
Syntax: if(expression)
Statement1;
else
if(expression2)
Statement2;
9. 9
4] if-else statement inside if-else:-
Syntax:-if(expression1)
{if(expression2)
statement1;
else
statement 2;}
else
if(expression3)
statement3;
else
statement 4;
Eg Write a c program to accept 3 no from user and check greater no.
#include<stdio.h>
#include<conio.h>
void main()
{
int no1, no2, no3;
clrscr();
printf(“n Ennter any 3 nos:”);
scanf(“%d%d%d”,&no1,&no2,&no3);
if(no1>no2)
if(no1>no3)
printf(“n no1 is greater number”);
10. 10
5]else-if ladder:-
If there is if-else statement nested in each else
of an if-else construct, that is called as else-if ladder.
Syntax: if(expression1)
statement 1;
else
if(expression 2)
statement2;
else
if(expression3)
statement 3;
else
statement 4;
11. 11
Conditional Branching switch Statement:
Whenever we want to make decision among
multiple alternatives nested else-if statement can
be used, however structure become very
complicated and code become difficult to read and
trace. For this reason C has a build-in multi
branch decision statement called switch.
case 1 value 1
switch(expr) case 2 value 2
case n value n
default
12. 12
RULES:-
Syntax:
switch(expression)
{
case value1: Statements;
break;
case value 2: Statements;
break;
….
case value n: Statements;
break;
default : Statements;
}
The expression enclosed within parenthesis is successively compare
against the constant expression(value)in each case. They called case
labeled and must be end with colon( : ).
The statement in each case may contain zero or more statements. If there
are multiple statement for a case they need not be enclosed in bracket.
All case expression must be different.
The case default is executed if non of the other case match.
13. 13
NESTED SWITCH STATEMENT:-
It is possible to have a switch statement
as a part of statement is another switch
statement then it is called as Nested
switch statement.
Even if the case constant of the inner and
outer switch contain common values there
is not conflict.(mix with each other)
14. 14
Ex.
#include<stdio.h>
main()
{
int x,y;
printf(“n enter 2 binary values”);
scanf(“%d%d”,&x,&y);
switch(x)
{
case 0:printf(“n Invalid Values”);
break;
case 1: switch(y)
{
case 0: printf(“n Values are 1 & 0”);
break;
case 1:printf(“n Values are 1 & 1”);
break;
}// inner switch
break;
} //outer switch
}//main
15. 15
LOOP CONTROL STRUCTURE:-
A block of program statements that is executed
repeatedly is called a loop. The repetition is done
until some condition for termination of the loop is
satisfied .
A loop structure essentially contains a test
condition.
1]A test condition determines the number of times
the loop body is executed.
2]Loop statements- If the test condition evaluate to
true then statement within the loop body get
executed otherwise control transfer outside the
loop i.e loop termination.
initially contains a test condition.
16. 16
1) Top –Tested Loop:
An Entry Controlled loop , the condition is evaluated before the loop
body is executed. Ex.while,for
Test
Condition
Loop Body Statement
False
True
17. 17
2) Bottom tested loop(exit controlled loop).
In exit controlled loop, the condition is tested after
the loop body executed.
Ex.do-while
Loop Body
Test
Condition
True
False
18. 18
ITERATION PROCEDURE:-
The iteration procedure takes place in 4 steps
1. Initializing the loop control variable.
2. Execution of loop statement.
3. Changing the value of the control variable.
4. Testing the condition.
5. The C language provides 3 loops:-
6. 1) while
7. 2) do-while
8. 3) for
19. 19
LOOPS:-
C LANGUAGE PROVIDES 3 LOOP
STRUCTURES:-
1) While: It is often used when the no. of time the
loop is to be executed is not known in advanced
but depends on the test condition.ENTRY
CONTOL LOOP
2) do-while: do-while loop is a bottom tested or
exit controlled loop i.e it evaluates the condition
after the execution of statement in it construct.
This means that the statements within the loop
are executed at least once if the condition is false.
3) for: for loop is very flexible, powerful and most
commonly used loop in C. It is useful when the
member of repetition is known in advance. This is
a top-tested loop similarly to the while loop.
20. 20
1] While:-
It is often used when the no. of time the
loop is to be executed is not known in
advanced but depends on the test condition.
Syntax:- while(expression)
{
statements;
}
That expression is a test condition and can
be any valid c expression. The statement can
be a single or set of statement.
21. 21
2) The do-while loop
Iterative statement means loop control statement.
do-while loop, is a bottom-tested or exit control
loop i.e. It evaluates the condition after the
execution of statements in its.
This means that the statements within the loop
are executed at-least once, when condition is false.
Syntax:
do
{
statements;
}while(expression);
22. 22
3] for loop:-
For loop is very flexible, powerful and most
commonly used loop in C . It is useful when the
member of repetition is known in advance.
This is a top-tested loop similarly to the while loop.
Syntax:-
for(exp1;exp2;exp3) ex. for( i=1;i<=10;i++)
{
statement;
}
Where, expr1 is to used to initialization of expression.
expr2 is used to test condition .
expr3 is used to update expression.
these 3 expression is separated by ‘ ; ’.
23. 23
Eg. Write a program to print first 10 no’s
using for loop
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i=i+1)
{
printf(“%d”,i);
}
}
24. 24
Print table for the given number using C for loop
#include<stdio.h>
int main()
{
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d n",(number*i));
}
return 0;
}
28. 28
NESTED LOOP:-
Loop can also be nested ,Nesting of loops means a loop i.e contain within another loop. Any loop can be nested
within any other loop.
Eg:
1) while(expr1)
{
while(expr2)
{
loop body of inner while
}
}
2) while(expr1)
{
for( )
{
loop body of for
}
}
3) for( )
{
…….
for( )
{
statements;
}
}
29. 29
#include <stdio.h>
int main()
{
int n;// variable declaration
printf("Enter the value of n :");
// Displaying the n tables.
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%dt",(i*j)); // printing the value.
}
printf("n");
}
30. 30
#include <stdio.h>
int main()
{
int rows; // variable declaration
int columns; // variable declaration
int k=1; // variable initialization
printf("Enter the number of rows :"); // input the number of rows.
scanf("%d",&rows);
printf("nEnter the number of columns :"); // input the number of columns.
scanf("%d",&columns);
int a[rows][columns]; //2d array declaration
int i=1;
while(i<=rows) // outer loop
{
int j=1;
while(j<=columns) // inner loop
{
printf("%dt",k); // printing the value of k.
k++; // increment counter
j++;
}
i++;
printf("n");
}
}
32. 32
JUMP STATEMENT:-
In many cases we want to stop the execution loop
before it ends or transfer the control to some
other part of a program. This can be done using
jump statement.
C supports 4 jump statements
1. break
2. continue
3. return
4. Goto
5. exit
33. 33
1] break:-
Sometime it is require to exit a loop as soon as a
certain condition is made.
To skip all subsequent statement of loop body and to come
out of the loop.
When the break statement is encounter inside a loop, the
loop is immediately terminate subsequent statement are
skipped and program control transfer at the next
statement of the following loop.
36. 36
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
38. 38
2]continue:-
Continue statement is used to transfer control
to the beginning of the loop.
It remaining statement and it forces the next
interaction of the loop to takes place as usual.
Syntax:- continue;
41. 41
In this program the loop generates 1 to 10 values of the variable "i".
Whenever it is an even number, the next iteration starts, skipping the printf
statement. Only the odd numbers are printed.
#include <stdio.h>
int main(){
int i = 0;
while (i < 10){
i++;
if(i%2 == 0)
continue;
printf("i: %dn", i);
}
}
Output
i: 1
i: 3
i: 5
i: 7
i: 9
42. 42
3] goto Statement:-
goto statement is an unconditional jump
statement.
Goto statement is use to alter the normal sequence of
program execution by unconditionally transferring by
control some other part of program.
Syntax: goto label;
The statement where control has to be transfer is
identified by the label.
A label is valid C identifier.
The label followed by colon “:” .
The label can attached to any statement in the same
function as the goto.
The label does not has to be declared like other identifier.
45. 45
#include <stdio.h>
int main (){
int n = 0;
if (n == 0)
goto end;
printf("The number is: %d", n);
end:
printf ("End of program");
return 0;
}
46. 46
#include <stdio.h>
int main (){
START:
printf("Hello World n");
printf("How are you? n");
goto START;
return 0;
}
Output:
Hello World
How are you?
.......
.......
The program prints the two strings continuously until
forcibly stopped.
47. 47
4] exit():-
The exit(0) function causes indicate
termination of the entire program .the exit
function is called with an arguments’0’ to
indicate that termination is normal.
Eg. Invalid password enter that time use exit(0).