SlideShare uma empresa Scribd logo
1 de 45
Content
1. Decision Making
2. Loops
3. Break and Continue Statement
4. Switch… case Statement
5. goto and label Statement
1.Decision Making
Decision making is used to
specify the order in which
statements are executed.
• Decision making in a Cprogram using:
• if statement
• if…else statement
• if…else if…else statement
• nested if...else statement
• Switch case Statement
1.1 if statement
if (testExpression)
{
/ / statements
}
Example:if statement
/ / Program to display a number if user enters negativenumber
/ / If user enters positive number, that number won't be displayed
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
/ / Test expression is true if number is lessthan 0
if (number < 0){
}
printf("You entered %d.n", number);
printf("The if statement is easy.");
}
return 0;
1.2 if...else statement
The if...else statement executes some code if the test expression
is true (nonzero) and some other code if the test expression is
false (0).
Syntax of if...else
if (testExpression) {
/ / codes inside the body of if
}else {
/ / codes inside the body of else
}
Example: if...else statement
/ / 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 is0
if( number%2 == 0)
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
1.3 if...else if....else Statement
The if...else statement executes two different codes
depending upon whether the test expression is true or false.
Sometimes, a choice has to be made from more than 2
possibilities.
The if...else if…else statement allows you to check for
multiple test expressions and execute different codes for
more than two conditions.
Syntax of if...else if....else statement.
if (testExpression1) {
/ / statements to be executed if testExpression1 istrue
} else if(testExpression2) {
/ / statements to be executed if testExpression1 is false and
testExpression2 is true
} else if (testExpression 3) {
/ / statements to be executed if testExpression1and
testExpression2 is false and testExpression3 is true
} else {
/ / statements to be executed if all test expressions are false
}
Example: if...else if....else statement
using =, > or <
#include <stdio.h>
int main(){
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1,
&number2);
//checks if two integers are
equal.
if(number1 == number2){
printf("Result: %d = %d“,
number1,number2);
}
// Program to relatetwo integers //checks if number1 is greater than
number2.
else if (number1 > number2){
printf("Result: %d > %d",
number1, number2);
}
// if both test expressionis false
else {
printf("Result: %d < %d",
number1, number2);
}
return 0;
}
1.4 Nested if elsestatement
Nested if else statement is same like if else
statement, where new block of if else statement is
defined in existing if or else block statement.
Used when user want to check more than one
conditions at a time.
Syntax of Nested If else Statement
if(condition is true){
if(condition is true){
statement;
}else{
statement;
}
}else{
statement;
}
Example of Nested if elseStatement
#include <stdio.h>
void main(){
char username;
int password;
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password);
if(username=='a'){
if(password==12345){
}else{
printf("Login successful");
printf("Password is incorrect, Try again.");
}else{
}
}
printf("Username is incorrect, Try again.");
}
2.Loops
Loops are used in programming to repeat a specific block until some end
condition is met.
There are three loops in C programming:
o for loop
o while loop
o do...while loop
o Nested loops
2.1 for Loop
The syntax of a for loop is:
for (initializationStatement; testExpression; updateStatement)
{
/ / codes
}
for loop Flowchart
Example: for loop
/ / Program to calculate thesum of
first n natural numbers
/ / Positive integers 1,2,3...nare
known asnatural numbers
#include <stdio.h>
int main(){
int n, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// for loop terminates when nis less
than count
for(count = 1; count <= n; ++count)
{
sum += count;
}
printf("Sum = %d",sum);
return 0;
}
2.2 while loop
The syntax of a while loop is:
while (testExpression)
{
//codes
}
Example: while loop
/ Program to find factorial of
a number
/ / For a positive integern,
factorial = 1*2*3...n
#include <stdio.h>
int main(){
int number;
long factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
th
// loop terminates when number is less
an or equal to 0
while (number > 0){
// factorial = factorial*number;
factorial *= number;
--number;
}
printf("Factorial= %lld", factorial);
return 0;
}
2.3 do...while loop
The do..while loop is similar to the while loop with one
important difference.
The body of do...while loop is executed once, before
checking the test expression.
The do...while loop is executed at least once.
do...while loop Syntax
The syntax of a do while loop is:
do
{
/ / codes
}
while (testExpression);
Example: do...while loop
// Program to add numbers until user enters zero
#include <stdio.h>
int main() {
double number, sum = 0;
// loop body is executed at leastonce
do{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
Nested loops
Cprogramming allows to use one loop insideanother
loop.
Syntax for loop
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
2.4 Nested loops (Con)
Syntax while loop
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
2.4 Nested loops (Con)
Syntax do while loop
do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition );
Exampleof Nested Loops
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter number of rowsn");
scanf("%d",&n);
for ( c = 1 ; c <= n ; c++ ){
for( k = 1 ; k <= c;k++ )
printf("*");
printf("n");
}
}
return 0;
3.Break And Continue Statement
What is BREAK meant?
What is CONTINUE meant?
3.1 Break Statement
The break statement terminates the loop immediately when
it is encountered.
The break statement is used with decision making
statement such as if...else.
Syntax of break statement
break;
Flowchart Of Break Statement
How break statement works?
Example: break statement
• enters positive
number # include
<stdio.h>
• int main() {
• int i;
• double number, sum = 0.0;
for(i=1; i <= 10; ++i) {
• printf("Enter a n%d: ",i);
scanf("%lf",&number);
• number, loop is
terminated if(number
< 0.0){
• break;
• }
• // sum = sum +
number; sum +=
number;
• }
• printf("Sum = %.2lf",sum);
return 0;
• }
// Program to calculate thesum
of maximum of 10numbers
// Calculates sum until user
/ / If user enters negative
3.2 Continue Statement
The continue statement skips some statements inside the loop.
The continue statement is used with decision making statement
such as if...else.
Syntax of continue Statement
continue;
Flowchart of Continue Statement
How Continue Statement Works?
Example: continue statement
// Program to calculate sumof
maximum of 10 numbers
// Negative numbers areskipped
from calculation
# include <stdio.h>
int main(){
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i) {
printf("Enter a n%d: ",i);
scanf("%lf",&number);
/ / If user enters negative
number, loop is terminated
if(number < 0.0){
continue;
}
// sum = sum + number;
sum += number;
}
printf("Sum = %.2lf",sum);
return 0;
}
4. Switch...Case Statement
The if...else if…else statement allows you to execute a block code among
many alternatives. If you are checking on the value of a single variable in
if...else if…else statement, it is better to use switch statement.
The switch statement is often faster than nested if...else (not always).
Also, the syntax of switch statement is cleaner and easy to understand.
Syntaxof switch...case
switch (n){
case constant1:
/ / code to be executed if n is equal toconstant1;
break;
case constant2:
/ / code to be executed if n is equal toconstant2;
break;
.
.
.
default:
/ / code to be executed ifn doesn't match any constant
}
Switch Statement Flowchart
Example: switch Statement
// Program to create a simplecalculator
/ / Performs addition, subtraction, multiplication ordivision
depending the input from user
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator) {
case
'+':
printf("%.1lf +%.1lf=%.1lf",firstNumber
, secondNumber, firstNumber+secondNumber);
break;
case '-':
printf("%.1lf - %.1lf=%.1lf",firstNumber
, secondNumber, firstNumber-secondNumber);
break;
case '*':
printf("%.1lf *%.1lf=%.1lf",firstNumber
, secondNumber, firstNumber*secondNumber);
break;
case '/':
printf("%.1lf / %.1lf=%.1lf",firstNumber
, secondNumber, firstNumber/firstNumber);
break;
// operator is doesn't match any case constant (+,-, *,/)
default:
printf("Error! operator is not correct");
}
return 0;}
5. goto Statement
The goto statement is used to alter the normal sequence of aC
program.
Syntaxof goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
What is Label?
The label is an identifier. When goto statement is encountered,
control of the program jumps to label: and starts executing thecode.
Example:goto Statement
• previously entered positive number is displayed
• # include <stdio.h> int main(){
• const int maxInput = 5; int i;
• double number, average, sum=0.0;
• for(i=1; i<=maxInput; ++i){ printf("%d. Enter a
number: ", i); scanf("%lf",&number);
/ / Program to calculate the sum and average of maximum of 5
numbers
/ / If user enters negative number, the sum and averageof
ju
/ / If user enters negative number, flow of program moves to label
mp
if(number < 0.0)
goto jump;
sum += number; / / sum = sum+number;
}
jump:
average=sum/(i-1);
printf("Sum = %.2fn", sum);
printf("Average = %.2f",average);
return 0;
}
Example:goto Statement
C programming Control Structure.pptx

Mais conteúdo relacionado

Semelhante a C programming Control Structure.pptx

Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 

Semelhante a C programming Control Structure.pptx (20)

Branching statements
Branching statementsBranching statements
Branching statements
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
3. control statement
3. control statement3. control statement
3. control statement
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
Programming egs
Programming egs Programming egs
Programming egs
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
C programm.pptx
C programm.pptxC programm.pptx
C programm.pptx
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 

Mais de DEEPAK948083

introAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.pptintroAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.ppt
DEEPAK948083
 
SensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.pptSensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.ppt
DEEPAK948083
 
Chapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.pptChapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.ppt
DEEPAK948083
 
introDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.pptintroDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.ppt
DEEPAK948083
 
lect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.pptlect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.ppt
DEEPAK948083
 
applicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.pptapplicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.ppt
DEEPAK948083
 
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITYMOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
DEEPAK948083
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
DEEPAK948083
 
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
DEEPAK948083
 
block ciphermodes of operation.pptx
block ciphermodes of operation.pptxblock ciphermodes of operation.pptx
block ciphermodes of operation.pptx
DEEPAK948083
 

Mais de DEEPAK948083 (20)

turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.pptturban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
turban_ch07ch07ch07ch07ch07ch07dss9e_ch07.ppt
 
introAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.pptintroAdhocRoutingRoutingRoutingRouting-new.ppt
introAdhocRoutingRoutingRoutingRouting-new.ppt
 
SensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.pptSensorSensorSensorSensorSensorSensor.ppt
SensorSensorSensorSensorSensorSensor.ppt
 
Chapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.pptChapter1_IntroductionIntroductionIntroduction.ppt
Chapter1_IntroductionIntroductionIntroduction.ppt
 
introDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.pptintroDMintroDMintroDMintroDMintroDMintroDM.ppt
introDMintroDMintroDMintroDMintroDMintroDM.ppt
 
lect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.pptlect1lect1lect1lect1lect1lect1lect1lect1.ppt
lect1lect1lect1lect1lect1lect1lect1lect1.ppt
 
Chchchchchchchchchchchchchchchchc 11.pptx
Chchchchchchchchchchchchchchchchc 11.pptxChchchchchchchchchchchchchchchchc 11.pptx
Chchchchchchchchchchchchchchchchc 11.pptx
 
applicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.pptapplicationapplicationapplicationapplication.ppt
applicationapplicationapplicationapplication.ppt
 
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITYMOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
MOBILE & WIRELESS SECURITY And MOBILE & WIRELESS SECURITY
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
 
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
5virusandmaliciouscodechapter5-130716024935-phpapp02-converted.pptx
 
Lect no 13 ECC.ppt
Lect no 13 ECC.pptLect no 13 ECC.ppt
Lect no 13 ECC.ppt
 
block ciphermodes of operation.pptx
block ciphermodes of operation.pptxblock ciphermodes of operation.pptx
block ciphermodes of operation.pptx
 
Lect no 13 ECC.ppt
Lect no 13 ECC.pptLect no 13 ECC.ppt
Lect no 13 ECC.ppt
 
unit1Intro_final.pptx
unit1Intro_final.pptxunit1Intro_final.pptx
unit1Intro_final.pptx
 
whitman_ch04.ppt
whitman_ch04.pptwhitman_ch04.ppt
whitman_ch04.ppt
 
lesson333.ppt
lesson333.pptlesson333.ppt
lesson333.ppt
 
ICS PPT Unit 4.ppt
ICS PPT Unit 4.pptICS PPT Unit 4.ppt
ICS PPT Unit 4.ppt
 
stack-Intro.pptx
stack-Intro.pptxstack-Intro.pptx
stack-Intro.pptx
 
BST.ppt
BST.pptBST.ppt
BST.ppt
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Último (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

C programming Control Structure.pptx

  • 1. Content 1. Decision Making 2. Loops 3. Break and Continue Statement 4. Switch… case Statement 5. goto and label Statement
  • 2. 1.Decision Making Decision making is used to specify the order in which statements are executed. • Decision making in a Cprogram using: • if statement • if…else statement • if…else if…else statement • nested if...else statement • Switch case Statement
  • 3. 1.1 if statement if (testExpression) { / / statements }
  • 4. Example:if statement / / Program to display a number if user enters negativenumber / / If user enters positive number, that number won't be displayed #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); / / Test expression is true if number is lessthan 0 if (number < 0){ } printf("You entered %d.n", number); printf("The if statement is easy."); } return 0;
  • 5. 1.2 if...else statement The if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0). Syntax of if...else if (testExpression) { / / codes inside the body of if }else { / / codes inside the body of else }
  • 6. Example: if...else statement / / 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 is0 if( number%2 == 0) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); return 0; }
  • 7. 1.3 if...else if....else Statement The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else if…else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
  • 8. Syntax of if...else if....else statement. if (testExpression1) { / / statements to be executed if testExpression1 istrue } else if(testExpression2) { / / statements to be executed if testExpression1 is false and testExpression2 is true } else if (testExpression 3) { / / statements to be executed if testExpression1and testExpression2 is false and testExpression3 is true } else { / / statements to be executed if all test expressions are false }
  • 9. Example: if...else if....else statement using =, > or < #include <stdio.h> int main(){ int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); //checks if two integers are equal. if(number1 == number2){ printf("Result: %d = %d“, number1,number2); } // Program to relatetwo integers //checks if number1 is greater than number2. else if (number1 > number2){ printf("Result: %d > %d", number1, number2); } // if both test expressionis false else { printf("Result: %d < %d", number1, number2); } return 0; }
  • 10. 1.4 Nested if elsestatement Nested if else statement is same like if else statement, where new block of if else statement is defined in existing if or else block statement. Used when user want to check more than one conditions at a time.
  • 11. Syntax of Nested If else Statement if(condition is true){ if(condition is true){ statement; }else{ statement; } }else{ statement; }
  • 12. Example of Nested if elseStatement #include <stdio.h> void main(){ char username; int password; printf("Username:"); scanf("%c",&username); printf("Password:"); scanf("%d",&password); if(username=='a'){ if(password==12345){ }else{ printf("Login successful"); printf("Password is incorrect, Try again."); }else{ } } printf("Username is incorrect, Try again."); }
  • 13. 2.Loops Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming: o for loop o while loop o do...while loop o Nested loops
  • 14. 2.1 for Loop The syntax of a for loop is: for (initializationStatement; testExpression; updateStatement) { / / codes }
  • 16. Example: for loop / / Program to calculate thesum of first n natural numbers / / Positive integers 1,2,3...nare known asnatural numbers #include <stdio.h> int main(){ int n, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); // for loop terminates when nis less than count for(count = 1; count <= n; ++count) { sum += count; } printf("Sum = %d",sum); return 0; }
  • 17. 2.2 while loop The syntax of a while loop is: while (testExpression) { //codes }
  • 18. Example: while loop / Program to find factorial of a number / / For a positive integern, factorial = 1*2*3...n #include <stdio.h> int main(){ int number; long factorial; printf("Enter an integer: "); scanf("%d",&number); factorial = 1; th // loop terminates when number is less an or equal to 0 while (number > 0){ // factorial = factorial*number; factorial *= number; --number; } printf("Factorial= %lld", factorial); return 0; }
  • 19. 2.3 do...while loop The do..while loop is similar to the while loop with one important difference. The body of do...while loop is executed once, before checking the test expression. The do...while loop is executed at least once.
  • 20. do...while loop Syntax The syntax of a do while loop is: do { / / codes } while (testExpression);
  • 21. Example: do...while loop // Program to add numbers until user enters zero #include <stdio.h> int main() { double number, sum = 0; // loop body is executed at leastonce do{ printf("Enter a number: "); scanf("%lf", &number); sum += number; }while(number != 0.0); printf("Sum = %.2lf",sum); return 0; }
  • 22. Nested loops Cprogramming allows to use one loop insideanother loop. Syntax for loop for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); }
  • 23. 2.4 Nested loops (Con) Syntax while loop while(condition) { while(condition) { statement(s); } statement(s); }
  • 24. 2.4 Nested loops (Con) Syntax do while loop do { statement(s); do { statement(s); }while( condition ); }while( condition );
  • 25. Exampleof Nested Loops #include <stdio.h> int main() { int n, c, k; printf("Enter number of rowsn"); scanf("%d",&n); for ( c = 1 ; c <= n ; c++ ){ for( k = 1 ; k <= c;k++ ) printf("*"); printf("n"); } } return 0;
  • 26. 3.Break And Continue Statement What is BREAK meant? What is CONTINUE meant?
  • 27. 3.1 Break Statement The break statement terminates the loop immediately when it is encountered. The break statement is used with decision making statement such as if...else. Syntax of break statement break;
  • 28. Flowchart Of Break Statement
  • 30. Example: break statement • enters positive number # include <stdio.h> • int main() { • int i; • double number, sum = 0.0; for(i=1; i <= 10; ++i) { • printf("Enter a n%d: ",i); scanf("%lf",&number); • number, loop is terminated if(number < 0.0){ • break; • } • // sum = sum + number; sum += number; • } • printf("Sum = %.2lf",sum); return 0; • } // Program to calculate thesum of maximum of 10numbers // Calculates sum until user / / If user enters negative
  • 31. 3.2 Continue Statement The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else. Syntax of continue Statement continue;
  • 34. Example: continue statement // Program to calculate sumof maximum of 10 numbers // Negative numbers areskipped from calculation # include <stdio.h> int main(){ int i; double number, sum = 0.0; for(i=1; i <= 10; ++i) { printf("Enter a n%d: ",i); scanf("%lf",&number); / / If user enters negative number, loop is terminated if(number < 0.0){ continue; } // sum = sum + number; sum += number; } printf("Sum = %.2lf",sum); return 0; }
  • 35. 4. Switch...Case Statement The if...else if…else statement allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in if...else if…else statement, it is better to use switch statement. The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
  • 36. Syntaxof switch...case switch (n){ case constant1: / / code to be executed if n is equal toconstant1; break; case constant2: / / code to be executed if n is equal toconstant2; break; . . . default: / / code to be executed ifn doesn't match any constant }
  • 38. Example: switch Statement // Program to create a simplecalculator / / Performs addition, subtraction, multiplication ordivision depending the input from user # include <stdio.h> int main() { char operator; double firstNumber,secondNumber; printf("Enter an operator (+, -, *,): "); scanf("%c", &operator); printf("Enter two operands: "); scanf("%lf %lf",&firstNumber, &secondNumber);
  • 39. switch(operator) { case '+': printf("%.1lf +%.1lf=%.1lf",firstNumber , secondNumber, firstNumber+secondNumber); break; case '-': printf("%.1lf - %.1lf=%.1lf",firstNumber , secondNumber, firstNumber-secondNumber); break; case '*': printf("%.1lf *%.1lf=%.1lf",firstNumber , secondNumber, firstNumber*secondNumber); break; case '/': printf("%.1lf / %.1lf=%.1lf",firstNumber , secondNumber, firstNumber/firstNumber); break; // operator is doesn't match any case constant (+,-, *,/) default: printf("Error! operator is not correct"); } return 0;}
  • 40. 5. goto Statement The goto statement is used to alter the normal sequence of aC program.
  • 41. Syntaxof goto Statement goto label; ... .. ... ... .. ... ... .. ... label: statement;
  • 42. What is Label? The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing thecode.
  • 43. Example:goto Statement • previously entered positive number is displayed • # include <stdio.h> int main(){ • const int maxInput = 5; int i; • double number, average, sum=0.0; • for(i=1; i<=maxInput; ++i){ printf("%d. Enter a number: ", i); scanf("%lf",&number); / / Program to calculate the sum and average of maximum of 5 numbers / / If user enters negative number, the sum and averageof
  • 44. ju / / If user enters negative number, flow of program moves to label mp if(number < 0.0) goto jump; sum += number; / / sum = sum+number; } jump: average=sum/(i-1); printf("Sum = %.2fn", sum); printf("Average = %.2f",average); return 0; } Example:goto Statement