SlideShare a Scribd company logo
1 of 53
Unit - 2
Control Statements
• The control statements help a user to specify a program control’s
flow.
• They specifies the order of execution of the instructions present in a
program.
• These make it possible for the program to make certain decisions,
perform various tasks repeatedly, or even jump from any one section
of the code to a different section.
Types of Control Statements
•There are four types of control statements in C:
Decision making statements (if, if-else, nested-if)
Selection statements (switch-case)
Iteration statements (for, while, do-while)
Jump statements (break, continue, goto)
If statement (Decision making)
• If the condition given in If statement is true, then the statement
inside the If block is executed, otherwise the statement outside the If
block is executed.
• Syntax
if(expression){
//code to be executed
}
Flowchart of if statement
Example-1
#include<stdio.h>
void main()
{
int a=5,b=2;
if(a>b)
{
printf(“a is big”);
}
printf(“Outer statement”);
}
Output:
a is big
Outer statement
//Finding largest of three numbers
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
Output
Enter three numbers?
12 23 34
34 is largest
If-else Statement (Decision making)
• If the condition given in If statement is true, then the statement
inside the If block is executed, otherwise the statement inside the
Else block is executed.
• Syntax
if(expression){
//code to be executed if condition is true
}
else{
//code to be executed if condition is false
}
Flowchart of if-else statement
//Finding odd or even number
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
Output
enter a number:4
4 is even number
enter a number:5
5 is odd number
//Maximum of two numbers
#include<stdio.h>
void main(){
int a,b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
if(a>b)
printf(“%d is big”,a);
else
printf(“%d is big”,b);
}
Output:
Enter two numbers: 20 40
40 is big
//Check whether a person is eligible to vote or not
#include <stdio.h>
int main()
{
int age;
printf("Enter your age?");
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
Output
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote
Nested if-else Statements (Decision making)
• The nested if-else statements consist of outer-if and inner-if.
• If the condition of outer-if is true then outer-if block is executed
which contains inner-if and if the condition of inner-if is true,
statements under inner-if block will be executed else the statements
of inner-if else block will be executed.
• If the outer-if condition is not true then the outer-if-else block is
executed.
Syntax
if ( outer-if condition)
{
if ( inner-if condition)
{
Inner-if statements;
}
else
{
Inner-if-else statements;
}
else
{
outer-if-else statements;
}
Flowchart of else-if ladder statement
//Maximum of three numbers
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
{
printf(“%d is the maximum“, num1);
}
else
{
printf(“%d is the maximum“, num3);
}
}
else
{
if(num2 > num3)
{
printf(“%d is the maximum“, num2);
}
else
{
printf(“%d is the maximum“, num3);
}
}
return 0;
}
Output:
Enter three numbers: 20 34 56
56 is the maximum
The else-if ladder statement (Decision making)
• The if-else-if ladder statement is an extension to the if-else statement
and similar to switch-case statement.
• It is used in the scenario where there are multiple cases to be
performed for different conditions.
• If a condition is true then the statements defined in the if block will
be executed, otherwise if some other condition is true then the
statements defined in the else-if block will be executed, at the last if
none of the condition is true then the statements defined in the else
block will be executed.
Syntax
if(condition1){
//statements;
}
else if(condition2){
//statements;
}
else if(condition3){
//statements;
}
else if(condition4){
//statements;
}
...
else{
//statements;
}
Flowchart of else-if ladder statement
//Students marks grading
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
Output
Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...
Switch Statement (Selection)
• The switch statement is an alternate to if-else-if ladder statement which allows us to
define various statements in the multiple cases for the different values of a single
variable.
• Syntax
switch(expression){
case value1:
//code to be executed;
break;
case value2:
//code to be executed;
break;
......
default:
code to be executed if all cases are not matched;
}
Rules for switch statement
• The switch expression must be of an integer or character type.
• The case value must be an integer or character constant.
• The case value can be used only inside the switch statement.
• The break statement in switch case is not must. It is optional. If there
is no break statement found in the case, all the cases will be executed
present after the matched case. It is known as fall through the state
of C switch statement.
Let's try to understand it by the examples. We are
assuming that there are following variables.
int x,y,z;
char a,b;
float f;
Valid Switch Invalid Switch Valid Case Invalid Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case 'a'; case x;
switch(a+b-2) case 1+2; case x+2;
switch(func(x,y)) case 'x'>'y'; case 1,2,3;
Flowchart for switch statement
Example
#include <stdio.h>
int main()
{
int x = 2;
switch (x) {
case 1:
printf("Choice is 1");
break;
case 2:
printf("Choice is 2");
break;
case 3:
printf("Choice is 3");
break;
default:
printf("Choice other than 1, 2 and 3");
break;
}
}
Output:
Choice is 2
//Arithmetic operation using switch case
#include<stdio.h>
int main()
{
int a,b;
int op;
printf(" 1.Additionn 2.Subtractionn
3.Multiplicationn 4.Divisionn");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2:
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3:
printf("Multiplication of %d and %d is: %d”,a,b,a*b);
break;
case 4:
printf("Division of %d and %d is %d : "a,b,a/b);
break;
default:
printf(" Enter Correct Choice.");
break;
}
return 0;
}
Output:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter the values of a & b: 20 15
Enter your Choice : 1
Sum of 20 and 15 is : 35
Loops
• Repeating the same process multiple times until a specific condition is
satisfied is called looping.
• Advantages
It provides code reusability.
Using loops, we do not need to write the same code again and again.
• Types of C Loops
for
while
do-while
for loop
• The for loop is used in the case where we need to execute some part
of the code until the given condition is satisfied.
• It is better to use for loop if the number of iteration is known in
advance.
• Syntax
for ( initialization; condition; incr/decr ){
//code to be executed
}
Flowchart of for loop
Example for for loop
#include<stdio.h>
void main(){
int i=0;
for(i=1;i<=10;i++)
{
printf("%d n",i);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
//Fibonacci series using for loop
#include<stdio.h>
void main()
{
int f1=-1,f2=1,f3,i,n;
printf("How many fibonacci
numbers you want: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
f3=f1+f2;
printf("%dn",f3);
f1=f2;
f2=f3;
}
}
Output:
How many fibonacci numbers you want: 7
0
1
1
2
3
5
8
//Finding factorial using for loop
#include<stdio.h>
void main()
{
int n,i,fact=1;
printf("Enter a number: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial of %d is %d",n,fact);
}
Output:
Enter a number: 5
The factorial of 5 is 120
//Infinite for loop
#include<stdio.h>
int main ()
{
for(;;)
{
printf(“Hello Dearn");
}
}
Output:
Hello Dear
Hello Dear
Hello Dear
….
….
….
While loop
• The while loop is to be used in the scenario where we don't know the
number of iterations in advance.
• The block of statements is executed in the while loop until the
condition is satisfied.
• Syntax
while(condition){
//code to be executed
}
Flowchart of while loop
Example for while loop
#include<stdio.h>
void main(){
int i=1;
while(i<=10){
printf("%d n",i);
i++;
}
}
Output:
1
2
3
4
5
6
7
8
9
10
//Finding sum of digits using while loop
#include<stdio.h>
void main()
{
int n,sum=0,d;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("The sum of the given digits is %d",sum);
}
Output:
Enter a number: 457
The sum of the given digits is 16
//Reversing the digits using while loop
#include<stdio.h>
void main()
{
int n,rev=0,d;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
printf("The reverse of the given digits is %d",rev);
}
Output:
Enter a number: 478
The reverse of the given digits is 874
//Finding whether a number is prime or not using while loop
#include <stdio.h>
void main()
{
int n, i, flag = 0;
printf("Enter a number: ");
scanf("%d",&n);
i=2;
while( i<n)
{
if(n%i==0)
{
flag=1;
break;
}
i++;
}
if (flag==0 && n!=1)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
}
Output:
Enter a number: 5
5 is a prime number
Enter a number: 4
4 is not a prime number
//Finding first n prime numbers using while loop
#include<stdio.h>
void main()
{
int i,n,p,count,flag;
printf("Enter the number: ") ;
scanf("%d",&n) ;
printf("First %d prime numbers aren”,n);
p=2;
i=1;
while(i<=n)
{
flag=1;
for(count=2;count<=p-1;count++)
{
if(p%count==0)
{
flag=0;
break;
}
}
if(flag==1)
{
printf("%dt",p) ;
i++;
}
p++;
}
}
Output:
Enter the number: 5
First 5 prime numbers are
2 3 5 7 11
Infinite while loop
#include<stdio.h>
void main(){
while(1){
printf(“Hello Dearn");
}
}
Output:
Hello Dear
Hello Dear
……….
……..
do-while loop
• The do while loop is a post tested loop.
• Using the do-while loop, we can repeat the execution of several parts
of the statements.
• The do-while loop is mainly used in the case where we need to
execute the loop at least once.
• Syntax:
do{
//code to be executed
}while(condition);
Flowchart for do-while loop
Example for do-while loop
#include<stdio.h>
void main(){
int i=1;
do{
printf("%d n",i);
i++;
}while(i<=10);
}
Output:
1
2
3
4
5
6
7
8
9
10
Example for do-while loop
#include<stdio.h>
void main()
{
int i=1;
do{
printf(“Hello Dear”);
i++;
}while(i<0);
}
Output:
Hello Dear
In the above program, though the condition is not satisfied, the statement is
executed at least once.
Infinite do-while loop
#include<stdio.h>
void main(){
do{
printf(“Hello Dearn");
}while(True);
}
Output:
Hello Dear
Hello Dear
……….
……..
Break Statement (Jump Statement)
• The break statement is used to bring the program control out of the
loop for the particular condition.
• break keyword is used
• The break statement is used inside loops or switch statement.
Example for break statement
#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%dt",i);
if(i == 5)
break;
}
printf(“n came outside of loop");
}
Output
0 1 2 3 4 5
came outside of loop
Continue Statement (Jump Statement)
• The continue statement is used to skip the iteration of the loop one
time for the particular condition.
• continue keyword is used
Example for continue statement
#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%dt",i);
if(i == 5)
continue;
}
printf(“n came outside of loop");
}
Output
0 1 2 3 4 6 7 8 9
came outside of loop
Goto Statement (Jump Statement)
• Goto statement is a jump statement that is used to jump from one
part of the code to any other part of the code.
• Syntax
Example for goto statement
#include<stdio.h>
void main()
{
printf("Good morningn");
goto point;
printf("How are you all?n");
printf("Had breakfast?n");
printf("Have a good dayn");
point:
printf("Today's Topic");
}
Output:
Good morning
Today’s Topic

More Related Content

Similar to C Unit-2.ppt

CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxSmitaAparadh
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Control structure of c
Control structure of cControl structure of c
Control structure of cKomal Kotak
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CChandrakantDivate1
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfmounikanarra3
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVINGGOWSIKRAJAP
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionalish sha
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxSasideepa
 

Similar to C Unit-2.ppt (20)

CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
control statement
control statement control statement
control statement
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
 
Bsit1
Bsit1Bsit1
Bsit1
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
What is c
What is cWhat is c
What is c
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Lec 10
Lec 10Lec 10
Lec 10
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
BHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptxBHARGAVISTATEMENTS.PPT.pptx
BHARGAVISTATEMENTS.PPT.pptx
 

Recently uploaded

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 

Recently uploaded (20)

AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 

C Unit-2.ppt

  • 2. Control Statements • The control statements help a user to specify a program control’s flow. • They specifies the order of execution of the instructions present in a program. • These make it possible for the program to make certain decisions, perform various tasks repeatedly, or even jump from any one section of the code to a different section.
  • 3. Types of Control Statements •There are four types of control statements in C: Decision making statements (if, if-else, nested-if) Selection statements (switch-case) Iteration statements (for, while, do-while) Jump statements (break, continue, goto)
  • 4. If statement (Decision making) • If the condition given in If statement is true, then the statement inside the If block is executed, otherwise the statement outside the If block is executed. • Syntax if(expression){ //code to be executed }
  • 5. Flowchart of if statement
  • 6. Example-1 #include<stdio.h> void main() { int a=5,b=2; if(a>b) { printf(“a is big”); } printf(“Outer statement”); } Output: a is big Outer statement
  • 7. //Finding largest of three numbers #include <stdio.h> int main() { int a, b, c; printf("Enter three numbers?"); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) { printf("%d is largest",a); } if(b>a && b > c) { printf("%d is largest",b); } if(c>a && c>b) { printf("%d is largest",c); } if(a == b && a == c) { printf("All are equal"); } } Output Enter three numbers? 12 23 34 34 is largest
  • 8. If-else Statement (Decision making) • If the condition given in If statement is true, then the statement inside the If block is executed, otherwise the statement inside the Else block is executed. • Syntax if(expression){ //code to be executed if condition is true } else{ //code to be executed if condition is false }
  • 10. //Finding odd or even number #include<stdio.h> int main(){ int number=0; printf("enter a number:"); scanf("%d",&number); if(number%2==0){ printf("%d is even number",number); } else{ printf("%d is odd number",number); } return 0; } Output enter a number:4 4 is even number enter a number:5 5 is odd number
  • 11. //Maximum of two numbers #include<stdio.h> void main(){ int a,b; printf("Enter two numbers: "); scanf("%d%d", &a, &b); if(a>b) printf(“%d is big”,a); else printf(“%d is big”,b); } Output: Enter two numbers: 20 40 40 is big
  • 12. //Check whether a person is eligible to vote or not #include <stdio.h> int main() { int age; printf("Enter your age?"); scanf("%d",&age); if(age>=18) { printf("You are eligible to vote..."); } else { printf("Sorry ... you can't vote"); } } Output Enter your age?18 You are eligible to vote... Enter your age?13 Sorry ... you can't vote
  • 13. Nested if-else Statements (Decision making) • The nested if-else statements consist of outer-if and inner-if. • If the condition of outer-if is true then outer-if block is executed which contains inner-if and if the condition of inner-if is true, statements under inner-if block will be executed else the statements of inner-if else block will be executed. • If the outer-if condition is not true then the outer-if-else block is executed.
  • 14. Syntax if ( outer-if condition) { if ( inner-if condition) { Inner-if statements; } else { Inner-if-else statements; } else { outer-if-else statements; }
  • 15. Flowchart of else-if ladder statement
  • 16. //Maximum of three numbers #include <stdio.h> int main() { int num1, num2, num3; printf("Enter three numbers: "); scanf("%d%d%d", &num1, &num2, &num3); if(num1 > num2) { if(num1 > num3) { printf(“%d is the maximum“, num1); } else { printf(“%d is the maximum“, num3); } } else { if(num2 > num3) { printf(“%d is the maximum“, num2); } else { printf(“%d is the maximum“, num3); } } return 0; } Output: Enter three numbers: 20 34 56 56 is the maximum
  • 17. The else-if ladder statement (Decision making) • The if-else-if ladder statement is an extension to the if-else statement and similar to switch-case statement. • It is used in the scenario where there are multiple cases to be performed for different conditions. • If a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed.
  • 19. Flowchart of else-if ladder statement
  • 20. //Students marks grading #include <stdio.h> int main() { int marks; printf("Enter your marks?"); scanf("%d",&marks); if(marks > 85 && marks <= 100) { printf("Congrats ! you scored grade A ..."); } else if (marks > 60 && marks <= 85) { printf("You scored grade B + ..."); } else if (marks > 40 && marks <= 60) { printf("You scored grade B ..."); } else if (marks > 30 && marks <= 40) { printf("You scored grade C ..."); } else { printf("Sorry you are fail ..."); } } Output Enter your marks?10 Sorry you are fail ... Enter your marks?40 You scored grade C ... Enter your marks?90 Congrats ! you scored grade A ...
  • 21. Switch Statement (Selection) • The switch statement is an alternate to if-else-if ladder statement which allows us to define various statements in the multiple cases for the different values of a single variable. • Syntax switch(expression){ case value1: //code to be executed; break; case value2: //code to be executed; break; ...... default: code to be executed if all cases are not matched; }
  • 22. Rules for switch statement • The switch expression must be of an integer or character type. • The case value must be an integer or character constant. • The case value can be used only inside the switch statement. • The break statement in switch case is not must. It is optional. If there is no break statement found in the case, all the cases will be executed present after the matched case. It is known as fall through the state of C switch statement.
  • 23. Let's try to understand it by the examples. We are assuming that there are following variables. int x,y,z; char a,b; float f; Valid Switch Invalid Switch Valid Case Invalid Case switch(x) switch(f) case 3; case 2.5; switch(x>y) switch(x+2.5) case 'a'; case x; switch(a+b-2) case 1+2; case x+2; switch(func(x,y)) case 'x'>'y'; case 1,2,3;
  • 24. Flowchart for switch statement
  • 25. Example #include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; case 3: printf("Choice is 3"); break; default: printf("Choice other than 1, 2 and 3"); break; } } Output: Choice is 2
  • 26. //Arithmetic operation using switch case #include<stdio.h> int main() { int a,b; int op; printf(" 1.Additionn 2.Subtractionn 3.Multiplicationn 4.Divisionn"); printf("Enter the values of a & b: "); scanf("%d %d",&a,&b); printf("Enter your Choice : "); scanf("%d",&op); switch(op) { case 1: printf("Sum of %d and %d is : %d",a,b,a+b); break; case 2: printf("Difference of %d and %d is : %d",a,b,a-b); break; case 3: printf("Multiplication of %d and %d is: %d”,a,b,a*b); break; case 4: printf("Division of %d and %d is %d : "a,b,a/b); break; default: printf(" Enter Correct Choice."); break; } return 0; } Output: 1.Addition 2.Subtraction 3.Multiplication 4.Division Enter the values of a & b: 20 15 Enter your Choice : 1 Sum of 20 and 15 is : 35
  • 27. Loops • Repeating the same process multiple times until a specific condition is satisfied is called looping. • Advantages It provides code reusability. Using loops, we do not need to write the same code again and again. • Types of C Loops for while do-while
  • 28. for loop • The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. • It is better to use for loop if the number of iteration is known in advance. • Syntax for ( initialization; condition; incr/decr ){ //code to be executed }
  • 30. Example for for loop #include<stdio.h> void main(){ int i=0; for(i=1;i<=10;i++) { printf("%d n",i); } } Output: 1 2 3 4 5 6 7 8 9 10
  • 31. //Fibonacci series using for loop #include<stdio.h> void main() { int f1=-1,f2=1,f3,i,n; printf("How many fibonacci numbers you want: "); scanf("%d",&n); for(i=0;i<n;i++) { f3=f1+f2; printf("%dn",f3); f1=f2; f2=f3; } } Output: How many fibonacci numbers you want: 7 0 1 1 2 3 5 8
  • 32. //Finding factorial using for loop #include<stdio.h> void main() { int n,i,fact=1; printf("Enter a number: "); scanf("%d",&n); for(i=1;i<=n;i++) { fact=fact*i; } printf("The factorial of %d is %d",n,fact); } Output: Enter a number: 5 The factorial of 5 is 120
  • 33. //Infinite for loop #include<stdio.h> int main () { for(;;) { printf(“Hello Dearn"); } } Output: Hello Dear Hello Dear Hello Dear …. …. ….
  • 34. While loop • The while loop is to be used in the scenario where we don't know the number of iterations in advance. • The block of statements is executed in the while loop until the condition is satisfied. • Syntax while(condition){ //code to be executed }
  • 36. Example for while loop #include<stdio.h> void main(){ int i=1; while(i<=10){ printf("%d n",i); i++; } } Output: 1 2 3 4 5 6 7 8 9 10
  • 37. //Finding sum of digits using while loop #include<stdio.h> void main() { int n,sum=0,d; printf("Enter a number: "); scanf("%d",&n); while(n>0) { d=n%10; sum=sum+d; n=n/10; } printf("The sum of the given digits is %d",sum); } Output: Enter a number: 457 The sum of the given digits is 16
  • 38. //Reversing the digits using while loop #include<stdio.h> void main() { int n,rev=0,d; printf("Enter a number: "); scanf("%d",&n); while(n>0) { d=n%10; rev=rev*10+d; n=n/10; } printf("The reverse of the given digits is %d",rev); } Output: Enter a number: 478 The reverse of the given digits is 874
  • 39. //Finding whether a number is prime or not using while loop #include <stdio.h> void main() { int n, i, flag = 0; printf("Enter a number: "); scanf("%d",&n); i=2; while( i<n) { if(n%i==0) { flag=1; break; } i++; } if (flag==0 && n!=1) printf("%d is a prime number.",n); else printf("%d is not a prime number.",n); } Output: Enter a number: 5 5 is a prime number Enter a number: 4 4 is not a prime number
  • 40. //Finding first n prime numbers using while loop #include<stdio.h> void main() { int i,n,p,count,flag; printf("Enter the number: ") ; scanf("%d",&n) ; printf("First %d prime numbers aren”,n); p=2; i=1; while(i<=n) { flag=1; for(count=2;count<=p-1;count++) { if(p%count==0) { flag=0; break; } } if(flag==1) { printf("%dt",p) ; i++; } p++; } } Output: Enter the number: 5 First 5 prime numbers are 2 3 5 7 11
  • 41. Infinite while loop #include<stdio.h> void main(){ while(1){ printf(“Hello Dearn"); } } Output: Hello Dear Hello Dear ………. ……..
  • 42. do-while loop • The do while loop is a post tested loop. • Using the do-while loop, we can repeat the execution of several parts of the statements. • The do-while loop is mainly used in the case where we need to execute the loop at least once. • Syntax: do{ //code to be executed }while(condition);
  • 44. Example for do-while loop #include<stdio.h> void main(){ int i=1; do{ printf("%d n",i); i++; }while(i<=10); } Output: 1 2 3 4 5 6 7 8 9 10
  • 45. Example for do-while loop #include<stdio.h> void main() { int i=1; do{ printf(“Hello Dear”); i++; }while(i<0); } Output: Hello Dear In the above program, though the condition is not satisfied, the statement is executed at least once.
  • 46. Infinite do-while loop #include<stdio.h> void main(){ do{ printf(“Hello Dearn"); }while(True); } Output: Hello Dear Hello Dear ………. ……..
  • 47.
  • 48. Break Statement (Jump Statement) • The break statement is used to bring the program control out of the loop for the particular condition. • break keyword is used • The break statement is used inside loops or switch statement.
  • 49. Example for break statement #include<stdio.h> void main () { int i; for(i = 0; i<10; i++) { printf("%dt",i); if(i == 5) break; } printf(“n came outside of loop"); } Output 0 1 2 3 4 5 came outside of loop
  • 50. Continue Statement (Jump Statement) • The continue statement is used to skip the iteration of the loop one time for the particular condition. • continue keyword is used
  • 51. Example for continue statement #include<stdio.h> void main () { int i; for(i = 0; i<10; i++) { printf("%dt",i); if(i == 5) continue; } printf(“n came outside of loop"); } Output 0 1 2 3 4 6 7 8 9 came outside of loop
  • 52. Goto Statement (Jump Statement) • Goto statement is a jump statement that is used to jump from one part of the code to any other part of the code. • Syntax
  • 53. Example for goto statement #include<stdio.h> void main() { printf("Good morningn"); goto point; printf("How are you all?n"); printf("Had breakfast?n"); printf("Have a good dayn"); point: printf("Today's Topic"); } Output: Good morning Today’s Topic