SlideShare a Scribd company logo
1 of 42
GANDHINAGAR INSTITUTE
OF TECHNOLOGY
SUBJECT : COMPUTER PROGRAMMING AND
UTILIZATION(2110003)
TOPIC : “Control Structures in C .”
Guided by : Prof. Archana Singh
MADE BY:-JAYDEV PATEL(150120119127)
KRUNAL PATEL(150120119128)
JAIMIN PATEL(150120119126)
CONTENT
• Introduction
• Decision Making Statement
i. If else statement
ii. Ladder if else
iii. Switch statement
• Looping
i. While loop
ii. do-while loop
iii. For loop
iv. Nesting loop
v. Flow breaking statement
Introduction
• The important and essential part of a
programming language is their control
structures.
1. Sequence
2. Decision
3. Looping
Decision making statement
• This is same as choosing one of the alternatives
out of the two alternatives available.
• There are various types of decision making
statements:-
i. If else statement
ii. Ladder if-else
iii. Switch
iv. goto
If-else statement
• The syntax of if else statement is:
if(condition)
statement 1;
else
statement 2;
Flow chart of if else statement
If-else statement
• Here the condition is represented by relational or
logical expression.
• If condition is true, then statement 1 is executed and if
condition is false statement 2 is executed.
• A compound statement must be enclosedin pair of
braces{}.
• Enclosing in a single statement is not compulsory.
• The else part is is optional. The if statement without
else looks like:-
if(condition)
statement;
Write a program to print minimum of
two integers.
#include<stdio.h>
Void main()
{
Int x,y,min;
Printf(“enter two integers”);
Scanf(“%d %d”,&x,&y);
If(x<y)
min=x;
Else
min=y;
Printf(“minimum is:%dn”,min);
}
• Output:-
enter two integers
10 6
Minimum is :6
Ladder if-else
• The if else is used for two way decision where we select one of the
alternative.
• But for more than that c supports more multiple if-else or ladder if else for
this purpose.
• The syntax of multiple if else is:-
• If(condition 1)
statement1;
else if(condition2)
statement2;
.
.
else if(conditionN)
statementN;
else
default_statement;
Ladder if else
• The condition1,condition2,…,conditionN are
represented in logical or relational
expression s .
• if the first condition is true than statement1
will be executed or the other conditions will
be evaluated till the correct condition is
found.
• If the true condition is not found than at last
the default statement will be executed.
Write a program to compute the
electricity bill.
#include<stdio.h>
Void main()
{
Int units;
Float ucharge,fcharge,gtax,meter_rent= 25.0,total;
Printf(“enter number of units consumed”);
Scanf(“%d”,units);
If(units<=50)
Ucharge=0.5*units;
Else if(units<=100)
Ucharge=50*0.5+(units-50)*0.75;
Else if(units<=200)
Ucharge=50*0.5+50*0.75+(units-100)*1.00;
Else
Ucharge=50*0.5+50*0.75+100*1.00+(units-200)*1.50;
Fcharge=0.40*ucharge;
Gtax=0.10*(ucharge+fcharge);
Total=ucharge+fcharge+gtax+meter_rent;
Printf(“total bill: %fn”,total);
}
Output
Enter the value of units consumed
85
Total bill : 103.925003
Switch statement
• The switch in C is a multi-choice statement.
• It provides one choice for each value of variable expression.
• The syntax of switch is given below:
Switch(variable) {
Case label1: statement_block1;
break;
Case label2: statement_block2;
break;
.
.
default: default_block
Switch statement
• Lebel1 ,label2,…. Show the possible value of
the variable or expression.
• After every statement block there is a break.
• Break sends control to the next switch
statement.
• The last choice is default which is chosen
when the value of expression does not match
to any of the expression.
• But default is optional.
Write a program to perform arithmetic
operations.
#include<stdio.h>
#include<conio.h>
Void main()
{
Int choice,a,b;
Clrscr();
Printf(“1. add n”);
Printf(“2. subn”);
Printf(“3. muln”);
Printf(“4. divn”);
Printf(“Enter your choice:”);
Scanf(“%d”,&choice);
Printf(“enter two numbers”);
Scanf(“%d %d “,&a,&b);
Switch(choice) {
Case 1: printf(“answer=%dn”,a+b);
break;
Case 2: printf(“answer=%dn”,a-b);
break;
Case 3: printf(“answer=%dn”,a*b);
break;
Case4 : printf(“answer=%dn”a/b);
break;
Default: break;
}
}
Output
1. Add
2. Sub
3. Mul
4. Div
Enter your choice :1
Enter two numbers
23 35
Answer =58
Goto statement
• The goto statement in C programming is used
to transfer the control unconditionally from
one part of program to another.
• The goto statement can be dangerous to
programming languages.
• The syntax is
Goto label;
Write a program to print the sum of 1
to N using goto statement.
#include<stdio.h>
Void main()
{
Int i=1,sum=0,n;
Printf(“Enter value of N”);
Scanf(“%d”,&n);
next:
Sum= sum+I;
i+ =1;
If(I <= n )
Goto next;
Printf(“sum = %dn”,sum);
}
Output
Enter the value of N
4
Sum = 10
Looping
• A looping is an important control structure in
higher level programming language.
• The objective of looping is to provide
repetition of the part of the program i.e. block
of statements in a program, number of times.
Types of loop
• There are two types of loop:
1. Entry control
2. Exit control
• In entry control loop the condition is checked
first and then the body is executed, while in
exit control the body is excuted first and
then the condition is checked.
While loop
• The syntax of while loop is:
While(condition)
{
body;
}
While loop
• Here the condition is evaluated first and then
the body is executed. After executing the body
the condition is evaluated again and if it is
true body is executed again. This process is
repeated as long as the condition is true.
• While loop is a entry control loop.
• It is not necessary to enclose body of loop in
pair of {} if body contains single statement.
Write program to compute the sum of
following series.
1²+2²+…+10²
#include<stdio.h>
Void main()
{
Int sum=0,n=1;
While(n<=10)
{
sum = = n*n;
n++;
}
Printf(“sum of series :%dn”,sum);
}
Output
Sum of series : 385
Do-while loop
• The syntax of do-while loop is:
do{
Body;
} while (condition);
Do-while loop
• In do-while first the body is executed and then
the condition is checked. If the condition is
true the body is again executed. The body is
executed as long as the condition is true.
• Do-while loop is a exit control loop.
• This ensures the body is atleast once executed
even if the condition is false.
For loop
• The syntax of for loop is :
For(<e1>;<e2>;<e3>)
{
body;
}
For loop
• <e1> is initialization of variable by initial
values.<e1> executes only once when we
enter in loop. If they are more than one they
should be separated by commas.
• <e2> is the condition and the if it is true, then
the control enters into the body of the loop
and executes the statement in body.
• After executing the body of the loop, <e3> is
executed which modifies the variable.
Write a program to print the
multiplication table for a given number
N.
#include<stdio.h>
Void main()
{
Int I,n;
Printf(“enter a number”);
Scanf(“%d”,&n);
Printf(“Table->n”);
For(i=1,1<=10;i++)
Printf(“%4d*%2d=%4dn’,n,I,n*i);
}
Output
Enter a number
3
Table->
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30
Nesting of loops
• Loop inside loop is called nested loops. For example,
…
…
For( )
{
…..
for( )
{
…….
……..
}
…….
}
…….
…….
Nesting of loops
Write a program to print the
triangle.for n=5
#include<stdio.h>
Void main()
{
Int n,l,j;
Printf(“enter number of lines”);
Scanf(“%d”,&n);
Printf(“triangle ->n”);
For(l=1;l<=n;l++)
{for(j=1;j<=1;j++)
Printf(“%d”,j);
Printf(“n”);
}
}
Output
Enter number of lines
5
Triangle->
1
12
123
1234
12345
Thank you

More Related Content

What's hot

Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 

What's hot (20)

Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Control structure
Control structureControl structure
Control structure
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in c
 
Behavioral modeling
Behavioral modelingBehavioral modeling
Behavioral modeling
 
Control statement-Selective
Control statement-SelectiveControl statement-Selective
Control statement-Selective
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 

Similar to computer programming and utilization

JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 

Similar to computer programming and utilization (20)

Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Session 3
Session 3Session 3
Session 3
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
NRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptxNRG 106_Session 4_CLanguage.pptx
NRG 106_Session 4_CLanguage.pptx
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
C_Language_PS&PC_Notes.ppt
C_Language_PS&PC_Notes.pptC_Language_PS&PC_Notes.ppt
C_Language_PS&PC_Notes.ppt
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Lec 10
Lec 10Lec 10
Lec 10
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 

More from JAYDEV PATEL (10)

SUPERCONDUCTORS
SUPERCONDUCTORSSUPERCONDUCTORS
SUPERCONDUCTORS
 
PARALINGUISTICS
PARALINGUISTICSPARALINGUISTICS
PARALINGUISTICS
 
LINEAR ALGEBRA AND VECTOR CALCULUS
LINEAR ALGEBRA AND VECTOR CALCULUSLINEAR ALGEBRA AND VECTOR CALCULUS
LINEAR ALGEBRA AND VECTOR CALCULUS
 
ELEMENTS OF ELECTRICAL ENGINEERING
ELEMENTS OF ELECTRICAL ENGINEERINGELEMENTS OF ELECTRICAL ENGINEERING
ELEMENTS OF ELECTRICAL ENGINEERING
 
Modes of transportation
Modes of transportationModes of transportation
Modes of transportation
 
WATER AND WASTE WATER MANAGEMENT
WATER AND WASTE WATER MANAGEMENTWATER AND WASTE WATER MANAGEMENT
WATER AND WASTE WATER MANAGEMENT
 
SCALE
SCALESCALE
SCALE
 
INTEGRAL TEST, COMPARISON TEST, RATIO TEST AND ROOT TEST
INTEGRAL TEST, COMPARISON TEST, RATIO TEST AND ROOT TESTINTEGRAL TEST, COMPARISON TEST, RATIO TEST AND ROOT TEST
INTEGRAL TEST, COMPARISON TEST, RATIO TEST AND ROOT TEST
 
Components of pneumatic system
Components of pneumatic systemComponents of pneumatic system
Components of pneumatic system
 
Components of hydraulic system
Components of hydraulic systemComponents of hydraulic system
Components of hydraulic system
 

Recently uploaded

Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Recently uploaded (20)

(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
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
 

computer programming and utilization

  • 1. GANDHINAGAR INSTITUTE OF TECHNOLOGY SUBJECT : COMPUTER PROGRAMMING AND UTILIZATION(2110003) TOPIC : “Control Structures in C .” Guided by : Prof. Archana Singh MADE BY:-JAYDEV PATEL(150120119127) KRUNAL PATEL(150120119128) JAIMIN PATEL(150120119126)
  • 2. CONTENT • Introduction • Decision Making Statement i. If else statement ii. Ladder if else iii. Switch statement • Looping i. While loop ii. do-while loop iii. For loop iv. Nesting loop v. Flow breaking statement
  • 3. Introduction • The important and essential part of a programming language is their control structures. 1. Sequence 2. Decision 3. Looping
  • 4. Decision making statement • This is same as choosing one of the alternatives out of the two alternatives available. • There are various types of decision making statements:- i. If else statement ii. Ladder if-else iii. Switch iv. goto
  • 5. If-else statement • The syntax of if else statement is: if(condition) statement 1; else statement 2;
  • 6. Flow chart of if else statement
  • 7. If-else statement • Here the condition is represented by relational or logical expression. • If condition is true, then statement 1 is executed and if condition is false statement 2 is executed. • A compound statement must be enclosedin pair of braces{}. • Enclosing in a single statement is not compulsory. • The else part is is optional. The if statement without else looks like:- if(condition) statement;
  • 8. Write a program to print minimum of two integers. #include<stdio.h> Void main() { Int x,y,min; Printf(“enter two integers”); Scanf(“%d %d”,&x,&y); If(x<y) min=x; Else min=y; Printf(“minimum is:%dn”,min); } • Output:- enter two integers 10 6 Minimum is :6
  • 9. Ladder if-else • The if else is used for two way decision where we select one of the alternative. • But for more than that c supports more multiple if-else or ladder if else for this purpose. • The syntax of multiple if else is:- • If(condition 1) statement1; else if(condition2) statement2; . . else if(conditionN) statementN; else default_statement;
  • 10.
  • 11. Ladder if else • The condition1,condition2,…,conditionN are represented in logical or relational expression s . • if the first condition is true than statement1 will be executed or the other conditions will be evaluated till the correct condition is found. • If the true condition is not found than at last the default statement will be executed.
  • 12. Write a program to compute the electricity bill. #include<stdio.h> Void main() { Int units; Float ucharge,fcharge,gtax,meter_rent= 25.0,total; Printf(“enter number of units consumed”); Scanf(“%d”,units); If(units<=50) Ucharge=0.5*units; Else if(units<=100) Ucharge=50*0.5+(units-50)*0.75; Else if(units<=200) Ucharge=50*0.5+50*0.75+(units-100)*1.00; Else Ucharge=50*0.5+50*0.75+100*1.00+(units-200)*1.50; Fcharge=0.40*ucharge; Gtax=0.10*(ucharge+fcharge); Total=ucharge+fcharge+gtax+meter_rent; Printf(“total bill: %fn”,total); }
  • 13. Output Enter the value of units consumed 85 Total bill : 103.925003
  • 14. Switch statement • The switch in C is a multi-choice statement. • It provides one choice for each value of variable expression. • The syntax of switch is given below: Switch(variable) { Case label1: statement_block1; break; Case label2: statement_block2; break; . . default: default_block
  • 15.
  • 16. Switch statement • Lebel1 ,label2,…. Show the possible value of the variable or expression. • After every statement block there is a break. • Break sends control to the next switch statement. • The last choice is default which is chosen when the value of expression does not match to any of the expression. • But default is optional.
  • 17. Write a program to perform arithmetic operations. #include<stdio.h> #include<conio.h> Void main() { Int choice,a,b; Clrscr(); Printf(“1. add n”); Printf(“2. subn”); Printf(“3. muln”); Printf(“4. divn”); Printf(“Enter your choice:”); Scanf(“%d”,&choice); Printf(“enter two numbers”); Scanf(“%d %d “,&a,&b); Switch(choice) { Case 1: printf(“answer=%dn”,a+b); break; Case 2: printf(“answer=%dn”,a-b); break; Case 3: printf(“answer=%dn”,a*b); break; Case4 : printf(“answer=%dn”a/b); break; Default: break; } }
  • 18. Output 1. Add 2. Sub 3. Mul 4. Div Enter your choice :1 Enter two numbers 23 35 Answer =58
  • 19. Goto statement • The goto statement in C programming is used to transfer the control unconditionally from one part of program to another. • The goto statement can be dangerous to programming languages. • The syntax is Goto label;
  • 20. Write a program to print the sum of 1 to N using goto statement. #include<stdio.h> Void main() { Int i=1,sum=0,n; Printf(“Enter value of N”); Scanf(“%d”,&n); next: Sum= sum+I; i+ =1; If(I <= n ) Goto next; Printf(“sum = %dn”,sum); }
  • 21. Output Enter the value of N 4 Sum = 10
  • 22. Looping • A looping is an important control structure in higher level programming language. • The objective of looping is to provide repetition of the part of the program i.e. block of statements in a program, number of times.
  • 23. Types of loop • There are two types of loop: 1. Entry control 2. Exit control • In entry control loop the condition is checked first and then the body is executed, while in exit control the body is excuted first and then the condition is checked.
  • 24.
  • 25. While loop • The syntax of while loop is: While(condition) { body; }
  • 26.
  • 27. While loop • Here the condition is evaluated first and then the body is executed. After executing the body the condition is evaluated again and if it is true body is executed again. This process is repeated as long as the condition is true. • While loop is a entry control loop. • It is not necessary to enclose body of loop in pair of {} if body contains single statement.
  • 28. Write program to compute the sum of following series. 1²+2²+…+10² #include<stdio.h> Void main() { Int sum=0,n=1; While(n<=10) { sum = = n*n; n++; } Printf(“sum of series :%dn”,sum); }
  • 30. Do-while loop • The syntax of do-while loop is: do{ Body; } while (condition);
  • 31.
  • 32. Do-while loop • In do-while first the body is executed and then the condition is checked. If the condition is true the body is again executed. The body is executed as long as the condition is true. • Do-while loop is a exit control loop. • This ensures the body is atleast once executed even if the condition is false.
  • 33. For loop • The syntax of for loop is : For(<e1>;<e2>;<e3>) { body; }
  • 34.
  • 35. For loop • <e1> is initialization of variable by initial values.<e1> executes only once when we enter in loop. If they are more than one they should be separated by commas. • <e2> is the condition and the if it is true, then the control enters into the body of the loop and executes the statement in body. • After executing the body of the loop, <e3> is executed which modifies the variable.
  • 36. Write a program to print the multiplication table for a given number N. #include<stdio.h> Void main() { Int I,n; Printf(“enter a number”); Scanf(“%d”,&n); Printf(“Table->n”); For(i=1,1<=10;i++) Printf(“%4d*%2d=%4dn’,n,I,n*i); }
  • 38. Nesting of loops • Loop inside loop is called nested loops. For example, … … For( ) { ….. for( ) { ……. …….. } ……. } ……. …….
  • 40. Write a program to print the triangle.for n=5 #include<stdio.h> Void main() { Int n,l,j; Printf(“enter number of lines”); Scanf(“%d”,&n); Printf(“triangle ->n”); For(l=1;l<=n;l++) {for(j=1;j<=1;j++) Printf(“%d”,j); Printf(“n”); } }
  • 41. Output Enter number of lines 5 Triangle-> 1 12 123 1234 12345