SlideShare a Scribd company logo
1 of 44
Control Structure
• Decision Making Statements
Simple if
If else
Nested if else
Switch Statements (conditional branching)
• Loops
Do- while
While statements
For Statements
• Unconditional Branching
break statement
continue statement
goto statement
Given By Prof. Aparadh S.Y.
Simple if
• Syntax
Flow Diagram
if (expression is true)
{
action 1;
}
action 2;
action3;
Given By Prof. Aparadh S.Y.
Examples of Simple if
1. Print hello when number is 10
Given By Prof. Aparadh S.Y.
Examples of Simple if
1. Print hello when number is 10
Given By Prof. Aparadh S.Y.
#include<conio.h>
#include<stdio.h>
void main()
{
int num;
clrscr();
printf("Enter value for num“);
scanf(“%d”, &num);
if(num==10)
{
printf("hello)";
getch();
}
}
If Else
• Syntax
Flow Chart
if (expression is true)
{
action 1;
}
Else
{
action 2;
}
action3;
Given By Prof. Aparadh S.Y.
Examples of If Else
1. Print entered number is positive or negative
Given By Prof. Aparadh S.Y.
Examples of If Else
1. Print entered number is positive or negative
Given By Prof. Aparadh S.Y.
#include<conio.h>
#include<stdio.h>
void main()
{
int num;
clrscr();
printf(“Enter value for num“);
Scanf(“%d”,&num);
if(num>0)
{
Printf("num is positive“);
}
else
{
Printf("num is negative“)S;
}
getch();
}
Examples of If Else
1. Print entered number is even or odd
Given By Prof. Aparadh S.Y.
Examples of If Else
1. Print entered number is even or odd
Given By Prof. Aparadh S.Y.
#include<conio.h>
#include<stdio.h>
void main()
{
int num;
clrscr();
printf(“Enter value for num“);
Scanf(“%d”,&num);
if(num%2==0)
{
Printf("num is even“);
}
else
{
Printf("num is odd“)S;
}
getch();
}
Examples of If Else
1. Print entered year is leap year or not
Given By Prof. Aparadh S.Y.
Examples of If Else
1. Print entered year is leap year or not
Given By Prof. Aparadh S.Y.
#include<conio.h>
#include<stdio.h>
void main()
{
int num;
clrscr();
printf(“Enter value for year“);
Scanf(“%d”,&num);
if(num%4==0)
{
Printf(“year is leap year“);
}
else
{
Printf(“year is not leap year“)S;
}
getch();
}
Nested If Else Ladder
• Syntax
Flow Chart
Given By Prof. Aparadh S.Y.
Home Work of Nested
If Else
Given By Prof. Aparadh S.Y.
Write a program to input three numbers from user. Find maximum
between given three numbers.
Home Work of Nested If Else
Given By Prof. Aparadh S.Y.
Write a program to input three numbers from user. Find maximum between given three numbers.
#include<stdio.hh>
#include<conio.h>
void main()
{
int num1,num2,num3;
clrscr();
printf("Enter value for num1,num2 and num3“);
scanf(“%d%d%d”,&num1, &num2, &num3);
if(num1>num2)
{
if(num1>num3)
{
printf("num1 is greater“);
}
else
{
printf("num3 is greater“);
}
}
else
{
if(num2>num3)
{
printf("num2 is greater“);
}
else
{
printf(num3 is greater“);
}
}
getch();
}
 Output:
Enter value for num1 ,num2 and num3
4 2 1
Num1 is greater
 Output:
Enter value for num1 ,num2 and num3
1 4 2
Num2 is greater
 Output:
Enter value for num1 ,num2 and num3
1 2 3
Num3 is greater
If Else Ladder
Given By Prof. Aparadh S.Y.
In C,C++ if- else –if ladder helps user decide from among multiple
options .
Here if statements are executed from top down
As soon as one of the conditions controlling the if is true , the
statements associated with that if is executed, and rest of all conditions
are bypassed.
If none of the conditions is true then the final else statement will be
executed
If Else Ladder
• Syntax
Flow Chart
if (condition1)
{
statement 1;
}
else if (condition2)
{
statement 2;
}
.
.
else
{
statement;
}
Given By Prof. Aparadh S.Y.
Examples of If Else
1. Print entered number is positive, negative or zero.
Given By Prof. Aparadh S.Y.
Examples of If Else ladder
1. Print entered number is positive, negative or zero.
Given By Prof. Aparadh S.Y.
#include<stdio.hh>
#include<conio.h>
void main()
{
int num;
clrscr();
Printf(“Enter num“);
Scanf(“%d”, &num;
if(num>0)
{
Printf(“Positive“);
}
else if(num<0)
{
Printf(“Negative“);
}
else
{
Printf(“Zero“);
}
getch();
}
 Output:
Enter num
6
Positive
 Output:
Enter num
-3
Negative
 Output:
Enter num
0
Zero
Examples of If Else ladder
Write a program to calculate grade of students
Given By Prof. Aparadh S.Y.
Write a program to calculate grade of students
#include<iostream.h>
#include<conio.h>
void main()
{
int grade;
clrscr();
Printf("enter value grade“);
Scanf(“%d”, &grade);
if(grade>=70 && grade<=100)
{
Printf(“Distinction“);
}
else if(grade>=60 && grade<=69)
{
Printf("First Class“);
}
else if(grade>=45 && grade<=59)
{
Printf(“Second Class“);
}
Given By Prof. Aparadh S.Y.
else if(grade>=40 && grade<=44)
{
Printf("Pass Class“);
}
else if(grade>=0 && grade<40)
{
Printf("fail“);
}
else
{
Printf("invalid grade“);
Printf("nshould enter grade
inbetween 0 to 100“);
}
getch();
}
//Distinction grade>=70 &&
grade<=100
//First Class grade>=60 &&
grade<=69
//Second Class grade>=45 &&
grade<=59
//Pass Class grade>=40 &&
grade<=44
//Fail grade>=0 &&
grade <45
//Invalid grade>=100
&& grade<=0
Switch Statements
Syntax
Flow Chart
switch (expression)
{
case 1:
action 1;
break;
Case 2:
action 2;
break;
}
.
Case n:
action n;
break;
Default:
break;
}
Given By Prof. Aparadh S.Y.
1. Perform arithmetic operations using switch case
Examples Switch Case
Given By Prof. Aparadh S.Y.
1. .Perform arithmetic operations using switch case
Examples Switch Case
Given By Prof. Aparadh S.Y.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ch;
clrscr();
Printf("Enter value for a and b“);
Scanf(”%d%d”,&a,&b);
Printf("Enter choice“);
Scanf(“%d”,&ch)
switch(ch)
{
case 1:
scanf("Addition is=%d”, a+b);
break;
case 4:
scanf(“Division is=%d”, a/b);
break;
default:
printf("Enter valid choice“);
break;
}
getch();
}
case 2:
scanf(“Subtraction is=%d”, a-b);
break;
case 3:
scanf(“ Multiplication is=%d”, a*b);
break;
Do While Loop
• Syntax
Flow Diagram
Given By Prof. Aparadh S.Y.
do
{
action 1;
}
while(condition is true);
action 2;
Examples of Do While
Loop
1. Print 1 to 10 numbers using do while loop
Given By Prof. Aparadh S.Y.
Examples of Do While
Loop
1. Print 1 to 10 numbers using do while loop
Given By Prof. Aparadh
S.Y.
#include<stdio.h>
#include<conio.h>
Void main()
{
int a=1;
Clrscr();
do
{
printf(“n%d”,a);
a++;
} while(a<=10);
getch();
}
Output:
1
2
3
4
5
6
7
8
9
10
Examples of Do While
Loop
1. Print 10 to 1 numbers using do while loop
Given By Prof. Aparadh
S.Y.
Examples of Do While
Loop
1. Print 10 to 1 numbers using do while loop
Given By Prof. Aparadh
S.Y.
#include<stdio.h>
#include<conio.h>
Void main()
{
int a=10;
Clrscr();
do
{
printf(“n%d”,a);
a--;
} while(a>0);
getch();
}
Output:
10
9
8
7
6
5
4
3
2
1
• Syntax
Flow Chart
while (condition is true)
{
action 1;
}
action 2;
While Loop
Examples of While Loop
1. Print 1 to 10 numbers using while loop
Given By Prof. Aparadh
S.Y.
#include<stdio.h>
#include<conio.h>
Void main()
{
int a=1;
Clrscr();
While(a<=10)
{
printf(“n%d”,a);
a++;
}
getch();
}
Output:
1
2
3
4
5
6
7
8
9
10
Examples of While Loop
1. Print 10 to 1 numbers using while loop
Given By Prof. Aparadh
S.Y.
Examples of While Loop
1. Print 10 to 1 numbers using while loop
Given By Prof. Aparadh
S.Y.
#include<stdio.h>
#include<conio.h>
Void main()
{
int a=10;
Clrscr();
While(a>=10)
{
printf(“n%d”,a);
a--;
}
getch();
}
Output:
10
9
8
7
6
5
4
3
2
1
• Syntax
Flow Chart
for (initial value; condition; increment/decrement)
{
action 1;
}
action 2;
For Loop
Examples of for Loop
1. Print 10 to 1 numbers using for loop
Given By Prof. Aparadh
S.Y.
#include<stdio.h>
#include<conio.h>
Void main()
{
int a;
Clrscr();
For(a=10;a>0;a--)
{
printf(“n%d”,a);
a--;
}
getch();
}
Output:
10
9
8
7
6
5
4
3
2
1
• Syntax
Flow Chart
for (initial value; condition; increment/decrement)
{
action 1;
For((initial value; condition; increment/decrement)
{
body of inner loop;
}
action 2;
}
Nesting For Loop
Examples of nesting for Loop
Given By Prof. Aparadh
S.Y.
Program to print following pattern using nested for loop
* * * * * *
* * * * * *
* * * * * *
* * * * * *
# include<stdio.h>
#include<conio.h>
Void main()
{
int I,j;
Clrscr();
For(j=1;j<=4;j++)
{
for(i=1;i<=5;i++)
{
printf(“*”);
}
printf(“n”);
}
getch();
}
Unconditional Branching
 It is also called as jumping
Control is transfer from one point to another without checking condition
goto statement
break statement
Continue
Break Statement
The break statement in C programming
has the following two usages −
•When a break statement is encountered
inside a loop, the loop is immediately
terminated and the program control
resumes at the next statement following the
loop.
•It can be used to terminate a case in
the switch statement (covered in the next
chapter).
If you are using nested loops, the break
statement will stop the execution of the
innermost loop and start executing the next
line of code after the block.
Syntax
The syntax for a break statement in C is as
follows −
break;
Flow Diagram
Break Statement
#include <stdio.h>
int main
{
/* local variable definition */
int a = 10;
/* while loop execution */
while( a < 20 )
{
printf("value of a: %dn", a);
a++;
if( a > 15)
{ /* terminate the loop using break
statement */
break;
}
}
return 0;
}
When the above code is compiled and
executed, it produces the following result
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
Continue Statement
The continue statement in C programming
works somewhat like the break statement.
Instead of forcing termination, it forces
the next iteration of the loop to take
place, skipping any code in between.
For the for loop, continue statement
causes the conditional test and increment
portions of the loop to execute.
For
the while and do...while loops, continue
statement causes the program control to
pass to the conditional tests.
Syntax
The syntax for a continue statement in C is
as follows −
continue;
Flow Diagram
Continue Statement
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
continue;
}
printf("value of a: %dn", a);
a++;
} while( a < 20 );
return 0;
}
When the above code is compiled and
executed, it produces the following result
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Goto Statement
A goto statement in C programming
provides an unconditional jump from the
'goto' to a labeled statement in the same
function.
NOTE − Use of goto statement is highly
discouraged in any programming language
because it makes difficult to trace the
control flow of a program, making the
program hard to understand and hard to
modify. Any program that uses a goto can
be rewritten to avoid them.
Syntax
The syntax for a goto statement in C is as
follows −
goto label;
.. .
label: statement;
Flow Diagram
Continue Statement
#include <stdio.h>
int main ()
{
/* local variable definition */
int a = 10;
/* do loop execution */
LOOP:do
{
if( a == 15)
{
/* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %dn", a);
a++;
}
while( a < 20 );
return 0;
}
When the above code is compiled and
executed, it produces the following result
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

More Related Content

Similar to CONTROL FLOW in C.pptx

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptSanjjaayyy
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin cVikash Dhal
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming DharmaKumariBhandari
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
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 Hemantha Kulathilake
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 

Similar to CONTROL FLOW in C.pptx (20)

CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
control statement
control statement control statement
control statement
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
Bsit1
Bsit1Bsit1
Bsit1
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
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
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 

Recently uploaded

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 

Recently uploaded (20)

Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 

CONTROL FLOW in C.pptx

  • 1.
  • 2. Control Structure • Decision Making Statements Simple if If else Nested if else Switch Statements (conditional branching) • Loops Do- while While statements For Statements • Unconditional Branching break statement continue statement goto statement Given By Prof. Aparadh S.Y.
  • 3. Simple if • Syntax Flow Diagram if (expression is true) { action 1; } action 2; action3; Given By Prof. Aparadh S.Y.
  • 4. Examples of Simple if 1. Print hello when number is 10 Given By Prof. Aparadh S.Y.
  • 5. Examples of Simple if 1. Print hello when number is 10 Given By Prof. Aparadh S.Y. #include<conio.h> #include<stdio.h> void main() { int num; clrscr(); printf("Enter value for num“); scanf(“%d”, &num); if(num==10) { printf("hello)"; getch(); } }
  • 6. If Else • Syntax Flow Chart if (expression is true) { action 1; } Else { action 2; } action3; Given By Prof. Aparadh S.Y.
  • 7. Examples of If Else 1. Print entered number is positive or negative Given By Prof. Aparadh S.Y.
  • 8. Examples of If Else 1. Print entered number is positive or negative Given By Prof. Aparadh S.Y. #include<conio.h> #include<stdio.h> void main() { int num; clrscr(); printf(“Enter value for num“); Scanf(“%d”,&num); if(num>0) { Printf("num is positive“); } else { Printf("num is negative“)S; } getch(); }
  • 9. Examples of If Else 1. Print entered number is even or odd Given By Prof. Aparadh S.Y.
  • 10. Examples of If Else 1. Print entered number is even or odd Given By Prof. Aparadh S.Y. #include<conio.h> #include<stdio.h> void main() { int num; clrscr(); printf(“Enter value for num“); Scanf(“%d”,&num); if(num%2==0) { Printf("num is even“); } else { Printf("num is odd“)S; } getch(); }
  • 11. Examples of If Else 1. Print entered year is leap year or not Given By Prof. Aparadh S.Y.
  • 12. Examples of If Else 1. Print entered year is leap year or not Given By Prof. Aparadh S.Y. #include<conio.h> #include<stdio.h> void main() { int num; clrscr(); printf(“Enter value for year“); Scanf(“%d”,&num); if(num%4==0) { Printf(“year is leap year“); } else { Printf(“year is not leap year“)S; } getch(); }
  • 13. Nested If Else Ladder • Syntax Flow Chart Given By Prof. Aparadh S.Y.
  • 14. Home Work of Nested If Else Given By Prof. Aparadh S.Y. Write a program to input three numbers from user. Find maximum between given three numbers.
  • 15. Home Work of Nested If Else Given By Prof. Aparadh S.Y. Write a program to input three numbers from user. Find maximum between given three numbers. #include<stdio.hh> #include<conio.h> void main() { int num1,num2,num3; clrscr(); printf("Enter value for num1,num2 and num3“); scanf(“%d%d%d”,&num1, &num2, &num3); if(num1>num2) { if(num1>num3) { printf("num1 is greater“); } else { printf("num3 is greater“); } } else { if(num2>num3) { printf("num2 is greater“); } else { printf(num3 is greater“); } } getch(); }  Output: Enter value for num1 ,num2 and num3 4 2 1 Num1 is greater  Output: Enter value for num1 ,num2 and num3 1 4 2 Num2 is greater  Output: Enter value for num1 ,num2 and num3 1 2 3 Num3 is greater
  • 16. If Else Ladder Given By Prof. Aparadh S.Y. In C,C++ if- else –if ladder helps user decide from among multiple options . Here if statements are executed from top down As soon as one of the conditions controlling the if is true , the statements associated with that if is executed, and rest of all conditions are bypassed. If none of the conditions is true then the final else statement will be executed
  • 17. If Else Ladder • Syntax Flow Chart if (condition1) { statement 1; } else if (condition2) { statement 2; } . . else { statement; } Given By Prof. Aparadh S.Y.
  • 18. Examples of If Else 1. Print entered number is positive, negative or zero. Given By Prof. Aparadh S.Y.
  • 19. Examples of If Else ladder 1. Print entered number is positive, negative or zero. Given By Prof. Aparadh S.Y. #include<stdio.hh> #include<conio.h> void main() { int num; clrscr(); Printf(“Enter num“); Scanf(“%d”, &num; if(num>0) { Printf(“Positive“); } else if(num<0) { Printf(“Negative“); } else { Printf(“Zero“); } getch(); }  Output: Enter num 6 Positive  Output: Enter num -3 Negative  Output: Enter num 0 Zero
  • 20. Examples of If Else ladder Write a program to calculate grade of students Given By Prof. Aparadh S.Y.
  • 21. Write a program to calculate grade of students #include<iostream.h> #include<conio.h> void main() { int grade; clrscr(); Printf("enter value grade“); Scanf(“%d”, &grade); if(grade>=70 && grade<=100) { Printf(“Distinction“); } else if(grade>=60 && grade<=69) { Printf("First Class“); } else if(grade>=45 && grade<=59) { Printf(“Second Class“); } Given By Prof. Aparadh S.Y. else if(grade>=40 && grade<=44) { Printf("Pass Class“); } else if(grade>=0 && grade<40) { Printf("fail“); } else { Printf("invalid grade“); Printf("nshould enter grade inbetween 0 to 100“); } getch(); } //Distinction grade>=70 && grade<=100 //First Class grade>=60 && grade<=69 //Second Class grade>=45 && grade<=59 //Pass Class grade>=40 && grade<=44 //Fail grade>=0 && grade <45 //Invalid grade>=100 && grade<=0
  • 22. Switch Statements Syntax Flow Chart switch (expression) { case 1: action 1; break; Case 2: action 2; break; } . Case n: action n; break; Default: break; } Given By Prof. Aparadh S.Y.
  • 23. 1. Perform arithmetic operations using switch case Examples Switch Case Given By Prof. Aparadh S.Y.
  • 24. 1. .Perform arithmetic operations using switch case Examples Switch Case Given By Prof. Aparadh S.Y. #include<stdio.h> #include<conio.h> void main() { int a,b,ch; clrscr(); Printf("Enter value for a and b“); Scanf(”%d%d”,&a,&b); Printf("Enter choice“); Scanf(“%d”,&ch) switch(ch) { case 1: scanf("Addition is=%d”, a+b); break; case 4: scanf(“Division is=%d”, a/b); break; default: printf("Enter valid choice“); break; } getch(); } case 2: scanf(“Subtraction is=%d”, a-b); break; case 3: scanf(“ Multiplication is=%d”, a*b); break;
  • 25. Do While Loop • Syntax Flow Diagram Given By Prof. Aparadh S.Y. do { action 1; } while(condition is true); action 2;
  • 26. Examples of Do While Loop 1. Print 1 to 10 numbers using do while loop Given By Prof. Aparadh S.Y.
  • 27. Examples of Do While Loop 1. Print 1 to 10 numbers using do while loop Given By Prof. Aparadh S.Y. #include<stdio.h> #include<conio.h> Void main() { int a=1; Clrscr(); do { printf(“n%d”,a); a++; } while(a<=10); getch(); } Output: 1 2 3 4 5 6 7 8 9 10
  • 28. Examples of Do While Loop 1. Print 10 to 1 numbers using do while loop Given By Prof. Aparadh S.Y.
  • 29. Examples of Do While Loop 1. Print 10 to 1 numbers using do while loop Given By Prof. Aparadh S.Y. #include<stdio.h> #include<conio.h> Void main() { int a=10; Clrscr(); do { printf(“n%d”,a); a--; } while(a>0); getch(); } Output: 10 9 8 7 6 5 4 3 2 1
  • 30. • Syntax Flow Chart while (condition is true) { action 1; } action 2; While Loop
  • 31. Examples of While Loop 1. Print 1 to 10 numbers using while loop Given By Prof. Aparadh S.Y. #include<stdio.h> #include<conio.h> Void main() { int a=1; Clrscr(); While(a<=10) { printf(“n%d”,a); a++; } getch(); } Output: 1 2 3 4 5 6 7 8 9 10
  • 32. Examples of While Loop 1. Print 10 to 1 numbers using while loop Given By Prof. Aparadh S.Y.
  • 33. Examples of While Loop 1. Print 10 to 1 numbers using while loop Given By Prof. Aparadh S.Y. #include<stdio.h> #include<conio.h> Void main() { int a=10; Clrscr(); While(a>=10) { printf(“n%d”,a); a--; } getch(); } Output: 10 9 8 7 6 5 4 3 2 1
  • 34. • Syntax Flow Chart for (initial value; condition; increment/decrement) { action 1; } action 2; For Loop
  • 35. Examples of for Loop 1. Print 10 to 1 numbers using for loop Given By Prof. Aparadh S.Y. #include<stdio.h> #include<conio.h> Void main() { int a; Clrscr(); For(a=10;a>0;a--) { printf(“n%d”,a); a--; } getch(); } Output: 10 9 8 7 6 5 4 3 2 1
  • 36. • Syntax Flow Chart for (initial value; condition; increment/decrement) { action 1; For((initial value; condition; increment/decrement) { body of inner loop; } action 2; } Nesting For Loop
  • 37. Examples of nesting for Loop Given By Prof. Aparadh S.Y. Program to print following pattern using nested for loop * * * * * * * * * * * * * * * * * * * * * * * * # include<stdio.h> #include<conio.h> Void main() { int I,j; Clrscr(); For(j=1;j<=4;j++) { for(i=1;i<=5;i++) { printf(“*”); } printf(“n”); } getch(); }
  • 38. Unconditional Branching  It is also called as jumping Control is transfer from one point to another without checking condition goto statement break statement Continue
  • 39. Break Statement The break statement in C programming has the following two usages − •When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. •It can be used to terminate a case in the switch statement (covered in the next chapter). If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax The syntax for a break statement in C is as follows − break; Flow Diagram
  • 40. Break Statement #include <stdio.h> int main { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %dn", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } } return 0; } When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
  • 41. Continue Statement The continue statement in C programming works somewhat like the break statement. Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control to pass to the conditional tests. Syntax The syntax for a continue statement in C is as follows − continue; Flow Diagram
  • 42. Continue Statement #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { if( a == 15) { /* skip the iteration */ a = a + 1; continue; } printf("value of a: %dn", a); a++; } while( a < 20 ); return 0; } When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
  • 43. Goto Statement A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function. NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them. Syntax The syntax for a goto statement in C is as follows − goto label; .. . label: statement; Flow Diagram
  • 44. Continue Statement #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ LOOP:do { if( a == 15) { /* skip the iteration */ a = a + 1; goto LOOP; } printf("value of a: %dn", a); a++; } while( a < 20 ); return 0; } When the above code is compiled and executed, it produces the following result value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19