Conditional Checking
IF-ELSE
if(<expression>) if(<expression>)
{ {
<statement block> <statement block 1>
} }
or else
{
<statement block 2>
}
●
C treats logical values as integer data type with value 0 for false and
non-zero for true.
●
Therefore, the <statement block> in the first syntax and <statement
block 1> in the second syntax is executed if <expression> evaluates to
true, i.e. evaluates to a non-zero value.
●
Even a negative value is treated as true.
●
Note that enclosing the block within curly braces becomes mandatory
if the statement block consists of multiple statements.
●
The <statement block 2> is executed when the <expression> in the if
statement evaluates to false, i.e evaluates to a zero value.
Example
#include <stdio.h>
main()
{
int min, x, y;
printf("\nProvide two numbers:");
scanf("%d %d", &x, &y);
fflush(stdin);
/* Computes minimum of x and y */
if(x < y)
min = x;
else
min = y;
printf("%d is the minimum of %d and %d\n", min, x, y);
}
Nested IF
if(<expression1>) if(<expression1>)
{ {
<statement block1> if(<expression2>)
} {
else if(<expression2>) <statement block1>
{ }
<statement block2> else
} or {
else <statement block2>
{ }
<statement block3> }
} else
{
<statement block3>
}
Example
#include <stdio.h> else
#define TRUE 1 {
#define FALSE 0 EQUAL = FALSE;
if(x > y)
main() {
{ min = y;
int EQUAL, min, max, x, y; max = x;
printf("\nProvide two numbers:"); }
scanf("%d %d", &x, &y); else
fflush(stdin); {
–> min = x;
/* Compare x and y */ max = y;
if(x == y) }
{ }
EQUAL = TRUE;
min = max = x; if(EQUAL)
} printf("Both x and y are equal\n");
else
printf("Minimum: %d, Maximum: %d \n",
min, max);
}
#include <stdio.h>
main()
Identify the mistake in the following C program. {
float basic;
printf("Enter basic: ");
scanf("%f", &basic);
fflush(stdin);
if(basic = 0)
printf("Invalid Input\n");
else
printf("Basic is %.2f", basic);
}
#include <stdio.h>
main()
{
Predict the output of the following C program. int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{
a += b;
a ++;
}
if(a > c)
a *= c;
else
c -= (a + b);
printf("%d %d %d\n", a, b, c);
}
switch statement
●
The switch statement is usefull when there are
multiple if-else conditions to be checked.
switch statement
switch <expression> case <expression n>
{ <statement block n>
case <expression 1> break;
<statement block 1>
break; default:
//When none of the above
case <expression 2> expressions evaluate to true
<statement block 2> –> <statement block n+1>
break; break;
}
case <expression 3>
<statement block 3>
break;
...
switch statement
●
The switch statement is a compound statement
which specifies alternate course of actions.
●
Each alternative is expressed as a group of one
or more statements which are identified by one
or more labels called case labels.
Example
●
The following two different programs, intended
to perform the same task, illustrate how nested
if..else can be replaced by a switch..case
construct.
Nested-if version
#include <stdio.h> else if(category == 'T')
main() {
{ printf("M.TECH Student \n");
char category; /* Statements for M.TECH
printf("Enter Category: "); Processing will go here */
category = getchar(); }
fflush(stdin); else if(category == 'P')
{
if(category == 'B') printf("Ph. D. Student \n");
{ /* Statements for Ph.D
printf("B.TECH students\n"); –> Processing will go here */
/* Statements for B.TECH }
Processing will go here */ else
} {
else if(category == 'M') printf("ERROR\n");
{ /* Statements for Error
printf("M.Sc. Student \n"); Processing will go here */
/* Statements for M.Sc }
Processing will go here */ }
}
Switch version
#include <stdio.h> case 'T' :
main() printf("M.TECH Student \n");
{ /* M.TECH Processing */
char category; break;
printf("Enter Category: ");
category = getchar(); case 'P' :
fflush(stdin); printf("Ph. D. Student \n");
/* Ph. D. Processing */
switch(category) break;
{ –>
case 'B' : default :
printf("B.TECH students\n"); printf("ERROR\n");
/* B.TECH Processing */ /* Error Processing */
break; }
}
case 'M' :
printf("M.Sc. Student \n");
/* M.Sc Processing */
break;
END
Q&A