Decision Statements
in C
Definition
 Decision making is about deciding the
order of execution of statements based
on certain conditions or repeat a group of
statements until certain specified
conditions are met.
 Decision-making statement checks the
given condition and then executes its
sub-block.
 The decision statement decides which
statement to execute after the success
or failure of a given condition.
 The conditional statements use relational
operators.
Form of a typical decision
making structure
 C language handles decision-making
by supporting the following
statements:
If statement
switch case statement
conditional operator statement
goto statement
If statement
 The if statement in C language is used to
perform operation on the basis of
condition. By using if-else statement, you
can perform operation either condition is
true or false.
 There are many ways to use if statement
in C language:
If statement
If-else statement
If else-if ladder
Nested if
If statement
 The single if statement in C language
is used to execute the code if
condition is true.
 Syntax:
Flowchart:
Example
Program to check whether the entered number is
even.
#include<stdio.h>
#include<conio.h>
void main(){
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
getch();
}
If-else Statement
 The if-else statement in C language is
used to execute the code if condition
is true or false.
 Syntax:
Flowchart:
1
Example
Program to check whether the entered
number is even.or odd.
#include<stdio.h>
#include<conio.h>
void main(){
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
getch();
}
If else-if ladder Statement
 The if else-if statement is used to
execute one code from multiple
conditions.
 Syntax:
Flowchart:
Example
#include<stdio.h>
#include<conio.h>
void main(){
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
getch();
}
Nested Ifs
 Syntax:
Example :
void main( )
{
int a,b,c;
printf("enter 3 number");
scanf("%d%d%d",&a,&b,&c);
if(a>b) {
if( a > c) {
printf("a is greatest");
}
else {
printf("c is greatest");
}
}
else {
if( b> c) {
printf("b is greatest");
}
else {
printf("c is greatest");
}
}
}
Points to Remember:
 In if statement, a single statement can be included
without enclosing it into curly braces { }
int a = 5;
if(a > 4)
printf("success");
No curly braces are required in the above case, but if
we have more than one statement
inside ifcondition, then we must enclose them
inside curly braces.
 == must be used for comparison in the expression
of if condition, if you use = the expression will
always return true, because it performs assignment
 Other than 0(zero), all other values
are considered as true.
if(27)
printf("hello");
In above example, hello will be printed.
Switch Statement
 The switch statement in C language is
used to execute the code from
multiple conditions. It is like if else-if
ladder statement.
 Syntax:
Rules for switch statement in C
language
 The expression (after switch keyword) must
yield an integer value i.e it can be an integer
or a variable or an expression that evaluates
to an integer.
 The case label i.e values must be unique.
 The case label must end with a colon(:)
 The following line, after case statement, can
be any valid C statement.
 The break statement in switch case is not
must. It is optional. If there is no break
statement found in switch case, all the cases
will be executed after matching the case
value. It is known as fall through state of C
switch statement.
Example
Points to Remember:
 We don't use those expressions to
evaluate switch case, which may
return floating point values or strings.
 break statements are used to exit the
switch block. It isn't necessary to use
break after each block, but if you do
not use it, then all the consecutive
block of codes will get executed after
the matching block.
int i = 1;
switch(i) {
case 1: printf("A");
// No break
case 2: printf("B");
// No break
case 3: printf("C");
break;
}
Output: A B C
The output was supposed to be only A because only the first
case matches, but as there is no break statement after that
block, the next blocks are executed, until it encounters a
break or reaches the end of the switch block.
 default case is executed when none of
the mentioned cases matches the switch
expression. Default case can be placed
anywhere in the switch case. Even if we
don't include the default case, switch
statement works.
 Nesting of switch statements are
allowed, which means you can have
switch statements inside another switch.
However, nested switch statements
should be avoided as it makes program
more complex and less readable.
Flowchart:
Example:
#include<stdio.h>
void main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
}
Example:
#include<stdio.h>
void main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10n");
case 50:
printf("number is equal to 50n");
case 100:
printf("number is equal to 100n");
default:
printf("number is not equal to 10, 50 or 100");
}
}
Difference between switch and
if
 if statements can evaluate float
conditions. switch statements cannot
evaluate float conditions.
 if statement can evaluate relational
operators. switch statement cannot
evaluate relational operators i.e they
are not allowed in switch statement.
Break Statement
 The break statement in C language is
used to break the execution of loop
(while, do while and for) and switch
case.
 In case of inner loops, it terminates
the control of inner loop only.
 There can be two usage of C break
keyword:
◦ With switch case
◦ With loop
Syntax:
 The jump statement in c break syntax
can be while loop, do while loop, for
loop or switch case.
 Flowchart:
 When break statement is encountered
inside a loop, the loop is immediately exited
and the program continues with the
statement immediately following the loop.
Example:
#include <stdio.h>
#include <conio.h>
void main(){
int i=1;//initializing a local variable
clrscr();
//starting a loop from 1 to 10
for(i=1;i<=10;i++){
printf("%d n",i);
if(i==5){//if value of i is equal to 5, it will break the loop
break;
}
}//end of for loop
getch();
}
Example:
#include <stdio.h>
#include <conio.h>
void main(){
int i=1,j=1;//initializing a local variable
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("%d &dn",i,j);
if(i==2 && j==2)
{
break;//will break loop of j only
}
}
}//end of for loop
getch();
}
Continue statement
 The continue statement in C
language is used to continue the
execution of loop (while, do while and
for). It is used with if condition within
the loop.
 In case of inner loops, it continues the
control of inner loop only.
 Syntax:
 The jump statement can be while, do
while and for loop.
 It causes the control to go directly to
the test-condition and then continue
the loop process. On encountering
continue, cursor leave the current
cycle of loop, and starts with the next
cycle.
Example
#include <stdio.h>
void main(){
int i=1;//initializing a local variable
//starting a loop from 1 to 10
for(i=1;i<=10;i++){
if(i==5){//if value of i is equal to 5, it will continue the loop
continue;
}
printf("%d n",i);
}//end of for loop
}
Goto Statement
 The goto statement is known as jump
statement in C language. It is used to
unconditionally jump to other label. It
transfers control to other parts of the
program.
 It is rarely used today because it
makes program less readable and
complex.
 Syntax:
Example:
#include <stdio.h>
#include <conio.h>
void main() {
int age;
clrscr();
ineligible:
printf("You are not eligible to vote!n");
printf("Enter you age:n");
scanf("%d", &age);
if(age<18)
goto ineligible;
else
printf("You are eligible to vote!n");
getch();
}

Decision statements in c language

  • 1.
  • 2.
    Definition  Decision makingis about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met.  Decision-making statement checks the given condition and then executes its sub-block.  The decision statement decides which statement to execute after the success or failure of a given condition.  The conditional statements use relational operators.
  • 3.
    Form of atypical decision making structure
  • 4.
     C languagehandles decision-making by supporting the following statements: If statement switch case statement conditional operator statement goto statement
  • 5.
    If statement  Theif statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false.  There are many ways to use if statement in C language: If statement If-else statement If else-if ladder Nested if
  • 6.
    If statement  Thesingle if statement in C language is used to execute the code if condition is true.  Syntax:
  • 7.
  • 8.
    Example Program to checkwhether the entered number is even. #include<stdio.h> #include<conio.h> void main(){ int number=0; clrscr(); printf("enter a number:"); scanf("%d",&number); if(number%2==0){ printf("%d is even number",number); } getch(); }
  • 9.
    If-else Statement  Theif-else statement in C language is used to execute the code if condition is true or false.  Syntax:
  • 10.
  • 11.
    Example Program to checkwhether the entered number is even.or odd. #include<stdio.h> #include<conio.h> void main(){ int number=0; clrscr(); printf("enter a number:"); scanf("%d",&number); if(number%2==0){ printf("%d is even number",number); } else{ printf("%d is odd number",number); } getch(); }
  • 12.
    If else-if ladderStatement  The if else-if statement is used to execute one code from multiple conditions.  Syntax:
  • 13.
  • 14.
    Example #include<stdio.h> #include<conio.h> void main(){ int number=0; clrscr(); printf("entera number:"); scanf("%d",&number); if(number==10){ printf("number is equals to 10"); } else if(number==50){ printf("number is equal to 50"); } else if(number==100){ printf("number is equal to 100"); } else{ printf("number is not equal to 10, 50 or 100"); } getch(); }
  • 15.
  • 16.
    Example : void main() { int a,b,c; printf("enter 3 number"); scanf("%d%d%d",&a,&b,&c); if(a>b) { if( a > c) { printf("a is greatest"); } else { printf("c is greatest"); } } else { if( b> c) { printf("b is greatest"); } else { printf("c is greatest"); } } }
  • 17.
    Points to Remember: In if statement, a single statement can be included without enclosing it into curly braces { } int a = 5; if(a > 4) printf("success"); No curly braces are required in the above case, but if we have more than one statement inside ifcondition, then we must enclose them inside curly braces.  == must be used for comparison in the expression of if condition, if you use = the expression will always return true, because it performs assignment
  • 18.
     Other than0(zero), all other values are considered as true. if(27) printf("hello"); In above example, hello will be printed.
  • 19.
    Switch Statement  Theswitch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.  Syntax:
  • 20.
    Rules for switchstatement in C language  The expression (after switch keyword) must yield an integer value i.e it can be an integer or a variable or an expression that evaluates to an integer.  The case label i.e values must be unique.  The case label must end with a colon(:)  The following line, after case statement, can be any valid C statement.  The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.
  • 21.
  • 22.
    Points to Remember: We don't use those expressions to evaluate switch case, which may return floating point values or strings.  break statements are used to exit the switch block. It isn't necessary to use break after each block, but if you do not use it, then all the consecutive block of codes will get executed after the matching block.
  • 23.
    int i =1; switch(i) { case 1: printf("A"); // No break case 2: printf("B"); // No break case 3: printf("C"); break; } Output: A B C The output was supposed to be only A because only the first case matches, but as there is no break statement after that block, the next blocks are executed, until it encounters a break or reaches the end of the switch block.
  • 24.
     default caseis executed when none of the mentioned cases matches the switch expression. Default case can be placed anywhere in the switch case. Even if we don't include the default case, switch statement works.  Nesting of switch statements are allowed, which means you can have switch statements inside another switch. However, nested switch statements should be avoided as it makes program more complex and less readable.
  • 25.
  • 26.
    Example: #include<stdio.h> void main(){ int number=0; printf("entera number:"); scanf("%d",&number); switch(number){ case 10: printf("number is equals to 10"); break; case 50: printf("number is equal to 50"); break; case 100: printf("number is equal to 100"); break; default: printf("number is not equal to 10, 50 or 100"); } }
  • 27.
    Example: #include<stdio.h> void main(){ int number=0; printf("entera number:"); scanf("%d",&number); switch(number){ case 10: printf("number is equals to 10n"); case 50: printf("number is equal to 50n"); case 100: printf("number is equal to 100n"); default: printf("number is not equal to 10, 50 or 100"); } }
  • 28.
    Difference between switchand if  if statements can evaluate float conditions. switch statements cannot evaluate float conditions.  if statement can evaluate relational operators. switch statement cannot evaluate relational operators i.e they are not allowed in switch statement.
  • 29.
    Break Statement  Thebreak statement in C language is used to break the execution of loop (while, do while and for) and switch case.  In case of inner loops, it terminates the control of inner loop only.  There can be two usage of C break keyword: ◦ With switch case ◦ With loop
  • 30.
    Syntax:  The jumpstatement in c break syntax can be while loop, do while loop, for loop or switch case.  Flowchart:
  • 31.
     When breakstatement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.
  • 32.
    Example: #include <stdio.h> #include <conio.h> voidmain(){ int i=1;//initializing a local variable clrscr(); //starting a loop from 1 to 10 for(i=1;i<=10;i++){ printf("%d n",i); if(i==5){//if value of i is equal to 5, it will break the loop break; } }//end of for loop getch(); }
  • 33.
    Example: #include <stdio.h> #include <conio.h> voidmain(){ int i=1,j=1;//initializing a local variable for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { printf("%d &dn",i,j); if(i==2 && j==2) { break;//will break loop of j only } } }//end of for loop getch(); }
  • 34.
    Continue statement  Thecontinue statement in C language is used to continue the execution of loop (while, do while and for). It is used with if condition within the loop.  In case of inner loops, it continues the control of inner loop only.  Syntax:  The jump statement can be while, do while and for loop.
  • 35.
     It causesthe control to go directly to the test-condition and then continue the loop process. On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle.
  • 36.
    Example #include <stdio.h> void main(){ inti=1;//initializing a local variable //starting a loop from 1 to 10 for(i=1;i<=10;i++){ if(i==5){//if value of i is equal to 5, it will continue the loop continue; } printf("%d n",i); }//end of for loop }
  • 37.
    Goto Statement  Thegoto statement is known as jump statement in C language. It is used to unconditionally jump to other label. It transfers control to other parts of the program.  It is rarely used today because it makes program less readable and complex.  Syntax:
  • 38.
    Example: #include <stdio.h> #include <conio.h> voidmain() { int age; clrscr(); ineligible: printf("You are not eligible to vote!n"); printf("Enter you age:n"); scanf("%d", &age); if(age<18) goto ineligible; else printf("You are eligible to vote!n"); getch(); }