SlideShare a Scribd company logo
Control Structure in C
Made By: Komal Kotak
C.K.Pithawala College of Engineering and Technology
C Programming Definition
C is a high-level and general-
purpose programming language that is ideal for developing
firmware or portable applications. Originally intended for
writing system software, C was developed at Bell Labs by
Dennis Ritchie for the Unix Operating System (OS) in the
early 1970s
Conditions in C Programming
o C executes program sequentially.
o Sometimes a program requires checking of certain
conditions in program execution.
o C provides various key condition statements to check
conditions and execute statements according conditional
criteria.
o This statements are called decision making statements and
control statements.
Control Structure in C
Decision making structures require that the
programmer specifies one or more conditions to be
evaluated or tested by the program, along with a
statement or statements to be executed if the
condition is determined to be true, and optionally,
other statements to be executed if the condition is
determined to be false.
Show here is the general form of a typical decision
making structure found in most of the
programming languages −
if-else statement (1)
The if-else statement can exist in two
forms: with or without the else. The two
forms are:
if(expression)
statement
or
if(expression)
statement1
else
statement2
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Example: Odd Or Even No. Program
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
if-else statement (2)
Output
Enter an integer: 7
7 is an odd integer.
Nested if-else statement (1)
o The if...else statement
executes two different codes
depending upon whether the
test expression is true or false.
Sometimes, a choice has to be
made from more than 2
possibilities.
o The nested if-else statement
allows you to check for
multiple test expressions and
execute different codes for
more than two conditions.
Nested if-else statement (2)
Example: Program to relate two integers using =, > or <
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if(number1 == number2)
{
printf("Result: %d = %d",number1,number2); }
else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2) }
else
{
printf("Result: %d < %d",number1, number2); }
return 0; }
Output
Enter two integers: 12
23
Result: 12 < 23
C Programming for Loop
Loops are used in programming to repeat a specific block until some
end condition is met.There are three loops in C programming:
1. for loop
2. while loop
3. do...while loop
For Loop (1)
o The initialization statement is executed only
once.
o Then, the test expression is evaluated. If the test
expression is false (0), for loop is terminated. But
if the test expression is true (nonzero), codes
inside the body of for loop is executed and the
update expression is updated.
o This process repeats until the test expression is
false.
o The for loop is commonly used when the number
of iterations is known.
For Loop (2)
Example: Program to calculate the sum of first n natural numbers
#include <stdio.h>
int main()
{
int num, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(i = 1; i <= num; i++)
{
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter a positive integer: 10
Sum = 55
while Loop (1)
o The while loop evaluates the test expression.
o If the test expression is true (nonzero), codes inside the body
of while loop is evaluated. The test expression is evaluated
again.The process goes on until the test expression is false.
o When the test expression is false, the while loop is
terminated.
o The syntax of a while loop is:
while (testExpression)
{
//codes
}
o where, testExpression checks the condition is true or false
before each loop.
while Loop (2)
Example: Program to find factorial of a number
#include <stdio.h>
int main()
{
int number;
int factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
while (number > 0)
{
factorial *= number;
--number;
}
printf("Factorial= %d", factorial);
return 0; }
Output
Enter an integer: 5
Factorial = 120
do-while Loop (1)
o The do..while loop is similar to the while loop with one important
difference.The body of do...while loop is executed once, before
checking the test expression. Hence, the do...while loop is executed at
least once.
o do...while loop Syntax
do
{
// codes
}
while (testExpression);
o The code block (loop body) inside the braces is executed once.
o Then, the test expression is evaluated. If the test expression is true,
the loop body is executed again.This process goes on until the test
expression is evaluated to 0 (false).
o When the test expression is false (nonzero), the do...while loop is
terminated.
do-while Loop (2)
Example: Program to add numbers until user enters zero
#include <stdio.h>
int main()
{
float number, sum = 0;
do
{
printf("Enter a number: ");
scanf("%f", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %f",sum);
return 0;
}
Output
Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
break Statement (1)
o It is sometimes desirable to skip some statements inside the loop or terminate
the loop immediately without checking the test expression.
o In such cases, break and continue statements are used.
o The break statement terminates the loop (for, while and do...while loop)
immediately when it is encountered.The break statement is used with decision
making statement such as if...else.
o Syntax of break statement
break;
break Statement (2)
continue Statement
o The continue statement skips
some statements inside the loop.
The continue statement is used
with decision making statement
such as if...else.
o Syntax of continue Statement
continue;
Switch case Statement (1)
o The switch case statement evaluates the value of an
expression and a block of code is selected on the basis
of that evaluated expression.
o Each case refers back to the original expression.
o The data type that can be used in switch expression is
integer type only.
o Each case has a break statement.
o The switch case takes decision on the basis of
equality.
Switch case Statement (2)
int main() {
char op;
int num1,num2;
printf("Enter an operator (+,-,*,/):");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%d %d",&num1, &num2);
switch(op)
{
case '+':
printf("%d + %d =
%d",num1, num2, num1+num2);
break;
case '-':
printf("%d - %d = %d",num1,
num2, num1-num2);
break;
case '*':
printf("%d * %d = %d",num1,
num2, num1*num2);
break;
case '/':
printf("%d / %d = %d",num1,
num2, num1/num2);
break;
default:
printf("Error! op is not
correct");
}
return 0;
}
Switch case Statement (3)
Output
Enter an operator (+, -, *,): -
Enter two operands: 32
12
32 - 12 = 20
goto Statement (1)
o The goto statement is used to alter the normal sequence of a C program.
o Syntax of goto statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
o The label is an identifier. When goto statement is encountered, control of the
program jumps to label: and starts executing the code.
goto Statement (2)
Example: To check whether you can
vote or not
int main()
{
int age;
Vote:
printf("you are eligible for
voting");
NoVote:
printf("you are not eligible to
vote");
printf("Enter your age:");
scanf("%d", &age);
if(age>=18)
goto Vote;
else
goto NoVote;
return 0;
}
Output
Enter your age:15
You are not eligible to vote
Reason to avoid goto Statement
o The use of goto statement may lead to code that is buggy and hard to follow. For example:
one:
for (i = 0; i < number; ++i)
{
test += i;
goto two;
}
two:
if (test > 5) {
goto three;
}
... .. ...
Also, goto statement allows you to do bad stuff such as jump out of scope.
o That being said, goto statement can be useful sometimes. For example: to break from nested loops.
Thank You

More Related Content

PPT
RECURSION IN C
PPT
Bitwise operators
PPTX
Variables in C++, data types in c++
PPTX
Functions in c language
PPT
File handling in c
PPT
Arithmetic operator
PPTX
Structure in C language
RECURSION IN C
Bitwise operators
Variables in C++, data types in c++
Functions in c language
File handling in c
Arithmetic operator
Structure in C language

What's hot (20)

PPTX
Functions in c
PPTX
Pointers in c++
PPTX
Control statements in c
PPTX
PPT
C program
PPT
C++ Language
PPTX
Union in c language
PPTX
Recursive Function
PPTX
Operator.ppt
PPT
Strings
PPTX
C Programming: Control Structure
PPTX
Python for loop
PPT
Pointers C programming
PPTX
asymptotic notation
PPTX
Passing an Array to a Function (ICT Programming)
PDF
Operators in c programming
PPTX
Forloop
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Programming in c Arrays
Functions in c
Pointers in c++
Control statements in c
C program
C++ Language
Union in c language
Recursive Function
Operator.ppt
Strings
C Programming: Control Structure
Python for loop
Pointers C programming
asymptotic notation
Passing an Array to a Function (ICT Programming)
Operators in c programming
Forloop
Array Introduction One-dimensional array Multidimensional array
Programming in c Arrays
Ad

Similar to Control structure of c (20)

PPTX
C decision making and looping.
PDF
Programming fundamental 02
PPTX
C programming Control Structure.pptx
PPTX
computer programming Control Statements.pptx
PPTX
Control Structures in C
PPTX
What is c
PDF
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
PDF
Control statements-Computer programming
PPTX
Programming in C
DOCX
Cs291 assignment solution
PPTX
Basics of Control Statement in C Languages
PDF
Unit ii chapter 2 Decision making and Branching in C
PPTX
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
PPTX
the refernce of programming C notes ppt.pptx
PPTX
COM1407: Program Control Structures – Repetition and Loops
PDF
5 c control statements looping
PPT
Lec 10
C decision making and looping.
Programming fundamental 02
C programming Control Structure.pptx
computer programming Control Statements.pptx
Control Structures in C
What is c
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
Control statements-Computer programming
Programming in C
Cs291 assignment solution
Basics of Control Statement in C Languages
Unit ii chapter 2 Decision making and Branching in C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
the refernce of programming C notes ppt.pptx
COM1407: Program Control Structures – Repetition and Loops
5 c control statements looping
Lec 10
Ad

More from Komal Kotak (6)

PDF
Internet Of things
PPTX
Mesh analysis and Nodal Analysis
PPTX
Decision Tree and Bayesian Classification
PPT
4 stroke petrol engine and 4 stroke diesel engine
PPTX
Corruption
PPTX
Traits of good listener
Internet Of things
Mesh analysis and Nodal Analysis
Decision Tree and Bayesian Classification
4 stroke petrol engine and 4 stroke diesel engine
Corruption
Traits of good listener

Recently uploaded (20)

PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Basic Mud Logging Guide for educational purpose
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Business Ethics Teaching Materials for college
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Pre independence Education in Inndia.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Basic Mud Logging Guide for educational purpose
102 student loan defaulters named and shamed – Is someone you know on the list?
Business Ethics Teaching Materials for college
Module 4: Burden of Disease Tutorial Slides S2 2025
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
O5-L3 Freight Transport Ops (International) V1.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Classroom Observation Tools for Teachers
Abdominal Access Techniques with Prof. Dr. R K Mishra
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Anesthesia in Laparoscopic Surgery in India
Pre independence Education in Inndia.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
human mycosis Human fungal infections are called human mycosis..pptx

Control structure of c

  • 1. Control Structure in C Made By: Komal Kotak C.K.Pithawala College of Engineering and Technology
  • 2. C Programming Definition C is a high-level and general- purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System (OS) in the early 1970s
  • 3. Conditions in C Programming o C executes program sequentially. o Sometimes a program requires checking of certain conditions in program execution. o C provides various key condition statements to check conditions and execute statements according conditional criteria. o This statements are called decision making statements and control statements.
  • 4. Control Structure in C Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Show here is the general form of a typical decision making structure found in most of the programming languages −
  • 5. if-else statement (1) The if-else statement can exist in two forms: with or without the else. The two forms are: if(expression) statement or if(expression) statement1 else statement2 int a, b; // ... if(a < b) a = 0; else b = 0;
  • 6. Example: Odd Or Even No. Program #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d",&number); if( number%2 == 0 ) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); return 0; } if-else statement (2) Output Enter an integer: 7 7 is an odd integer.
  • 7. Nested if-else statement (1) o The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. o The nested if-else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
  • 8. Nested if-else statement (2) Example: Program to relate two integers using =, > or < #include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); if(number1 == number2) { printf("Result: %d = %d",number1,number2); } else if (number1 > number2) { printf("Result: %d > %d", number1, number2) } else { printf("Result: %d < %d",number1, number2); } return 0; } Output Enter two integers: 12 23 Result: 12 < 23
  • 9. C Programming for Loop Loops are used in programming to repeat a specific block until some end condition is met.There are three loops in C programming: 1. for loop 2. while loop 3. do...while loop
  • 10. For Loop (1) o The initialization statement is executed only once. o Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated. o This process repeats until the test expression is false. o The for loop is commonly used when the number of iterations is known.
  • 11. For Loop (2) Example: Program to calculate the sum of first n natural numbers #include <stdio.h> int main() { int num, i, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); for(i = 1; i <= num; i++) { sum += i; } printf("Sum = %d", sum); return 0; } Output Enter a positive integer: 10 Sum = 55
  • 12. while Loop (1) o The while loop evaluates the test expression. o If the test expression is true (nonzero), codes inside the body of while loop is evaluated. The test expression is evaluated again.The process goes on until the test expression is false. o When the test expression is false, the while loop is terminated. o The syntax of a while loop is: while (testExpression) { //codes } o where, testExpression checks the condition is true or false before each loop.
  • 13. while Loop (2) Example: Program to find factorial of a number #include <stdio.h> int main() { int number; int factorial; printf("Enter an integer: "); scanf("%d",&number); factorial = 1; while (number > 0) { factorial *= number; --number; } printf("Factorial= %d", factorial); return 0; } Output Enter an integer: 5 Factorial = 120
  • 14. do-while Loop (1) o The do..while loop is similar to the while loop with one important difference.The body of do...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once. o do...while loop Syntax do { // codes } while (testExpression); o The code block (loop body) inside the braces is executed once. o Then, the test expression is evaluated. If the test expression is true, the loop body is executed again.This process goes on until the test expression is evaluated to 0 (false). o When the test expression is false (nonzero), the do...while loop is terminated.
  • 15. do-while Loop (2) Example: Program to add numbers until user enters zero #include <stdio.h> int main() { float number, sum = 0; do { printf("Enter a number: "); scanf("%f", &number); sum += number; } while(number != 0.0); printf("Sum = %f",sum); return 0; } Output Enter a number: 1.5 Enter a number: 2.4 Enter a number: -3.4 Enter a number: 4.2 Enter a number: 0 Sum = 4.70
  • 16. break Statement (1) o It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. o In such cases, break and continue statements are used. o The break statement terminates the loop (for, while and do...while loop) immediately when it is encountered.The break statement is used with decision making statement such as if...else. o Syntax of break statement break;
  • 18. continue Statement o The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else. o Syntax of continue Statement continue;
  • 19. Switch case Statement (1) o The switch case statement evaluates the value of an expression and a block of code is selected on the basis of that evaluated expression. o Each case refers back to the original expression. o The data type that can be used in switch expression is integer type only. o Each case has a break statement. o The switch case takes decision on the basis of equality.
  • 20. Switch case Statement (2) int main() { char op; int num1,num2; printf("Enter an operator (+,-,*,/):"); scanf("%c", &op); printf("Enter two operands: "); scanf("%d %d",&num1, &num2); switch(op) { case '+': printf("%d + %d = %d",num1, num2, num1+num2); break; case '-': printf("%d - %d = %d",num1, num2, num1-num2); break; case '*': printf("%d * %d = %d",num1, num2, num1*num2); break; case '/': printf("%d / %d = %d",num1, num2, num1/num2); break; default: printf("Error! op is not correct"); } return 0; }
  • 21. Switch case Statement (3) Output Enter an operator (+, -, *,): - Enter two operands: 32 12 32 - 12 = 20
  • 22. goto Statement (1) o The goto statement is used to alter the normal sequence of a C program. o Syntax of goto statement goto label; ... .. ... ... .. ... ... .. ... label: statement; o The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code.
  • 23. goto Statement (2) Example: To check whether you can vote or not int main() { int age; Vote: printf("you are eligible for voting"); NoVote: printf("you are not eligible to vote"); printf("Enter your age:"); scanf("%d", &age); if(age>=18) goto Vote; else goto NoVote; return 0; } Output Enter your age:15 You are not eligible to vote
  • 24. Reason to avoid goto Statement o The use of goto statement may lead to code that is buggy and hard to follow. For example: one: for (i = 0; i < number; ++i) { test += i; goto two; } two: if (test > 5) { goto three; } ... .. ... Also, goto statement allows you to do bad stuff such as jump out of scope. o That being said, goto statement can be useful sometimes. For example: to break from nested loops.