SlideShare a Scribd company logo
CONTROL STRUCTURES IN
‘C’
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
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
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
 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
 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
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;
8
 if- else statement inside if:-
Syntax:-if(expression 1)
 {
if(expression 2)
statement 1;
else
statement 2;
}
else
statements 3;

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
 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
 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
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
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
 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
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
 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
 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
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
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
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
 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
 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
 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
 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;
 }
25
 #include <stdio.h>
 int main()
 {
 int i,j,k;
 for(i=0,j=0,k=0;i<4,k<8,j<10;i++)
 {
 printf("%d %d %dn",i,j,k);
 j+=2;
 k+=3;
 }
 }
0 0 0
1 2 3
2 4 6
3 6 9
4 8 12
26
 #include <stdio.h>
 int main()
 {
 int i;
 for(i=0;;i++)
 {
 printf("%d",i);
 }
 }
27
1. #include<stdio.h>
2. void main ()
3. {
4. int i=0,j=2;
5. for(i = 0;i<5;i++,j=j+2)
6. {
7. printf("%d %dn",i,j);
8. }
9. }
0 2 1
4 2 6
3 8 4
10
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
 #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
 #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");
 }
 }
31
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

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.
34
35
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;
}
37
 #include <stdio.h>

 int main(){

 /* local variable definition */
 char ch = 'm';
 printf("Time code: %cnn", ch);

 switch(ch) {

 case 'm':
 printf("Good Morning n");
 break;

 case 'a':
 printf("Good Afternoon n");
 break;

 case 'e':
 printf("Good Evening n");
 break;

 default:
 printf("Hello");
 }
 }
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;
39
 while (expr){
 . . .
 . . .
 if (condition)
 continue;
 . . .
 }
40
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
 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.
43
44
 Ex.:
 for(….)
 {
 for(…)
 { while( ..)
 {
 …..
 if( )
 goto out;
 …..
 }
 }
 }
 out:
 ….
 ….
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
 #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
 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).
48
49
THANK YOU
Ad

Recommended

C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
C Programming Control Structures(if,if-else)
C Programming Control Structures(if,if-else)
poonambhagat36
 
Control Structures in C
Control Structures in C
sana shaikh
 
Session 3
Session 3
Shailendra Mathur
 
Control statments in c
Control statments in c
CGC Technical campus,Mohali
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
Programming in Arduino (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
Rai University
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
handling input output and control statements
Rai University
 
Loops in c language
Loops in c language
tanmaymodi4
 
Loops in c language
Loops in c language
Tanmay Modi
 
Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
Ch3 repetition
Ch3 repetition
Hattori Sidek
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Decision making in C(2020-2021) statements
Decision making in C(2020-2021) statements
BalaKrishnan466
 
control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
C Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
control-statements, control-statements, control statement
control-statements, control-statements, control statement
crrpavankumar
 
plant and animal nutrition..........pptx
plant and animal nutrition..........pptx
mayflorgalleno
 
Gas Exchange in Insects and structures 01
Gas Exchange in Insects and structures 01
PhoebeAkinyi1
 

More Related Content

Similar to C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH) (20)

Programming in Arduino (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
Rai University
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
handling input output and control statements
Rai University
 
Loops in c language
Loops in c language
tanmaymodi4
 
Loops in c language
Loops in c language
Tanmay Modi
 
Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
Ch3 repetition
Ch3 repetition
Hattori Sidek
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Decision making in C(2020-2021) statements
Decision making in C(2020-2021) statements
BalaKrishnan466
 
control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
C Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
control-statements, control-statements, control statement
control-statements, control-statements, control statement
crrpavankumar
 
1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
Rai University
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptx
LECO9
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
handling input output and control statements
Rai University
 
Loops in c language
Loops in c language
tanmaymodi4
 
Loops in c language
Loops in c language
Tanmay Modi
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Decision making in C(2020-2021) statements
Decision making in C(2020-2021) statements
BalaKrishnan466
 
control_structures_c_language_regarding how to represent the loop in language...
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
C Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
control-statements, control-statements, control statement
control-statements, control-statements, control statement
crrpavankumar
 

Recently uploaded (20)

plant and animal nutrition..........pptx
plant and animal nutrition..........pptx
mayflorgalleno
 
Gas Exchange in Insects and structures 01
Gas Exchange in Insects and structures 01
PhoebeAkinyi1
 
Paired Sketching of Distributed User Interfaces:Workflow, Protocol, Software ...
Paired Sketching of Distributed User Interfaces:Workflow, Protocol, Software ...
Jean Vanderdonckt
 
feismo.com-dll-for-science-11-4th-pr_9ffe2eea16c7798a3e81949d38e20447.pdf
feismo.com-dll-for-science-11-4th-pr_9ffe2eea16c7798a3e81949d38e20447.pdf
RODULFOVPAQUINGAN
 
Cancer
Cancer
Vartika
 
Enzyme Kinetics_Lecture 8.5.2025 Enzymology.pdf
Enzyme Kinetics_Lecture 8.5.2025 Enzymology.pdf
ayeshaalibukhari125
 
SULFUR PEARL OF NAMIBIA - Thiomargarita namibiensis
SULFUR PEARL OF NAMIBIA - Thiomargarita namibiensis
aparnamp966
 
Sujay Rao Mandavilli public profile June 2025.pdf
Sujay Rao Mandavilli public profile June 2025.pdf
Sujay Rao Mandavilli
 
MOLD -GENERAL CHARACTERISTICS AND CLASSIFICATION
MOLD -GENERAL CHARACTERISTICS AND CLASSIFICATION
aparnamp966
 
STAPHYLOCOCCAL AND STREPTOCOCCAL INFECTIONS 2.ppt
STAPHYLOCOCCAL AND STREPTOCOCCAL INFECTIONS 2.ppt
pakranti27
 
Single-Cell Multi-Omics in Neurodegeneration p1.pptx
Single-Cell Multi-Omics in Neurodegeneration p1.pptx
KanakChaudhary10
 
What is Research Grade 7/ Research I.pptx
What is Research Grade 7/ Research I.pptx
delapenamhey144
 
Study of Appropriate Information Combination in Image-based Obfuscated Malwar...
Study of Appropriate Information Combination in Image-based Obfuscated Malwar...
takahashi34
 
An Analysis Of The Pearl Short Story By John Steinbeck
An Analysis Of The Pearl Short Story By John Steinbeck
BillyDarmawan3
 
Fake Science: Where it comes from and how to avoid beign part of it
Fake Science: Where it comes from and how to avoid beign part of it
Leonid Schneider
 
Properties of Gases siwhdhadpaldndn.pptx
Properties of Gases siwhdhadpaldndn.pptx
CatherineJadeBurce
 
Science 10 1.3 Mountain Belts in the Philippines.pptx
Science 10 1.3 Mountain Belts in the Philippines.pptx
ClaireMangundayao1
 
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
ShrayasiRoy2
 
Herbal Excipients: Natural Colorants & Perfumery Agents
Herbal Excipients: Natural Colorants & Perfumery Agents
Seacom Skills University
 
GBSN__Unit 2 - Control of Microorganisms
GBSN__Unit 2 - Control of Microorganisms
Areesha Ahmad
 
plant and animal nutrition..........pptx
plant and animal nutrition..........pptx
mayflorgalleno
 
Gas Exchange in Insects and structures 01
Gas Exchange in Insects and structures 01
PhoebeAkinyi1
 
Paired Sketching of Distributed User Interfaces:Workflow, Protocol, Software ...
Paired Sketching of Distributed User Interfaces:Workflow, Protocol, Software ...
Jean Vanderdonckt
 
feismo.com-dll-for-science-11-4th-pr_9ffe2eea16c7798a3e81949d38e20447.pdf
feismo.com-dll-for-science-11-4th-pr_9ffe2eea16c7798a3e81949d38e20447.pdf
RODULFOVPAQUINGAN
 
Enzyme Kinetics_Lecture 8.5.2025 Enzymology.pdf
Enzyme Kinetics_Lecture 8.5.2025 Enzymology.pdf
ayeshaalibukhari125
 
SULFUR PEARL OF NAMIBIA - Thiomargarita namibiensis
SULFUR PEARL OF NAMIBIA - Thiomargarita namibiensis
aparnamp966
 
Sujay Rao Mandavilli public profile June 2025.pdf
Sujay Rao Mandavilli public profile June 2025.pdf
Sujay Rao Mandavilli
 
MOLD -GENERAL CHARACTERISTICS AND CLASSIFICATION
MOLD -GENERAL CHARACTERISTICS AND CLASSIFICATION
aparnamp966
 
STAPHYLOCOCCAL AND STREPTOCOCCAL INFECTIONS 2.ppt
STAPHYLOCOCCAL AND STREPTOCOCCAL INFECTIONS 2.ppt
pakranti27
 
Single-Cell Multi-Omics in Neurodegeneration p1.pptx
Single-Cell Multi-Omics in Neurodegeneration p1.pptx
KanakChaudhary10
 
What is Research Grade 7/ Research I.pptx
What is Research Grade 7/ Research I.pptx
delapenamhey144
 
Study of Appropriate Information Combination in Image-based Obfuscated Malwar...
Study of Appropriate Information Combination in Image-based Obfuscated Malwar...
takahashi34
 
An Analysis Of The Pearl Short Story By John Steinbeck
An Analysis Of The Pearl Short Story By John Steinbeck
BillyDarmawan3
 
Fake Science: Where it comes from and how to avoid beign part of it
Fake Science: Where it comes from and how to avoid beign part of it
Leonid Schneider
 
Properties of Gases siwhdhadpaldndn.pptx
Properties of Gases siwhdhadpaldndn.pptx
CatherineJadeBurce
 
Science 10 1.3 Mountain Belts in the Philippines.pptx
Science 10 1.3 Mountain Belts in the Philippines.pptx
ClaireMangundayao1
 
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
How Psychology Can Power Product Decisions: A Human-Centered Blueprint- Shray...
ShrayasiRoy2
 
Herbal Excipients: Natural Colorants & Perfumery Agents
Herbal Excipients: Natural Colorants & Perfumery Agents
Seacom Skills University
 
GBSN__Unit 2 - Control of Microorganisms
GBSN__Unit 2 - Control of Microorganisms
Areesha Ahmad
 
Ad

C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)

  • 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;
  • 8. 8  if- else statement inside if:- Syntax:-if(expression 1)  { if(expression 2) statement 1; else statement 2; } else statements 3; 
  • 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;  }
  • 25. 25  #include <stdio.h>  int main()  {  int i,j,k;  for(i=0,j=0,k=0;i<4,k<8,j<10;i++)  {  printf("%d %d %dn",i,j,k);  j+=2;  k+=3;  }  } 0 0 0 1 2 3 2 4 6 3 6 9 4 8 12
  • 26. 26  #include <stdio.h>  int main()  {  int i;  for(i=0;;i++)  {  printf("%d",i);  }  }
  • 27. 27 1. #include<stdio.h> 2. void main () 3. { 4. int i=0,j=2; 5. for(i = 0;i<5;i++,j=j+2) 6. { 7. printf("%d %dn",i,j); 8. } 9. } 0 2 1 4 2 6 3 8 4 10
  • 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");  }  }
  • 31. 31
  • 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.
  • 34. 34
  • 35. 35
  • 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; }
  • 37. 37  #include <stdio.h>   int main(){   /* local variable definition */  char ch = 'm';  printf("Time code: %cnn", ch);   switch(ch) {   case 'm':  printf("Good Morning n");  break;   case 'a':  printf("Good Afternoon n");  break;   case 'e':  printf("Good Evening n");  break;   default:  printf("Hello");  }  }
  • 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;
  • 39. 39  while (expr){  . . .  . . .  if (condition)  continue;  . . .  }
  • 40. 40
  • 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.
  • 43. 43
  • 44. 44  Ex.:  for(….)  {  for(…)  { while( ..)  {  …..  if( )  goto out;  …..  }  }  }  out:  ….  ….
  • 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).
  • 48. 48