KEMBAR78
presentation of data structure and algorithm | PPTX
Decision making
statements in C
Presented by: Omair Aquil - 35000323023
Bisweswar Mahato - 35000323013
Agnish Deyati - 35000323003
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
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
Decision making statements in C
if statement
switch statement
Conditional operator statement
goto statement
If statement
The expression must evaluate to true or false.
The “statement” can be a group of statements in braces:
Syntax:
if(expression)
{
Statement 1;
Statement 2;
}
#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)
if (test expression)
{
true block statement(s)
}
else
{
false block statement(s)
}
Example
#include<stdio.h>
#include<conio.h>
main()
{
int age;
printf("n
enter your
age:");
scanf("%d",&ag
e);
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: 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();
//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:
if(condition)
{
if(condition)
{
statement
}
else
{
statement
}
}
else
{
statement
}
#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);
}
}
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.
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
#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
The ?: operator
Known as conditional operator
Used for making two-way decisions
Syntax:
Conditional expression ? exp1: exp2
Example:
//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
Odd
a number: 9
Enter
Even
a number: 8
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
goto label;
-------------
-------------
label;
statement;
label;
statement;
------------
-
------------
-
goto
label;
Forward jump Backward jump
Example:
#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---->%f
n",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)

presentation of data structure and algorithm

  • 1.
    Decision making statements inC Presented by: Omair Aquil - 35000323023 Bisweswar Mahato - 35000323013 Agnish Deyati - 35000323003
  • 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
  • 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
  • 4.
    Decision making statementsin C if statement switch statement Conditional operator statement goto statement
  • 5.
    If statement The expressionmust evaluate to true or false. The “statement” can be a group of statements in braces: Syntax: 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.
  • 7.
    If else statement  Anextension version of if statement.  Generally in the form of if (test expression) if (test expression) { true block statement(s) } else { false block statement(s) }
  • 8.
    Example #include<stdio.h> #include<conio.h> main() { int age; printf("n enter your age:"); scanf("%d",&ag e); if(age>=18) { printf("eligiblefor 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: 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("Enter coefficients a, band 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(); //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: if(condition) { if(condition) { statement } else { statement } } else { statement }
  • 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); } } 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.
  • 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
  • 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
  • 16.
    The ?: operator Knownas conditional operator Used for making two-way decisions Syntax: Conditional expression ? exp1: exp2
  • 17.
    Example: //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 Odd a number: 9 Enter Even a number: 8
  • 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 goto label; ------------- ------------- label; statement; label; statement; ------------ - ------------ - goto label; Forward jump Backward jump
  • 19.
    Example: #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---->%f n",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.