SlideShare a Scribd company logo
Control Structures
if-else Statement
if-else Statement
◻ In simple ‘if’ statement, when the condition is true, a set of statements are
executed. But when it is false, there is no alternate set of statements
◻ The statement ‘else’ provides the same
Syntax:
if (testExpression)
{
// codes inside the body of if
}
else
{
// codes inside the body of else
}
2
If-else Statement (Cont’d)
else Statement
Example:
if (Duration >= 3) {
RateOfInterest = 6.0;
}
else {
RateOfInterest = 5.5;
}
else if Statement
◻ The ‘else if’ statement is to check for a sequence of conditions
◻ When one condition is false, it checks for the next condition and so on
◻ When all the conditions are false the ‘else’ block is executed
◻ The statements in that conditional block are executed and the other ‘if’ statements are
skipped
3
Nested if Statement
4
Syntax:
if (condition-1)
{
Statement 1;
if (condition-2)
{
Statement 2;
}
else
{
Statement n;
}
}
else {
Statement x;
}
Next Statement;
Nested if Statement
• An ‘if’ statement embedded within another ‘if’
statement is called as nested ‘if’
• Example:
if (iDuration > 6 )
{
if (dPrincipalAmount > 25000)
{
printf(“Your percentage of incentive is 4%”);
}
else
{
printf(“Your percentage of incentive is 2%”);
}}
else {
printf(“No incentive”);
}
Example
/* Program to check whether an integer entered by the user is odd
or even */
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
/* True if remainder is 0*/
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
5
switch case Statement
◻ The ‘switch’ statement is a selection control structure that selects a choice
from the set of available choices.
◻ It is very similar to ‘if’ statement.
◻ But ‘switch’ statement cannot replace ‘if’ statement in all situations.
Syntax:
int n;
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
6
7
switch case Statement
switch case Example
/*Following is a simple program to demonstrate syntax of switch.
#include <stdio.h>
void main()
{
int x =2;
switch (x)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
}
}
Output: Choice is 2
8
Iterational Control Structures
◻ Iterational (repetitive) control structures are used to repeat certain
statements for a specified number of times
◻ The statements are executed as long as the condition is true
◻ These kind of control structures are also called as loop control structures
◻ Three kinds of loop control structures are:
⬜ while
⬜ do while
⬜ for
9
while Loop Control Structure
◻ A ‘while’ loop is used to repeat certain statements as long as the condition is true
◻ When the condition becomes false, the ‘while’ loop is quitted
◻ This loop control structure is called as an entry-controlled loop because, only when the
condition is true, are the statements executed
Syntax:
while (condition)
{
Set of statements;
}
Next Statement;
Example:
int Count = 1;
while (Count <= 3)
{
printf(“%dn”,Count);
}
10
What is the output of the following code?
Example 1:
#include<stdio.h>
int main(){
int count=3;
while (count<=5) {
printf("%dn",count);
count++;
}
return 0;
}
11
Example 2:
#include<stdio.h>
int main(){
int count = 0;
while(count<=10)
{
printf("n PCU");
count++;
}
return 0;
}
do while Loop Control Structure
◻ The ‘do while’ loop is very similar to ‘while’ loop. In ‘do while’ loop, the
condition is tested at the end of the loop.
◻ Because of this, even when the condition is false, the body of the loop
is executed at least once.
◻ This is an exit-controlled loop.
Syntax:
do
{
Set of statement(s);
} while (condition);
Next Statement;
12
do while Loop Control Structure Example
#include<stdio.h>
int main()
{
int number, sum = 0;
do {
printf(“Enter a number or 0(zero) to end the input
”);
scanf("%d",&number);
sum = sum + number;
} while (number != 0);
return 0;
13
Difference between
while and do while
loops 14
for Loop Control Structure
◻ The ‘for’ loops are similar to the other loop control structures
◻ The ‘for’ loops are generally used when certain statements have to be
executed a specific number of times
◻ Advantage of for loops:
⬜ All the three parts of a loop (initialization, condition , increment) can be given
in a single statement
⬜ Because of this, there is no chance of user missing out initialization or increment
steps which is the common programming error in ‘while’ and ‘do while’ loops
Syntax:
for (Initialization; Termination-Condition; Increment-Step)
{
Set of statement(s);
}
Next Statement;
15
for Loop Control Structure (cont’d)
Example:
#include<stdio.h>
int main(){
int count;
for (int count = 1; count <= 5; count+
+)
{
printf(“%dn”,count);
}
return 0;
}
16
Output:
1
2
3
4
5
Unconditional Statement
goto statement :
• The statement is a control transfer statement that allows the control jump to a
labelled statement within the same function. The basic syntax of the follows:
goto statement example:
#include <stdio.h>
int main() {
int start = 1, end = 10;
int curr = start;
print_line:
printf("%d ", curr);
if (curr < end) {
curr++;
goto print_line;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10
Output :
Unconditional Statement
Break Statement:
• The break in C is a loop control statement that breaks out of the loop when
encountered. It can be used inside loops or switch statements to bring the control
out of the block. The break statement can only break out of a single loop at a time.
• The break statement has the following two usages:
• When the break statement is encountered inside a loop, the loop is immediately
terminated and program control resumes at the next statement following the loop.
• It can be used to terminate a case in the switch statement.
• Syntax:
break;
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
for (i = 1; i <= 4; ++i) {
printf("Enter n%d: ", i);
scanf("%lf", &number);
// if the user enters a negative number, break the loop
if (number < 0.0) {
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
return 0;
}
Enter n1: 1
Enter n2: 2
Enter n3: 3
Enter n4: 4
Sum = 10.00
Output :
Continue Statement in C
• The continue statement is used to skip the current
iteration of any loop and bring the control to the
beginning of the iteration.
• The statements following the continue statement are
skipped and the iteration starts again.
• It does not terminate the loop but only affects the
current iteration.
Continue Statement in C
#include<stdio.h>
int main () {
int i = 0;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
printf("%dn", i);
i++;
}
return 0;
}
• This C program in the C Compiler iterates 05
times using a for loop.
• The continue statement is encountered when
i=3. So it skips the remainder of the loop's
body for that iteration.
• As a result, it only prints the numbers 0-2 and
4-5, skipping out 3.
Output :
0
1
2
4
5
Continue Statement in C
Output:
2
4
6
8
10
#include <stdio.h>
int main() {
int i;
// Print even numbers between 1 and 10
for (i = 1; i <= 10; i++) {
if (i % 2 != 0) {
// Skip odd numbers
continue;}
printf("%dn", i);
}
return 0;
}
Ad

Recommended

PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
PPTX
Dti2143 chap 4 control structures aka_selection
alish sha
 
PPTX
Dti2143 chap 4 control structures aka_selection
alish sha
 
PDF
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
PPTX
Condition Stmt n Looping stmt.pptx
Likhil181
 
PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PallaviGholap4
 
PPT
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PDF
Controls & Loops in C
Thesis Scientist Private Limited
 
PPTX
Control Structures in C
sana shaikh
 
PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
PDF
Control statements-Computer programming
nmahi96
 
PDF
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PPTX
Module 2- Control Structures
nikshaikh786
 
PDF
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
PPTX
Unit_3A_If_Else_Switch and a if else statment example
umaghosal12101974
 
PPTX
Control Flow Statements
Tarun Sharma
 
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
PPT
Ch3 repetition
Hattori Sidek
 
PPTX
C Programming - Decision making, Looping
MURALIDHAR R
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPT
control-statements, control-statements, control statement
crrpavankumar
 
PPTX
Lect3-C--EEB.pptx
KIJAMALEGI
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PPTX
CONTROL STMTS.pptx
JavvajiVenkat
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PPTX
Control statement-Selective
Nurul Zakiah Zamri Tan
 
PPTX
CH-4 (1).pptx
Mehul Desai
 
PPTX
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
PPTX
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 

More Related Content

Similar to C Programming Control Structures(if,if-else) (20)

PDF
Controls & Loops in C
Thesis Scientist Private Limited
 
PPTX
Control Structures in C
sana shaikh
 
PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
PDF
Control statements-Computer programming
nmahi96
 
PDF
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PPTX
Module 2- Control Structures
nikshaikh786
 
PDF
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
PPTX
Unit_3A_If_Else_Switch and a if else statment example
umaghosal12101974
 
PPTX
Control Flow Statements
Tarun Sharma
 
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
PPT
Ch3 repetition
Hattori Sidek
 
PPTX
C Programming - Decision making, Looping
MURALIDHAR R
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPT
control-statements, control-statements, control statement
crrpavankumar
 
PPTX
Lect3-C--EEB.pptx
KIJAMALEGI
 
PPTX
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
PPTX
CONTROL STMTS.pptx
JavvajiVenkat
 
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
PPTX
Control statement-Selective
Nurul Zakiah Zamri Tan
 
PPTX
CH-4 (1).pptx
Mehul Desai
 
Controls & Loops in C
Thesis Scientist Private Limited
 
Control Structures in C
sana shaikh
 
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Control statements-Computer programming
nmahi96
 
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Module 2- Control Structures
nikshaikh786
 
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
Unit_3A_If_Else_Switch and a if else statment example
umaghosal12101974
 
Control Flow Statements
Tarun Sharma
 
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Carlos701746
 
Ch3 repetition
Hattori Sidek
 
C Programming - Decision making, Looping
MURALIDHAR R
 
Loops and conditional statements
Saad Sheikh
 
control-statements, control-statements, control statement
crrpavankumar
 
Lect3-C--EEB.pptx
KIJAMALEGI
 
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
CONTROL STMTS.pptx
JavvajiVenkat
 
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Control statement-Selective
Nurul Zakiah Zamri Tan
 
CH-4 (1).pptx
Mehul Desai
 

Recently uploaded (20)

PPTX
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
PPTX
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
PPTX
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
PPTX
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
PDF
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
PDF
International Journal of Advanced Information Technology (IJAIT)
ijait
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
PPTX
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PDF
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
PPTX
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
PDF
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
PPT
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
PDF
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
PDF
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
PPTX
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
PDF
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
PPTX
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
PPTX
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
Introduction to sensing and Week-1.pptx
KNaveenKumarECE
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
Structured Programming with C++ :: Kjell Backman
Shabista Imam
 
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Rapid Prototyping for XR: Lecture 4 - High Level Prototyping.
Mark Billinghurst
 
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
دراسة حاله لقرية تقع في جنوب غرب السودان
محمد قصص فتوتة
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
LECTURE 7 COMPUTATIONS OF LEVELING DATA APRIL 2025.pptx
rr22001247
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Ad

C Programming Control Structures(if,if-else)

  • 2. if-else Statement if-else Statement ◻ In simple ‘if’ statement, when the condition is true, a set of statements are executed. But when it is false, there is no alternate set of statements ◻ The statement ‘else’ provides the same Syntax: if (testExpression) { // codes inside the body of if } else { // codes inside the body of else } 2
  • 3. If-else Statement (Cont’d) else Statement Example: if (Duration >= 3) { RateOfInterest = 6.0; } else { RateOfInterest = 5.5; } else if Statement ◻ The ‘else if’ statement is to check for a sequence of conditions ◻ When one condition is false, it checks for the next condition and so on ◻ When all the conditions are false the ‘else’ block is executed ◻ The statements in that conditional block are executed and the other ‘if’ statements are skipped 3
  • 4. Nested if Statement 4 Syntax: if (condition-1) { Statement 1; if (condition-2) { Statement 2; } else { Statement n; } } else { Statement x; } Next Statement; Nested if Statement • An ‘if’ statement embedded within another ‘if’ statement is called as nested ‘if’ • Example: if (iDuration > 6 ) { if (dPrincipalAmount > 25000) { printf(“Your percentage of incentive is 4%”); } else { printf(“Your percentage of incentive is 2%”); }} else { printf(“No incentive”); }
  • 5. Example /* Program to check whether an integer entered by the user is odd or even */ #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d",&number); /* True if remainder is 0*/ if( number%2 == 0 ) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); return 0; } 5
  • 6. switch case Statement ◻ The ‘switch’ statement is a selection control structure that selects a choice from the set of available choices. ◻ It is very similar to ‘if’ statement. ◻ But ‘switch’ statement cannot replace ‘if’ statement in all situations. Syntax: int n; switch (n) { case 1: // code to be executed if n = 1; break; case 2: // code to be executed if n = 2; break; default: // code to be executed if n doesn't match any cases } 6
  • 8. switch case Example /*Following is a simple program to demonstrate syntax of switch. #include <stdio.h> void main() { int x =2; switch (x) { case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; case 3: printf("Choice is 3"); break; default: printf("Choice other than 1, 2 and 3"); } } Output: Choice is 2 8
  • 9. Iterational Control Structures ◻ Iterational (repetitive) control structures are used to repeat certain statements for a specified number of times ◻ The statements are executed as long as the condition is true ◻ These kind of control structures are also called as loop control structures ◻ Three kinds of loop control structures are: ⬜ while ⬜ do while ⬜ for 9
  • 10. while Loop Control Structure ◻ A ‘while’ loop is used to repeat certain statements as long as the condition is true ◻ When the condition becomes false, the ‘while’ loop is quitted ◻ This loop control structure is called as an entry-controlled loop because, only when the condition is true, are the statements executed Syntax: while (condition) { Set of statements; } Next Statement; Example: int Count = 1; while (Count <= 3) { printf(“%dn”,Count); } 10
  • 11. What is the output of the following code? Example 1: #include<stdio.h> int main(){ int count=3; while (count<=5) { printf("%dn",count); count++; } return 0; } 11 Example 2: #include<stdio.h> int main(){ int count = 0; while(count<=10) { printf("n PCU"); count++; } return 0; }
  • 12. do while Loop Control Structure ◻ The ‘do while’ loop is very similar to ‘while’ loop. In ‘do while’ loop, the condition is tested at the end of the loop. ◻ Because of this, even when the condition is false, the body of the loop is executed at least once. ◻ This is an exit-controlled loop. Syntax: do { Set of statement(s); } while (condition); Next Statement; 12
  • 13. do while Loop Control Structure Example #include<stdio.h> int main() { int number, sum = 0; do { printf(“Enter a number or 0(zero) to end the input ”); scanf("%d",&number); sum = sum + number; } while (number != 0); return 0; 13
  • 14. Difference between while and do while loops 14
  • 15. for Loop Control Structure ◻ The ‘for’ loops are similar to the other loop control structures ◻ The ‘for’ loops are generally used when certain statements have to be executed a specific number of times ◻ Advantage of for loops: ⬜ All the three parts of a loop (initialization, condition , increment) can be given in a single statement ⬜ Because of this, there is no chance of user missing out initialization or increment steps which is the common programming error in ‘while’ and ‘do while’ loops Syntax: for (Initialization; Termination-Condition; Increment-Step) { Set of statement(s); } Next Statement; 15
  • 16. for Loop Control Structure (cont’d) Example: #include<stdio.h> int main(){ int count; for (int count = 1; count <= 5; count+ +) { printf(“%dn”,count); } return 0; } 16 Output: 1 2 3 4 5
  • 17. Unconditional Statement goto statement : • The statement is a control transfer statement that allows the control jump to a labelled statement within the same function. The basic syntax of the follows:
  • 18. goto statement example: #include <stdio.h> int main() { int start = 1, end = 10; int curr = start; print_line: printf("%d ", curr); if (curr < end) { curr++; goto print_line; } return 0; } 1 2 3 4 5 6 7 8 9 10 Output :
  • 19. Unconditional Statement Break Statement: • The break in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time. • The break statement has the following two usages: • When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. • It can be used to terminate a case in the switch statement.
  • 21. #include <stdio.h> int main() { int i; double number, sum = 0.0; for (i = 1; i <= 4; ++i) { printf("Enter n%d: ", i); scanf("%lf", &number); // if the user enters a negative number, break the loop if (number < 0.0) { break; } sum += number; // sum = sum + number; } printf("Sum = %.2lf", sum); return 0; } Enter n1: 1 Enter n2: 2 Enter n3: 3 Enter n4: 4 Sum = 10.00 Output :
  • 22. Continue Statement in C • The continue statement is used to skip the current iteration of any loop and bring the control to the beginning of the iteration. • The statements following the continue statement are skipped and the iteration starts again. • It does not terminate the loop but only affects the current iteration.
  • 23. Continue Statement in C #include<stdio.h> int main () { int i = 0; while (i <= 5) { if (i == 3) { i++; continue; } printf("%dn", i); i++; } return 0; } • This C program in the C Compiler iterates 05 times using a for loop. • The continue statement is encountered when i=3. So it skips the remainder of the loop's body for that iteration. • As a result, it only prints the numbers 0-2 and 4-5, skipping out 3. Output : 0 1 2 4 5
  • 24. Continue Statement in C Output: 2 4 6 8 10 #include <stdio.h> int main() { int i; // Print even numbers between 1 and 10 for (i = 1; i <= 10; i++) { if (i % 2 != 0) { // Skip odd numbers continue;} printf("%dn", i); } return 0; }