KEMBAR78
Decision making statements in C programming | PPTX
Decision making
statements in C
1Presented by: Group-E (The Anonymous)
Bikash Dhakal
Bimal Pradhan
Bikram Bhurtel
Gagan Puri
Rabin BK
Introduction to decision making statements
Decision making statements in C
If statement
If….else statement
If….else if….else statement
Nested if statement
Switch statement
The conditional operator(? : )
GOTO statement
References
Queries
2
Introduction to decision making statement in C
Also known as control statements
Controls the flow of execution
Executes program until a specified condition is met
One of the most important parts in programming
3
Decision making statements in C
if statement
switch statement
Conditional operator statement
goto statement
4
If statement
The expression must evaluate to true or false.
The “statement” can be a group of statements in braces:
Syntax:
5
if(expression)
{
Statement 1;
Statement 2;
}
6
#include<stdio.h>
#include<conio.h>
main( )
{
int n ;
printf ( "Enter your age: " ) ;
scanf ("%d", &n ) ;
if ( n>=20 )
{
printf (“nYou are in your adulthood. ” ) ;
}
getch();
}
Enter any number: 25
You are in your adulthood.
If else statement
 An extension version of if statement.
 Generally in the form of if (test expression)
7
if (test expression)
{
true block statement(s)
}
else
{
false block statement(s)
}
Example
8
#include<stdio.h>
#include<conio.h>
main()
{
int age;
printf("n enter your age:");
scanf("%d",&age);
if(age>=18)
{
printf("eligible for citizenship");
}
else
{
printf("n NOT ELIGIBLE");
}
getch();
}
enter your age:13
NOT ELIGIBLE
enter your age:20
eligible for citizenship
If …else if…. else statement
 It is used to give series of decision
 Syntax:
9
if (condition)
{
//statement 1
}
else if (condition 2)
{
//statement 2
}
else
{
// statement when all condition are false
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,r1,r2;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
r1=-b/(2*a);
printf("n Roots are equal and is %.3f",r1);
}
else if(d>0)
{
d=sqrt(d);
r1=(-b+d)/(2*a);
r2=(-b-d)/(2*a);
printf("nRoots are real and r1=%.3f, r2=%.3f",r1,r2);
}
else
{
d=sqrt(fabs(d));
printf("n Roots are imaginary");
printf("n r1=%.3f + i%.3f",-b/(2*a),d/(2*a));
printf("n r2=%.3f - i%.3f",-b/(2*a),d/(2*a));
}
getch();
10
//Program to find the roots of the quadratic equations ax^2+bx+c=0
Enter coefficients a, b, c: 1
2
1
Roots are equal and is -1.000
Nested if-else statement
New block of if-else statement defined
in existing if or else block statement.
Syntax:
11
if(condition)
{
if(condition)
{
statement
}
else
{
statement
}
}
else
{
statement
}
12
#include <stdio.h>
#include <conio.h>
void main( )
{
int a,b,c;
printf("Enter 3 numbers: n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if( a > c)
{
printf(“%d is greatest“,a);
}
else
{
printf(“%d is greatest“,c);
}
}
else
{
if( b> c)
{
printf(“%d is greatest“,b);
}
else
{
printf(“%d is greatest“,c);
}
}
getch();
}
Using Nested-if statement to check greatest
number among three input numbers
Enter 3 numbers:
9
11
5
11 is greatest
Enter 3 numbers:
49
55
88
88 is greatest
 Multi-way decision statement
 Tests the value of a given variable against a list of case values
 when a match is found, a block of statements associated with that
case is executed.
13
switch (var)
{
case case value 1:
statements;
break;
case case value 2:
statements;
break;
…….
…….
case case value n:
default:
statements;
break;
}
Should be an integer or characters
Known as case labels and should be constants or
constant expressions
And should be end with semicolon
14
#include<stdio.h>
int main()
{
char operator;
float firstNumber,secondNumber;
printf("Enter an operator (+, -): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%f %f",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%f + %f = %f",firstNumber, secondNumber, firstNumber +
secondNumber);
break;
case '-':
printf("%f - %f = %f",firstNumber, secondNumber, firstNumber -
secondNumber);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
Enter an operator(+,-): -
Enter two operands: 6
4
6.000000 – 4.000000 = 2.000000
Enter an operator(+,-): /
Enter two operands: 9
3
Error! operator is not correct
15
The ?: operator
Known as conditional operator
Used for making two-way decisions
Syntax:
16
Conditional expression ? exp1: exp2
Example:
17
//If True first exp is executed
//If False second exp is executed
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int a;
printf("Enter a number: ");
scanf("%d",&a);
(a%2==0?printf("Even"):printf("Odd"));
getch();
}
Enter a number: 9
Odd
Enter a number: 8
Even
The GOTO statement
Rarely used statement
Provides an unconditional jump from ‘goto’ to a labelled statement
in the same function
Syntax:
Requires label to identify the place where the branch is to be made
18
goto label;
-------------
-------------
label;
statement;
label;
statement;
-------------
-------------
goto label;
Forward jump Backward jump
Example:
19
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
float y,x;
read:
printf("nEnter a number: ");
scanf("%f",&y);
if(y<0) goto read;
x=sqrt(y);
printf("nSquare root of %f---->%fn",y,x);
goto read;
}
Enter a number: 9
Square root of 9.000000---->3.000000
Enter a number: 25
Square root of 25.000000---->5.000000
References:
Websites:
google.com
codecadamy.com
tutorialspoint.com
External sources:
Text book: Programming in ANSIC (E-Balagurusamy)
20
Queries
21

Decision making statements in C programming

  • 1.
    Decision making statements inC 1Presented by: Group-E (The Anonymous) Bikash Dhakal Bimal Pradhan Bikram Bhurtel Gagan Puri Rabin BK
  • 2.
    Introduction to decisionmaking statements Decision making statements in C If statement If….else statement If….else if….else statement Nested if statement Switch statement The conditional operator(? : ) GOTO statement References Queries 2
  • 3.
    Introduction to decisionmaking statement in C Also known as control statements Controls the flow of execution Executes program until a specified condition is met One of the most important parts in programming 3
  • 4.
    Decision making statementsin C if statement switch statement Conditional operator statement goto statement 4
  • 5.
    If statement The expressionmust evaluate to true or false. The “statement” can be a group of statements in braces: Syntax: 5 if(expression) { Statement 1; Statement 2; }
  • 6.
    6 #include<stdio.h> #include<conio.h> main( ) { int n; printf ( "Enter your age: " ) ; scanf ("%d", &n ) ; if ( n>=20 ) { printf (“nYou are in your adulthood. ” ) ; } getch(); } Enter any number: 25 You are in your adulthood.
  • 7.
    If else statement An extension version of if statement.  Generally in the form of if (test expression) 7 if (test expression) { true block statement(s) } else { false block statement(s) }
  • 8.
    Example 8 #include<stdio.h> #include<conio.h> main() { int age; printf("n enteryour age:"); scanf("%d",&age); if(age>=18) { printf("eligible for citizenship"); } else { printf("n NOT ELIGIBLE"); } getch(); } enter your age:13 NOT ELIGIBLE enter your age:20 eligible for citizenship
  • 9.
    If …else if….else statement  It is used to give series of decision  Syntax: 9 if (condition) { //statement 1 } else if (condition 2) { //statement 2 } else { // statement when all condition are false }
  • 10.
    #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,d,r1,r2; printf("Entercoefficients a, b and c: "); scanf("%f%f%f",&a,&b,&c); d=b*b-4*a*c; if(d==0) { r1=-b/(2*a); printf("n Roots are equal and is %.3f",r1); } else if(d>0) { d=sqrt(d); r1=(-b+d)/(2*a); r2=(-b-d)/(2*a); printf("nRoots are real and r1=%.3f, r2=%.3f",r1,r2); } else { d=sqrt(fabs(d)); printf("n Roots are imaginary"); printf("n r1=%.3f + i%.3f",-b/(2*a),d/(2*a)); printf("n r2=%.3f - i%.3f",-b/(2*a),d/(2*a)); } getch(); 10 //Program to find the roots of the quadratic equations ax^2+bx+c=0 Enter coefficients a, b, c: 1 2 1 Roots are equal and is -1.000
  • 11.
    Nested if-else statement Newblock of if-else statement defined in existing if or else block statement. Syntax: 11 if(condition) { if(condition) { statement } else { statement } } else { statement }
  • 12.
    12 #include <stdio.h> #include <conio.h> voidmain( ) { int a,b,c; printf("Enter 3 numbers: n"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if( a > c) { printf(“%d is greatest“,a); } else { printf(“%d is greatest“,c); } } else { if( b> c) { printf(“%d is greatest“,b); } else { printf(“%d is greatest“,c); } } getch(); } Using Nested-if statement to check greatest number among three input numbers Enter 3 numbers: 9 11 5 11 is greatest Enter 3 numbers: 49 55 88 88 is greatest
  • 13.
     Multi-way decisionstatement  Tests the value of a given variable against a list of case values  when a match is found, a block of statements associated with that case is executed. 13
  • 14.
    switch (var) { case casevalue 1: statements; break; case case value 2: statements; break; ……. ……. case case value n: default: statements; break; } Should be an integer or characters Known as case labels and should be constants or constant expressions And should be end with semicolon 14
  • 15.
    #include<stdio.h> int main() { char operator; floatfirstNumber,secondNumber; printf("Enter an operator (+, -): "); scanf("%c", &operator); printf("Enter two operands: "); scanf("%f %f",&firstNumber, &secondNumber); switch(operator) { case '+': printf("%f + %f = %f",firstNumber, secondNumber, firstNumber + secondNumber); break; case '-': printf("%f - %f = %f",firstNumber, secondNumber, firstNumber - secondNumber); break; default: printf("Error! operator is not correct"); } return 0; } Enter an operator(+,-): - Enter two operands: 6 4 6.000000 – 4.000000 = 2.000000 Enter an operator(+,-): / Enter two operands: 9 3 Error! operator is not correct 15
  • 16.
    The ?: operator Knownas conditional operator Used for making two-way decisions Syntax: 16 Conditional expression ? exp1: exp2
  • 17.
    Example: 17 //If True firstexp is executed //If False second exp is executed #include <stdio.h> #include <conio.h> #include <math.h> void main() { int a; printf("Enter a number: "); scanf("%d",&a); (a%2==0?printf("Even"):printf("Odd")); getch(); } Enter a number: 9 Odd Enter a number: 8 Even
  • 18.
    The GOTO statement Rarelyused statement Provides an unconditional jump from ‘goto’ to a labelled statement in the same function Syntax: Requires label to identify the place where the branch is to be made 18 goto label; ------------- ------------- label; statement; label; statement; ------------- ------------- goto label; Forward jump Backward jump
  • 19.
    Example: 19 #include <stdio.h> #include <conio.h> #include<math.h> void main() { float y,x; read: printf("nEnter a number: "); scanf("%f",&y); if(y<0) goto read; x=sqrt(y); printf("nSquare root of %f---->%fn",y,x); goto read; } Enter a number: 9 Square root of 9.000000---->3.000000 Enter a number: 25 Square root of 25.000000---->5.000000
  • 20.
  • 21.