SlideShare a Scribd company logo
Decision
control
statement
Iteration
statement
Transfer
statement
Decision Control statement:-
Decision
control
statement
Decision control statement disrupt or alter the
sequential execution of the statement of the program
depending on the test condition in program
Types of Decision control statement:-
1. If
statement
3. Switch
statement
4. Go To
statement
2. If else
statement
The If statement is a powerful
decision making statement
and is used to control the flow
of execution of statement.condition
Block of if
Next statement
STOP
FALSE
TRUE
main()
{
int a;
printf(“enter value of a”);
scanf(“%d”,&a);
if(a>25)
{
}
printf(“no.is greater than 25”);
printf(“n bye”);
}
condition
Block of if
Next statement
STOP
FALSE
TRUE
Block of else
If the condition is true the true block is execute
otherwise
False block is execute.
main()
{
int n,c;
printf(“n enter value of n”);
scanf(“%d”,&n);
c=n%2;
if(c==0)
printf(“no is even”);
else
printf(“no is odd”);
getch();
}
What Is Else If Ladder:
If we are having different - different test conditions with different - different statements,
then for these kind of programming we need else if ladder
Syntax Of Else If Leader:
---------------------------------------------------------------------
if(test_condition1)
{
statement 1;
}
else if(test_condition2)
{
statement 2;
}
else if(test_condition3)
{
statement 3;
}
else if(test_condition4)
{
statement 4;
}
else
{
}
void main ( )
{
int num = 10 ;
if ( num > 0 )
printf ("n Number is Positive");
else if ( num < 0 )
printf ("n Number is Negative");
else printf ("n Number is Zero");
}
Switch statement is a multi-way decision making statement which selects one
of the several alternative based on the value of integer variable or expression.
A switch statement tests the value of a variable and compares it with multiple
cases. Once the case match is found, a block of statements associated with that
particular case is executed.
Each case in a block of a switch has a different name/number which is referred to
as an identifier. The value provided by the user is compared with all the cases
inside the switch block until the match is found.
. Syntax :-
switch(expression)
{
case constant 1: statement;
break;
case constant 2: statement;
break;
default : statement;
}
Flow Chart Diagram of Switch Case
Following diagram illustrates how a case is selected in switch case:
#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
Example
Following program illustrates the use of switch:
#include <stdio.h>
int main() {
int language = 10;
switch (language) {
case 1:
printf("C#n");
break;
case 2:
printf("Cn");
break;
case 3:
printf("C++n");
break;
default:
printf("Other programming languagen");}}
Output:
Other programming language
Example:
main()
{
char choice;
printf(“enter any alphabet”);
scanf(“%d”,& choice);
switch(choice)
{
case ‘a’:
printf(“this is a vowel n”);
break;
case ‘e’ :
printf(“this is a vowel n”);
break;
case ‘i’ :
printf(“this is a vowel n”);
break;
case ‘o’ :
printf(“this is a vowel n”);
break;
case ‘u’ :
printf(“this is a vowel n”);
break;
default :
printf(“this is not a vowel”);
}
}
#include<stdio.h>
void main( )
{
int a, b, c, choice;
/* Printing the available options */
printf("n 1. Press 1 for addition");
printf("n 2. Press 2 for subtraction");
printf("n Enter your choice");
/* Taking users input */
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter 2 numbers");
scanf("%d%d", &a, &b);
c = a + b;
printf("%d", c);
break;
Example
case 2:
printf("Enter 2 numbers");
scanf("%d%d", &a, &b);
c = a - b;
printf("%d", c);
break;
default:
printf("you have passed a wrong key");
printf("n press any key to continue");
}
}
In switch case, the break statement is used to terminate the switch case.
Basically it is used to execute the statements of a single case statement. If no
break appears, the flow of control will fall through all the subsequent cases
until a break is reached or the closing curly brace ‘}’ is reached.
#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
case 2:
printf("Case2 ");
case 3:
printf("Case3 ");
case 4:
printf("Case4 ");
default:
printf("Default ");
}
return 0;
}
Output:
Case2 Case3 Case4 Default
CONDITIONAL OR TERNARY OPERATORS IN C:
Conditional operators return one value if condition is true and returns
another value is condition is false.
This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
In above example, if A is greater than 100, 0 is returned else 1 is
returned. This is equal to if else conditional statements.
Loop control statements in C are used to perform looping operations until
the given condition is true. Control comes out of the loop statements once
condition becomes false.
Sometimes we want some part of our code to be executed more than once.
We can either repeat the code in our program or use loops instead. It is
obvious that if for example we need to execute some part of code for a
hundred times it is not practical to repeat the code. Alternatively we can
use our repeating code inside a loop.
• Depending on the position of control statement in c,control structure may
be classified in
• Entry_ controlled loop (Top Tested)
In an entry controlled loop, a condition is checked before executing the body
of a loop. It is also called as a pre-checking loop.
Exit _controlled loop (Bottom Tested)
In an exit controlled loop, a condition is checked after executing the body of
a loop. It is also called as a post-checking loop.
False
Entry controlled loop exit controlled loop
Test
conditio
n ?
true
Body of the
loop
Body of the
loop
Test
conditio
n ?
• C language provides three constructs for
perfoming loop operations
• While statement
• Do statements
• For statements
While(test condition)
{
body of the loop
}
#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count);
count++;
}
return 0;
}
Output:
1 2 3 4
………………………
………………………
int sum=0;
int n=1;
while(n<=10)
{
sum=sum+n;
n=n+1;
}
printf(“sum=%dn”, sum);
…………………………………………..
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 ) {
printf("value of a: %dn", a);
a++;
}
return 0;
}
When the above code is compiled and executed, it
produces the following result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
A do while loop is similar to while loop with one exception that it
executes the statements inside the body of do-while before checking
the condition. On the other hand in the while loop, first the
condition is checked and then the statements in while loop are
executed. So you can say that if a condition is false at the first place
then the do while would run once, however the while loop would
not run at all.
Do statement
do
{
Body of the loop
}
While(test condition)
#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %dn", j);
j++;
}while (j<=3);
return 0;
}
Output:
Value of variable j is: 0
Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3
#include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%dn",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:
2
4
6
8
10
12
14
16
18
20
While vs do..while loop in C
Using while loop:
#include <stdio.h>
int main()
{
int i=0;
while(i==1)
{
printf("while vs do-while");
}
printf("Out of loop");
}
Output:
Out of loop
Same example using do-while loop
#include <stdio.h>
int main()
{
int i=0;
do
{
printf("while vs do-whilen");
}while(i==1);
printf("Out of loop");
}
Output:
while vs do-while
Out of loop
For statements
For(intialization;testcondition;icrement)
{
body of the loop
}
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number
of times.
Syntax
The syntax of a for loop in C programming language is −
for ( init; condition; increment )
{
statement(s);
}
Step 1: First initialization happens and the counter variable gets
initialized.
Step 2: In the second step the condition is checked, where the
counter variable is tested for the given condition, if the
condition returns true then the C statements inside the body of
for loop gets executed, if the condition returns false then the
for loop gets terminated and the control comes out of the loop.
Step 3: After successful execution of statements inside the
body of loop, the counter variable is incremented or
decremented, depending on the operation (++ or –).
Example of For loop
#include <stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%dn", i);
}
return 0;
}
Output:
1
2
3
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
Output
Enter a positive integer: 10
Sum = 55
C Program: 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;
}
Output
Enter a number: 2
2
4
6
8
10
12
14
16
18
20
Program to print natural numbers in reverse
#include <stdio.h>
int main()
{
int i, start;
/* Input start range from user */
printf("Enter starting value: ");
scanf("%d", &start);
/*
* Run loop from 'start' to 1 and
* decrement 1 in each iteration
*/
for(i=start; i>=1; i--)
{
printf("%dn", i);
}
return 0;
}
Program to print natural number in reverse in given range
#include <stdio.h>
int main()
{
int i, start, end;
/* Input start and end limit from user */
printf("Enter starting value: ");
scanf("%d", &start);
printf("Enter end value: ");
scanf("%d", &end);
/*
* Run loop from 'start' to 'end' and
* decrement by 1 in each iteration
*/
for(i=start; i>=end; i--)
{
printf("%dn", i);
}
return 0;
}
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("%dn", i);
}
for(num=10;num<20;)
{
//Statements
num++;
}
Nesting of for loop
For(i=0;i<n;i++)
{
………………………………
For(j=0;j<n-1;j++)
{
………………………………
}
}
C programming allows to use one loop inside another loop.
#include <stdio.h>
int main()
{
for (int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{
printf("%d, %dn",i ,j);
}
}
return 0;
}
Output:
0, 0
0, 1
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3
Skipping a part of loop
The continue statement is used inside loops. When a continue statement is
encountered inside a loop, control jumps to the beginning of the loop for
next iteration, skipping the execution of statements inside the body of loop
for the current iteration.
Eg:
While (test condition)
{
………………………..
If(…………)
Continue;
#include <stdio.h>
int main()
{
for (int j=0; j<=8; j++)
{
if (j==4)
{
/* The continue statement is encountered when
* the value of j is equal to 4.
*/
continue;
}
/* This print statement would not execute for the
* loop iteration where j ==4 because in that case
* this statement would be skipped.
*/
printf("%d ", j);
}
return 0;
}
Output:
0 1 2 3 5 6 7 8
Go To statement
A GO TO statement can cause program control to end up anywhere in the
program unconditionally.
Example :-
main()
{
int i=1;
up : printf(“Hello ToC”)
i++;
If (i<=5)
goto up
getch();
}
#include <stdio.h>
int main()
{
int sum=0;
for(int i = 0; i<=10; i++){
sum = sum+i;
if(i==5){
goto addition;
}
}
addition:
printf("%d", sum);
return 0;
}
Output:
15
Control Structures in C

More Related Content

What's hot

User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functionsAlisha Korpal
 
Function in c
Function in cFunction in c
Function in c
savitamhaske
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
C function presentation
C function presentationC function presentation
C function presentation
Touhidul Shawan
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
Prabhu Govind
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
mubashir farooq
 
user defined function
user defined functionuser defined function
user defined function
King Kavin Patel
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
Saket Pathak
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
Swapnil Yadav
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Functions in c
Functions in cFunctions in c
Functions in c
SunithaVesalpu
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Appili Vamsi Krishna
 

What's hot (20)

User defined functions
User defined functionsUser defined functions
User defined functions
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Function in c
Function in cFunction in c
Function in c
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C function presentation
C function presentationC function presentation
C function presentation
 
Pre defined Functions in C
Pre defined Functions in CPre defined Functions in C
Pre defined Functions in C
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
user defined function
user defined functionuser defined function
user defined function
 
Functions in C
Functions in CFunctions in C
Functions in C
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Function in c
Function in cFunction in c
Function in c
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
 

Similar to Control Structures in C

C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Chandrakant Divate
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Chandrakant Divate
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
REHAN IJAZ
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
Mehul Desai
 
What is c
What is cWhat is c
What is c
Nitesh Saitwal
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
Munazza-Mah-Jabeen
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
3. control statements
3. control statements3. control statements
3. control statements
amar kakde
 
Session 3
Session 3Session 3

Similar to Control Structures in C (20)

C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
What is c
What is cWhat is c
What is c
 
Decision Making and Branching
Decision Making and BranchingDecision Making and Branching
Decision Making and Branching
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
3. control statements
3. control statements3. control statements
3. control statements
 
Session 3
Session 3Session 3
Session 3
 

Recently uploaded

Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Natan Silnitsky
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 

Control Structures in C

  • 1.
  • 3. Decision Control statement:- Decision control statement Decision control statement disrupt or alter the sequential execution of the statement of the program depending on the test condition in program Types of Decision control statement:- 1. If statement 3. Switch statement 4. Go To statement 2. If else statement
  • 4. The If statement is a powerful decision making statement and is used to control the flow of execution of statement.condition Block of if Next statement STOP FALSE TRUE
  • 5. main() { int a; printf(“enter value of a”); scanf(“%d”,&a); if(a>25) { } printf(“no.is greater than 25”); printf(“n bye”); }
  • 6. condition Block of if Next statement STOP FALSE TRUE Block of else If the condition is true the true block is execute otherwise False block is execute.
  • 7. main() { int n,c; printf(“n enter value of n”); scanf(“%d”,&n); c=n%2; if(c==0) printf(“no is even”); else printf(“no is odd”); getch(); }
  • 8. What Is Else If Ladder: If we are having different - different test conditions with different - different statements, then for these kind of programming we need else if ladder Syntax Of Else If Leader: --------------------------------------------------------------------- if(test_condition1) { statement 1; } else if(test_condition2) { statement 2; } else if(test_condition3) { statement 3; } else if(test_condition4) { statement 4; } else { }
  • 9. void main ( ) { int num = 10 ; if ( num > 0 ) printf ("n Number is Positive"); else if ( num < 0 ) printf ("n Number is Negative"); else printf ("n Number is Zero"); }
  • 10. Switch statement is a multi-way decision making statement which selects one of the several alternative based on the value of integer variable or expression. A switch statement tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. . Syntax :- switch(expression) { case constant 1: statement; break; case constant 2: statement; break; default : statement; }
  • 11. Flow Chart Diagram of Switch Case Following diagram illustrates how a case is selected in switch case:
  • 12. #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 Example Following program illustrates the use of switch:
  • 13. #include <stdio.h> int main() { int language = 10; switch (language) { case 1: printf("C#n"); break; case 2: printf("Cn"); break; case 3: printf("C++n"); break; default: printf("Other programming languagen");}} Output: Other programming language Example:
  • 14. main() { char choice; printf(“enter any alphabet”); scanf(“%d”,& choice); switch(choice) { case ‘a’: printf(“this is a vowel n”); break; case ‘e’ : printf(“this is a vowel n”); break; case ‘i’ : printf(“this is a vowel n”); break; case ‘o’ : printf(“this is a vowel n”); break; case ‘u’ : printf(“this is a vowel n”); break; default : printf(“this is not a vowel”); } }
  • 15. #include<stdio.h> void main( ) { int a, b, c, choice; /* Printing the available options */ printf("n 1. Press 1 for addition"); printf("n 2. Press 2 for subtraction"); printf("n Enter your choice"); /* Taking users input */ scanf("%d", &choice); switch(choice) { case 1: printf("Enter 2 numbers"); scanf("%d%d", &a, &b); c = a + b; printf("%d", c); break; Example
  • 16. case 2: printf("Enter 2 numbers"); scanf("%d%d", &a, &b); c = a - b; printf("%d", c); break; default: printf("you have passed a wrong key"); printf("n press any key to continue"); } }
  • 17. In switch case, the break statement is used to terminate the switch case. Basically it is used to execute the statements of a single case statement. If no break appears, the flow of control will fall through all the subsequent cases until a break is reached or the closing curly brace ‘}’ is reached. #include <stdio.h> int main() { int i=2; switch (i) { case 1: printf("Case1 "); case 2: printf("Case2 "); case 3: printf("Case3 "); case 4: printf("Case4 "); default: printf("Default "); } return 0; } Output: Case2 Case3 Case4 Default
  • 18. CONDITIONAL OR TERNARY OPERATORS IN C: Conditional operators return one value if condition is true and returns another value is condition is false. This operator is also called as ternary operator. Syntax : (Condition? true_value: false_value); Example : (A > 100 ? 0 : 1); In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.
  • 19. Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false. Sometimes we want some part of our code to be executed more than once. We can either repeat the code in our program or use loops instead. It is obvious that if for example we need to execute some part of code for a hundred times it is not practical to repeat the code. Alternatively we can use our repeating code inside a loop.
  • 20. • Depending on the position of control statement in c,control structure may be classified in • Entry_ controlled loop (Top Tested) In an entry controlled loop, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop. Exit _controlled loop (Bottom Tested) In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.
  • 21. False Entry controlled loop exit controlled loop Test conditio n ? true Body of the loop Body of the loop Test conditio n ?
  • 22. • C language provides three constructs for perfoming loop operations • While statement • Do statements • For statements
  • 23.
  • 25. #include <stdio.h> int main() { int count=1; while (count <= 4) { printf("%d ", count); count++; } return 0; } Output: 1 2 3 4
  • 27. #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; } return 0; } When the above code is compiled and executed, it produces the following result − value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 28. A do while loop is similar to while loop with one exception that it executes the statements inside the body of do-while before checking the condition. On the other hand in the while loop, first the condition is checked and then the statements in while loop are executed. So you can say that if a condition is false at the first place then the do while would run once, however the while loop would not run at all.
  • 29. Do statement do { Body of the loop } While(test condition)
  • 30. #include <stdio.h> int main() { int j=0; do { printf("Value of variable j is: %dn", j); j++; }while (j<=3); return 0; } Output: Value of variable j is: 0 Value of variable j is: 1 Value of variable j is: 2 Value of variable j is: 3
  • 31. #include<stdio.h> #include<conio.h> int main() { int num=1; //initializing the variable do //do-while loop { printf("%dn",2*num); num++; //incrementing operation }while(num<=10); return 0; } Output: 2 4 6 8 10 12 14 16 18 20
  • 32. While vs do..while loop in C Using while loop: #include <stdio.h> int main() { int i=0; while(i==1) { printf("while vs do-while"); } printf("Out of loop"); } Output: Out of loop
  • 33. Same example using do-while loop #include <stdio.h> int main() { int i=0; do { printf("while vs do-whilen"); }while(i==1); printf("Out of loop"); } Output: while vs do-while Out of loop
  • 35. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax The syntax of a for loop in C programming language is − for ( init; condition; increment ) { statement(s); }
  • 36.
  • 37. Step 1: First initialization happens and the counter variable gets initialized. Step 2: In the second step the condition is checked, where the counter variable is tested for the given condition, if the condition returns true then the C statements inside the body of for loop gets executed, if the condition returns false then the for loop gets terminated and the control comes out of the loop. Step 3: After successful execution of statements inside the body of loop, the counter variable is incremented or decremented, depending on the operation (++ or –).
  • 38. Example of For loop #include <stdio.h> int main() { int i; for (i=1; i<=3; i++) { printf("%dn", i); } return 0; } Output: 1 2 3
  • 39. // Program to calculate the sum of first n natural numbers // Positive integers 1,2,3...n are known as natural numbers #include <stdio.h> int main() { int num, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); // for loop terminates when num is less than count for(count = 1; count <= num; ++count) { sum += count; } printf("Sum = %d", sum); return 0; Output Enter a positive integer: 10 Sum = 55
  • 40. C Program: 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; } Output Enter a number: 2 2 4 6 8 10 12 14 16 18 20
  • 41. Program to print natural numbers in reverse #include <stdio.h> int main() { int i, start; /* Input start range from user */ printf("Enter starting value: "); scanf("%d", &start); /* * Run loop from 'start' to 1 and * decrement 1 in each iteration */ for(i=start; i>=1; i--) { printf("%dn", i); } return 0; }
  • 42. Program to print natural number in reverse in given range #include <stdio.h> int main() { int i, start, end; /* Input start and end limit from user */ printf("Enter starting value: "); scanf("%d", &start); printf("Enter end value: "); scanf("%d", &end); /* * Run loop from 'start' to 'end' and * decrement by 1 in each iteration */ for(i=start; i>=end; i--) { printf("%dn", i); } return 0; }
  • 43. 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("%dn", i); } for(num=10;num<20;) { //Statements num++; }
  • 44. Nesting of for loop For(i=0;i<n;i++) { ……………………………… For(j=0;j<n-1;j++) { ……………………………… } } C programming allows to use one loop inside another loop.
  • 45. #include <stdio.h> int main() { for (int i=0; i<2; i++) { for (int j=0; j<4; j++) { printf("%d, %dn",i ,j); } } return 0; } Output: 0, 0 0, 1 0, 2 0, 3 1, 0 1, 1 1, 2 1, 3
  • 46. Skipping a part of loop The continue statement is used inside loops. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration, skipping the execution of statements inside the body of loop for the current iteration. Eg: While (test condition) { ……………………….. If(…………) Continue;
  • 47. #include <stdio.h> int main() { for (int j=0; j<=8; j++) { if (j==4) { /* The continue statement is encountered when * the value of j is equal to 4. */ continue; } /* This print statement would not execute for the * loop iteration where j ==4 because in that case * this statement would be skipped. */ printf("%d ", j); } return 0; } Output: 0 1 2 3 5 6 7 8
  • 48. Go To statement A GO TO statement can cause program control to end up anywhere in the program unconditionally. Example :- main() { int i=1; up : printf(“Hello ToC”) i++; If (i<=5) goto up getch(); }
  • 49. #include <stdio.h> int main() { int sum=0; for(int i = 0; i<=10; i++){ sum = sum+i; if(i==5){ goto addition; } } addition: printf("%d", sum); return 0; } Output: 15