Introduction
- Allow different sets of instructions to be executed depending on truth or falsity of alogical condition also called Branching
- How do we specify conditions?
- Using expressions
- non-zero value means condition is true
- value 0 means condition is false
- Usually logical expressions, but can be any expression
- The value of the expression will be used
The if Statement
Equality and Assignment Operators
- Dangerous error
- Does not ordinarily cause syntax errors
- Any expression that produces a value can be used in control structures
- Nonzero values are true, zero values are false
- Example: if ( payCode = 4 )
printf( "You get a bonus!\n" );
Note:WRONG! Will always print the line
Multiple Statements within if
#include<stdio.h> int main() { int num=19; if(num < 10) { printf("Number is %d.\n",num); printf("The value is less than 10"); } else { printf("The value is greater than 10\n"); printf("Number is %d.\n",num); } return 0; }
Output: The value is greater than 10 Number is 19.
The if-else Statement

Illustration 4.1 void main() { int marks; scanf("%d", &marks); if (marks >= 80) printf ("A") ; else if (marks >= 70) printf ("B") ; else if (marks >= 60) printf ("C") ; else printf ("Failed") ; }
Nested if-else
- It is possible to nest if-else statements,one within another
- All "if" statements may not be having the "else" part
- Rule to be remembered:
-
Confusion??
-
An "else" clause is associated with the closest preceding unmatched
"if"
Illustration 4.2 int main () { int a = 1; if( a < 10) { if( a == 1) { printf("Value of a is:%d\n",a); } else { printf("The value is greater than 100."); } } else { printf("The value is greater than 10."); } return 0; }
Output:
Value of a is:1
The Terniary Operator ?:
This makes use of an expression that is either non-0 or 0.
An
appropriate value is selected,depending on the value of the expression
Syntax:-
Condition ? True Statement : False Statement
Note:if condition will true then true statement will be executed else false statement will be executed.
#include<stdio.h> void main() { int marks=80; if (marks >= 60) printf("Passed \n"); else printf("Failed \n"); return 0; } |
#include<stdio.h> void main() { int marks=80; (marks >= 60) ? printf("Passed \n") : printf("Failed \n"); } |
Output:Passed | Output:Passed |
#include<stdio.h> void main() { int a=5; int b=3; if((a > 10) && (b < 5)) x = a + b; else x = a-b; printf("x=%d",x); return 0; } |
void main() { int a=5; int b=3; x = ((a > 10) && (b < 5)) ? a + b : a-b printf("x=%d",x); } |
Output:x=2 | Output:x=2 |
The switch statement
- An alternative to writing lots of if-else in some special cases
- This causes a particular group of statements to be chosen from several available groups based on equality tests only
- Uses switch statement and case labels
-
Syntax
switch (expression) { case const-expr-1: S-1 case const-expr-2: S-2 : case const-expr-m: S-m default: S }
- expression is any integer-valued expression
-
const-expr-1, const-expr-2,..are any constant integer- valued
expressions
Values must be distinct - S-1, S-2,...,S-m, S are statements/compound statements
- Default is optional, and can come anywhere (not necessarily at the end as shown)
Behaviour of switch
- expression is first evaluated
- It is then compared with const-expr-1,constexpr-2,...for equality in order
- If it matches any one, all statements from that point till the end of the switch are executed (including statements for default, if present)
- Use break statements if you do not want this (see example)
- Statements corresponding to default, if present,are executed if no other expression matches
Example

The break statement
- Used to exit from a switch or terminate from a loop
- With respect to "switch", the "break" statement causes a transfer of control out of the entire "switch" statement, to the first statement following the "switch" statement
- Break out of the loop body { }
- It can use with while, do while, for, switch,
- It does not work with if, else
- It Causes immediate exit from a while,do/while, for or switch structure
- Program execution continues with the first statement after the structure
- It Can be used with other statements also
Chapter 4 Quiz
Q1).Which loop is faster in C Language, for, while or Do While?
Q2).What will be the correct syntax for running two variables in for
loop simultaneously?
Q3).In the context of "break" and "continue" statements in C, pick the
best statement.
Q4).In _______, the bodies of the two loops are merged together to
form a single loop provided that they do not make any references to
each other.
Q5).If switch feature is used, then