SlideShare a Scribd company logo
1 of 41
Download to read offline
Chapter 4
Control Structures
8/12/2021 1
Control structure:
In a program, a control structure determines the order in which
statements are executed. Control structures refer to the structure
of program that may be sequential or conditional or iterative. C
programs are basically of three structures:
• In sequential execution, each statement in the source code will be
executed one by one in a sequential order.
• The selection or conditional statement is used for making decisions
and branching statements.
• The iterative control structures are used for repetitively executing a
block of code multiple times.
Control statement:
The statement that alters the flow of execution of the program is
known as control statements.
1. Decision Making (conditional) statements
2. Loop statements
3. Jumping Statements
8/12/2021 2
1. Decision Making (conditional)
statements
Conditional statement:
A statement that is executed only when certain condition
within a program has been met is called conditional
expression. These expressions are used for decision
making.
Following statements are used to perform the task of
decision making:
I. IF statement
II. SWITCH statement
III. Conditional statement
8/12/2021 3
I. IF statement:
If the statement is used to control the flow of
execution of statements. It is implemented in
different forms. Depending on the complexity
of conditions to be tested, the different forms
are:
A. Simple if statement
B. If….. else
C. Nested if else
D. Else if ladder
8/12/2021 4
A. Simple if statement
Syntax of simple if statement:
if (condition)
{
//These statements will only execute if the condition is true
}
Flowchart for simple if statement:
8/12/2021 5
Example for simple if statement:
#include <stdio.h>
#include <conio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf(" %d is less than %d",x,y);
}
return 0;
getch();
}
8/12/2021 6
B. If …. else Statement
Syntax of if...else statement:
if (testexpression)
{
// statementsto be executed if the test expression is true
}
else {
// statementsto be executed if the test expression is false
}
8/12/2021 7
Example for if...else statement:
// Check whether an integer is odd or even using if…. Else statement
#include <stdio.h>
int main()
{
int num;
printf("Enteran integer: ");
scanf("%d", &num);
if (num%2 == 0)
{
printf("%dis an even integer.",num);
}
else
{
printf("%dis an odd integer.",num);
}
return 0;
}
8/12/2021 8
C. Nested if...else statement
Syntax of Nested if...else statement:
if(condition) {
if(condition2) {
//Statement1
}
else {
//Statement2
}
}
else {
//Statement3
}
8/12/2021 9
Flowchart of Nested if...else statement:
8/12/2021 10
Example of Nested if...else statement:
/* C Program to find given number is positive or negative using nested if ...else */
#include <stdio.h>
#include <conio.h>
int main()
{
float num;
printf("Enter a number: ");
scanf("%f",&num);
if (num <= 0)
{
if (num == 0)
printf("Youentered 0.");
else
printf("%fis a negative number.",num);
}
else
printf("%fis a positive number.",num);
return 0;
getch();
}
8/12/2021 11
// Check Largest number among two given numbers
#include <stdio.h>
main()
{
int a, b;
printf("Enter two numbers: n");
scanf("%d %d", &a, &b);
if(a >= b)
{
if (a==b)
{
printf("Both number are equaln");
}
else {
printf("%d is Largestn", a);
}
}
else {
printf("%d is Largestn", b);
}
}
8/12/2021 12
D. Else if ladder
Syntax of else if ladder statement:
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}
8/12/2021 13
Flowchart of else if ladder statement:
8/12/2021 14
Example of else if ladder statement:
// Check Largest number among two given numbers
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: n");
scanf("%d %d", &a, &b);
if(a == b)
{
printf("Both numbers are equal“,a,b);
}
else if (a > b)
{
printf(" %d is largest than %d", a, b);
}
else
{
printf(" %d is largest than %d", b, a);
}
return 0;
}
8/12/2021 15
II. Switch Statement
The switch statement allows us to execute one code block among many alternatives.
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.
If a case match is not found, then the default statement is executed, and the control goes out of the
switch block.
Syntax of switch statement:
switch (expression)
​{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
8/12/2021 16
flowchart of switch statement
8/12/2021 17
#include <stdio.h>
int main()
{
int num=2;
switch(num)
{
case 1:
printf("Case1:Value is: %d", num);
break;
case 2:
printf("Case2:Value is: %d", num);
break;
case 3:
printf("Case3:Value is: %d", num);
break;
default:
printf("Default:Value is: %d", num);
}
return 0;
}
8/12/2021 18
#include <stdio.h>
main()
{
int a, b;
char ch;
printf("What do you want to perform?? n ");
printf(" Enter first letter: A for add, S for subtract, M for multiply, or D for divide?n");
printf("Your choice: ");
ch = getchar();
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
switch(ch)
{
case 'A': printf("%d", a + b);
break;
case 'S': printf("%d", a - b);
break;
case 'M': printf("%d", a * b);
break;
case 'D': if(b!=0) printf("%d", a / b);
break;
default:
printf("choose properly") ;
}
}
8/12/2021 19
III. Conditional(ternary ) statement
If any operator is used on three operands or variable is known as Ternary
Operator.
The first argument is a comparison argument, the second is the result
upon a true comparison, and the third is the result upon a false
comparison.
Ternary operator is shortened way of writing an if-else statement.
syntax:
(condition) ? expression-2 : expression-3
8/12/2021 20
//example program to illustrate the use of ternary statements
#include<stdio.h>
main()
{
int a, b, c, large;
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b ? (a>c?a:c) : (b>c?b:c);
printf("Largest Number is: %d", large);
}
8/12/2021 21
2. Loop statements
Loop statement:
loops are used to repeat a block of code until a
specified condition is met.
Following statements are used to perform
repetitive task:
I. for loop
II. while loop
III. do while loop
8/12/2021 22
I. for loop
8/12/2021 23
8/12/2021 24
8/12/2021 25
8/12/2021 26
8/12/2021 27
8/12/2021 28
8/12/2021 29
II. while loop
8/12/2021 30
8/12/2021 31
8/12/2021 32
III. do while loop
8/12/2021 33
8/12/2021 34
8/12/2021 35
3 . Jumping statement
There are three different controls used to jump
from one C program statement to another and
make the execution of the programming
procedure fast. These three Jumping controls are:
A. go to statement
B. break statement
C. continue statement
8/12/2021 36
A. go to statement
The go to moves the control on a specified address called label or label name.
Syntax:
goto label_name;
.. ..
label_name: C-statements
Example program of go to statement:
#include <stdio.h>
main()
{
int sum=0;
for(int i = 0; i<=10; i++)
{
sum = sum+i;
if(i==5)
{
goto addition;
}
}
addition:
printf("%d", sum);
}
8/12/2021 37
B. break statement
Break is always used with the decision-makingstatementlike if and switch
statements.
The statementwill quit from the loop when the conditionis true.
Example program using break statement
#include< stdio.h>
main()
{
int i=1;
while(i<=10)
{
if(i==6)
{
break;
}
printf("n I=%d",i);
i++;
}
}
8/12/2021 38
C. continue statement
This statementis skip some part of iteration (loop) and comes to the next looping
step i.e. it will increment / decrement the loop value, when continue occurs.
Example program using continue statement
#include< stdio.h>
main()
{
int i=1;
while(i<=10)
{
if(i==6)
{
i++;
continue;
}
printf("n I=%d",i);
i++;
}
}
8/12/2021 39
Assignment 4(submit up to 22 Jan)
1. WAP to input an integer and check if it is palindrome or not.
2. Differentiate between
• pre-test and post-test loop.
• Break and continue statement
• Switch and nested if else
3. WAP to generate all prime numbers from 1 to 100.
4. WAP to evaluate the following power series:
ex =1+x/1!+x2/2!+x3/3!+…….xn/n!, 0<x<1
3. WAP to display the following output :
5
5 4
5 4 3
8/12/2021 40
Thank you 
8/12/2021 41

More Related Content

Similar to chapter-4 slide.pdf

Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
RSathyaPriyaCSEKIOT
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
yasir_cesc
 

Similar to chapter-4 slide.pdf (20)

Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdf
 
Control Structures
Control StructuresControl Structures
Control Structures
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, Strings
 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 

More from cricketreview (10)

FINAL PRESENTATION.pptx
FINAL PRESENTATION.pptxFINAL PRESENTATION.pptx
FINAL PRESENTATION.pptx
 
DREM_chapter 6.pptx
DREM_chapter 6.pptxDREM_chapter 6.pptx
DREM_chapter 6.pptx
 
URBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdfURBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdf
 
Estimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptxEstimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptx
 
Chapter 3.4_ Art Deco.pdf
Chapter 3.4_  Art Deco.pdfChapter 3.4_  Art Deco.pdf
Chapter 3.4_ Art Deco.pdf
 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptx
 
3e- Persian Gardens.pptx
3e- Persian Gardens.pptx3e- Persian Gardens.pptx
3e- Persian Gardens.pptx
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
Vernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptxVernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptx
 
URBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdfURBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdf
 

Recently uploaded

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Recently uploaded (20)

Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 

chapter-4 slide.pdf

  • 2. Control structure: In a program, a control structure determines the order in which statements are executed. Control structures refer to the structure of program that may be sequential or conditional or iterative. C programs are basically of three structures: • In sequential execution, each statement in the source code will be executed one by one in a sequential order. • The selection or conditional statement is used for making decisions and branching statements. • The iterative control structures are used for repetitively executing a block of code multiple times. Control statement: The statement that alters the flow of execution of the program is known as control statements. 1. Decision Making (conditional) statements 2. Loop statements 3. Jumping Statements 8/12/2021 2
  • 3. 1. Decision Making (conditional) statements Conditional statement: A statement that is executed only when certain condition within a program has been met is called conditional expression. These expressions are used for decision making. Following statements are used to perform the task of decision making: I. IF statement II. SWITCH statement III. Conditional statement 8/12/2021 3
  • 4. I. IF statement: If the statement is used to control the flow of execution of statements. It is implemented in different forms. Depending on the complexity of conditions to be tested, the different forms are: A. Simple if statement B. If….. else C. Nested if else D. Else if ladder 8/12/2021 4
  • 5. A. Simple if statement Syntax of simple if statement: if (condition) { //These statements will only execute if the condition is true } Flowchart for simple if statement: 8/12/2021 5
  • 6. Example for simple if statement: #include <stdio.h> #include <conio.h> int main() { int x = 20; int y = 22; if (x<y) { printf(" %d is less than %d",x,y); } return 0; getch(); } 8/12/2021 6
  • 7. B. If …. else Statement Syntax of if...else statement: if (testexpression) { // statementsto be executed if the test expression is true } else { // statementsto be executed if the test expression is false } 8/12/2021 7
  • 8. Example for if...else statement: // Check whether an integer is odd or even using if…. Else statement #include <stdio.h> int main() { int num; printf("Enteran integer: "); scanf("%d", &num); if (num%2 == 0) { printf("%dis an even integer.",num); } else { printf("%dis an odd integer.",num); } return 0; } 8/12/2021 8
  • 9. C. Nested if...else statement Syntax of Nested if...else statement: if(condition) { if(condition2) { //Statement1 } else { //Statement2 } } else { //Statement3 } 8/12/2021 9
  • 10. Flowchart of Nested if...else statement: 8/12/2021 10
  • 11. Example of Nested if...else statement: /* C Program to find given number is positive or negative using nested if ...else */ #include <stdio.h> #include <conio.h> int main() { float num; printf("Enter a number: "); scanf("%f",&num); if (num <= 0) { if (num == 0) printf("Youentered 0."); else printf("%fis a negative number.",num); } else printf("%fis a positive number.",num); return 0; getch(); } 8/12/2021 11
  • 12. // Check Largest number among two given numbers #include <stdio.h> main() { int a, b; printf("Enter two numbers: n"); scanf("%d %d", &a, &b); if(a >= b) { if (a==b) { printf("Both number are equaln"); } else { printf("%d is Largestn", a); } } else { printf("%d is Largestn", b); } } 8/12/2021 12
  • 13. D. Else if ladder Syntax of else if ladder statement: if (condition1) { //These statements would execute if the condition1 is true } else if(condition2) { //These statements would execute if the condition2 is true } else if (condition3) { //These statements would execute if the condition3 is true } . . else { //These statements would execute if all the conditions return false. } 8/12/2021 13
  • 14. Flowchart of else if ladder statement: 8/12/2021 14
  • 15. Example of else if ladder statement: // Check Largest number among two given numbers #include <stdio.h> int main() { int a, b; printf("Enter two numbers: n"); scanf("%d %d", &a, &b); if(a == b) { printf("Both numbers are equal“,a,b); } else if (a > b) { printf(" %d is largest than %d", a, b); } else { printf(" %d is largest than %d", b, a); } return 0; } 8/12/2021 15
  • 16. II. Switch Statement The switch statement allows us to execute one code block among many alternatives. 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. If a case match is not found, then the default statement is executed, and the control goes out of the switch block. Syntax of switch statement: switch (expression) ​{ case constant1: // statements break; case constant2: // statements break; . . . default: // default statements } 8/12/2021 16
  • 17. flowchart of switch statement 8/12/2021 17
  • 18. #include <stdio.h> int main() { int num=2; switch(num) { case 1: printf("Case1:Value is: %d", num); break; case 2: printf("Case2:Value is: %d", num); break; case 3: printf("Case3:Value is: %d", num); break; default: printf("Default:Value is: %d", num); } return 0; } 8/12/2021 18
  • 19. #include <stdio.h> main() { int a, b; char ch; printf("What do you want to perform?? n "); printf(" Enter first letter: A for add, S for subtract, M for multiply, or D for divide?n"); printf("Your choice: "); ch = getchar(); printf("Enter first number: "); scanf("%d", &a); printf("Enter second number: "); scanf("%d", &b); switch(ch) { case 'A': printf("%d", a + b); break; case 'S': printf("%d", a - b); break; case 'M': printf("%d", a * b); break; case 'D': if(b!=0) printf("%d", a / b); break; default: printf("choose properly") ; } } 8/12/2021 19
  • 20. III. Conditional(ternary ) statement If any operator is used on three operands or variable is known as Ternary Operator. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. Ternary operator is shortened way of writing an if-else statement. syntax: (condition) ? expression-2 : expression-3 8/12/2021 20
  • 21. //example program to illustrate the use of ternary statements #include<stdio.h> main() { int a, b, c, large; printf("Enter any three number: "); scanf("%d%d%d",&a,&b,&c); large=a>b ? (a>c?a:c) : (b>c?b:c); printf("Largest Number is: %d", large); } 8/12/2021 21
  • 22. 2. Loop statements Loop statement: loops are used to repeat a block of code until a specified condition is met. Following statements are used to perform repetitive task: I. for loop II. while loop III. do while loop 8/12/2021 22
  • 33. III. do while loop 8/12/2021 33
  • 36. 3 . Jumping statement There are three different controls used to jump from one C program statement to another and make the execution of the programming procedure fast. These three Jumping controls are: A. go to statement B. break statement C. continue statement 8/12/2021 36
  • 37. A. go to statement The go to moves the control on a specified address called label or label name. Syntax: goto label_name; .. .. label_name: C-statements Example program of go to statement: #include <stdio.h> main() { int sum=0; for(int i = 0; i<=10; i++) { sum = sum+i; if(i==5) { goto addition; } } addition: printf("%d", sum); } 8/12/2021 37
  • 38. B. break statement Break is always used with the decision-makingstatementlike if and switch statements. The statementwill quit from the loop when the conditionis true. Example program using break statement #include< stdio.h> main() { int i=1; while(i<=10) { if(i==6) { break; } printf("n I=%d",i); i++; } } 8/12/2021 38
  • 39. C. continue statement This statementis skip some part of iteration (loop) and comes to the next looping step i.e. it will increment / decrement the loop value, when continue occurs. Example program using continue statement #include< stdio.h> main() { int i=1; while(i<=10) { if(i==6) { i++; continue; } printf("n I=%d",i); i++; } } 8/12/2021 39
  • 40. Assignment 4(submit up to 22 Jan) 1. WAP to input an integer and check if it is palindrome or not. 2. Differentiate between • pre-test and post-test loop. • Break and continue statement • Switch and nested if else 3. WAP to generate all prime numbers from 1 to 100. 4. WAP to evaluate the following power series: ex =1+x/1!+x2/2!+x3/3!+…….xn/n!, 0<x<1 3. WAP to display the following output : 5 5 4 5 4 3 8/12/2021 40