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;
}

More Related Content

PDF
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
Dti2143 chap 4 control structures aka_selection
PDF
Fundamental of Information Technology - UNIT 8
PPTX
Condition Stmt n Looping stmt.pptx
PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PPT
2. Control structures with for while and do while.ppt
PPTX
control_structures_c_language_regarding how to represent the loop in language...
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
Fundamental of Information Technology - UNIT 8
Condition Stmt n Looping stmt.pptx
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
2. Control structures with for while and do while.ppt
control_structures_c_language_regarding how to represent the loop in language...

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

PDF
Controls & Loops in C
PPTX
Control Structures in C
PPS
Programming in Arduino (Part 2)
PDF
Control statements-Computer programming
PDF
Module 2 - Control Structures c programming.pptm.pdf
PPTX
Module 2- Control Structures
PDF
Unit 2=Decision Control & Looping Statements.pdf
PPTX
Unit_3A_If_Else_Switch and a if else statment example
PPTX
Control Flow Statements
PDF
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
PPT
Ch3 repetition
PPTX
C Programming - Decision making, Looping
PDF
Loops and conditional statements
PPT
control-statements, control-statements, control statement
PPTX
Lect3-C--EEB.pptx
PPTX
Constructs (Programming Methodology)
PPTX
CONTROL STMTS.pptx
PPTX
Programming in java - Concepts- Operators- Control statements-Expressions
PPTX
Control statement-Selective
PPTX
CH-4 (1).pptx
Controls & Loops in C
Control Structures in C
Programming in Arduino (Part 2)
Control statements-Computer programming
Module 2 - Control Structures c programming.pptm.pdf
Module 2- Control Structures
Unit 2=Decision Control & Looping Statements.pdf
Unit_3A_If_Else_Switch and a if else statment example
Control Flow Statements
OIT 116 LOOPS AND CONDITION STATEMENTS.pdf
Ch3 repetition
C Programming - Decision making, Looping
Loops and conditional statements
control-statements, control-statements, control statement
Lect3-C--EEB.pptx
Constructs (Programming Methodology)
CONTROL STMTS.pptx
Programming in java - Concepts- Operators- Control statements-Expressions
Control statement-Selective
CH-4 (1).pptx
Ad

Recently uploaded (20)

PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
DOCX
573137875-Attendance-Management-System-original
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Mechanical Engineering MATERIALS Selection
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
PPT on Performance Review to get promotions
PPTX
web development for engineering and engineering
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
573137875-Attendance-Management-System-original
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Model Code of Practice - Construction Work - 21102022 .pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Mechanical Engineering MATERIALS Selection
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT on Performance Review to get promotions
web development for engineering and engineering
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
CH1 Production IntroductoryConcepts.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
bas. eng. economics group 4 presentation 1.pptx
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; }