Chapter 3
Conditional Instructions

Sometimes we want to watch comedy videos on YouTube if the day is Sunday
sometimes we order pizza if it’s our friends birthday
you might want to buy an umbrella if it’s raining and you have the money..
All these are decisions which depends on a condition being met
In C language to we must be able to execute instructions on a conditions being met
Decision making instructions in C
If else statement
Switch statement
If else statement
Syntax of if else is look like this
If (condition to be checked){
Statement if condition true
}else{
statement if condition false
}
Code example
Int a = 23;
If (a>18){
Printf(“you can drive\n”);
}
Note that else block is not necessary but optional
Relational operator in C
Relational operators are used to evaluate conditions ( true or false) inside the if statements some examples of relational operators are
== equal operator
>= greater than or equal to operator
<= less than or equal to operator
< less than
> greater than
!= Not operator
Important note
= is used for assignment whereas == is used for equality check
The condition can be any valid expression in C a non zero value is considered to be true
Logical operators
&& ,||,and ! are three logical operators in C are read as “and” “or” and “Not” they are used to provide logic to our C programs
Usage of logical operators

&& ➡️ And ➡️ is true when both the conditions are true
1 and 0 is evaluated as false
0 and 0 is evaluated as false
1 and 1 is evaluated as true
! ➡️ Returns true if given false and false if given true.( सच को झूठ और झूठ को सच बनाने वाले ऑपरेटर को नॉट ऑपरेटर कहते हैं)
!(3==3) evaluates to false
!(3 > 30) evaluates to true
|| ➡️ Or ➡️ is true when at least one of the conditions is true ( 1 or 0 ➡️ 1) ( 1 or 1 ➡️ 1) (या तो यह या तो यह)
As the number of conditions increases the level of indentations increases this reduces readability logical operators come to rescue in such cases.
Else if clause
Instead of using multiple if statements we can also use else if along with if does forming an if else if else ladder

if{
//Statement;
}else if{
//Statement;
}else if{
//Statement;
}else{
//Statement;
}
Using if else if else reduces indents the last else is optional also there can be any number of else if statement be used last else is executed only if all conditions fail
Operator precedence Table
Conditional operators
A shorthand if else can be written using the conditional or ternary operators.
Condition ? expression – if -true; expression-if-false
? Or ; are ternary operators.
Switch case control instruction
Switch case is used when we have to make a choice between number of alternatives for a given variable

Switch (integer expression)
{
case c 1:
Code;
case c 2:
Code;
case c 3:
Code;
case c 4:
Code;
case c default:
Code;
}
The value of integer expression is matched against C1 C2 C3 EC4 default if it matches any of these cases that case along with all subsequent case and default statement are executed.

Important notes
- We can use switch case statement even by writing cases in any order of our choice (not necessarily ascending order)
- Char values are allowed as they can be easily evaluated to an integer
- A switch can occur within another but in practice this is rearly done.
One Switch case code Example
// Find day using switch case
#include <stdio.h >
int main()
{
int day;
printf(“Enter day number between 1 to 7:>>\n”);
scanf(“%d”,&day);
switch(day)
{
case 1:
{
printf(“Monday\n”);
printf(“Starting Freash”);
break;
}
case 2:
{
printf(“Tuesday\n”);
printf(“doing daily work”);
break;
}
case 3:
{
printf(“Wednesday\n”);
printf(“working hard”);
break;
}
case 4:
{
printf(“Thursday\n”);
printf(“boring day”);
break;
}
case 5:
{
printf(“Friday\n”);
printf(“weekend coming”);
break;
}
case 6:
{
printf(“Saturday\n”);
printf(“it is weekend enjoy”);
break;
}
case 7:
{
printf(“Sunday\n”);
printf(“it’s party time”);
break;
}
default:
{
printf(“Invalid day number”);
}
printf(“\n Please check your number”);
return 0;
}
}
💻 Output//
Enter day number between 1 to 7:>>
6
Saturday
it is weekend enjoy
[Program finished]
