Introduction to C
Programming
Control Instruction
alter our actions by circumstances.
If the weather is fine, then I will go for a stroll
If the highway is busy I would take a diversion
notice that all these decisions depend on some condition being
met
C language too must be able to perform different sets of
actions depending on the circumstances
C has three major decision making instructions
If statement
if-else statement
SThe conditional operators.
switch statement
Decisions! Decisions!
Last class programs steps are executed sequentially i.e. in the
same order in which they appear in the program.
Many a times, we want a set of instructions to be executed in
one situation, and an entirely different set of instructions to be
executed in another situation.
This kind of situation is dealt in C programs using a decision
control instruction
(a) The if statement
(b) The if-else statement
(c) The conditional operators.
The if Statement
General form
if ( this condition is true )
{
execute this statement ;
}
But how do we express the condition itself in C?
we express a condition using C’s ‘relational’ operators.
this expression is true if
x == y x is equal to y
x != y x is not equal to y
x < y x is less than y
x > y x is greater than y
x <= y x is less than or equal to y
x >= y x is greater than or equal to y
/* Demonstration of if statement */
#include<stdio.h>
void main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}
Fcp chapter 2 upto_if_else (2)
While purchasing certain items, a discount of 10% is offered if
the quantity purchased is more than 1000. If quantity and price
per item are input through the keyboard, write a program to
calculate the total expenses.
void main( )
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
Fcp chapter 2 upto_if_else (2)
The current year and the year in which the employee joined the
organization are entered through the keyboard. If the number of
years for which the employee has served the organization is
greater than 3 then a bonus of Rs. 2500/- is given to the
employee. If the years of service are not greater than 3, then
the program should do nothing.
#include<stdio.h>
void main( )
{
int bonus, cy, yoj, yr_of_ser ;
printf ( "Enter current year and year of joining " ) ;
scanf ( "%d %d", &cy, &yoj ) ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{
bonus = 2500 ;
printf ( "Bonus = Rs. %d", bonus ) ;
}
}
The Real Thing
General form
if ( this condition is true )
{ execute this statement ; }
But if ( expression )
{ execute this statement ; }
expression can be any valid expression
if ( 3 + 2 % 5 )
printf ( "This works" ) ;
if ( a = 10 )
printf ( "Even this works" ) ;
if ( -5 )
printf ( "Surprisingly even this works" ) ;
C a non-zero value is considered to be true, whereas a 0 is
considered to be false.
The if-else Statement
Can we execute one group of statements if the expression
evaluates to true and another group of statements if the
expression evaluates to false?
Of course! This is what is the purpose of the else statement
General Form
if ( this condition is true )
{
execute this statement ;
}
else
{
execute this statement ;
}
The group of statements after the if upto and not including the
else is called an ‘if block’. Similarly, the statements after the
else form the ‘else block’.
Notice that the else is written exactly below the if.
Had there been only one statement to be executed in the if
block and only one statement in the else block we could have
dropped the pair of braces.
As with the if statement, the default scope of else is also the
statement immediately after the else.
Example. In a company an employee is paid as under: If his
basic salary is less than Rs. 1500, then HRA = 10% of basic
salary and DA = 90% of basic salary. If his salary is either
equal to or above Rs. 1500, then HRA = Rs. 500 and DA =
98% of basic salary. If the employee's salary is input through
the keyboard write a program to find his gross salary.
#include<stdio.h>
void main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
Nested if-elses
if we write an entire if-else construct within either the body of the if
statement or the body of an else statement. This is called ‘nesting’ of ifs.
if ( condition )
{
if ( condition )
do this ;
else
{
do this ;
and this ;
}
}
else
do this ;
#include<stdio.h>.
void main( )
{
int i ;
printf ( "Enter either 1 or 2 " ) ;
scanf ( "%d", &i ) ;
if ( i == 1 )
printf ( "You would go to heaven !" ) ;
else
{
if ( i == 2 )
printf ( "Hell was created with you in mind" ) ;
else
printf ( "How about mother earth !" ) ;
}
Forms of if
The if statement can take any of the following forms:
(a) if ( condition )
do this ;
(b) if ( condition )
{
do this ;
and this ;
}
(c) if ( condition )
do this ;
else
do this ;
Forms of if
(d) if ( condition )
{
do this ;
and this ;
}
else
{
do this ;
and this ;
}
Forms of if
(e) if ( condition )
do this ;
else
{
if ( condition )
do this ;
else
{
do this ;
and this ;
}
}
(f) if ( condition )
{
if ( condition )
do this ;
else
{
do this ;
and this ;
}
}
else
do this ;
Use of Logical Operators
C allows usage of three logical operators, namely, &&, || and !.
These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively.
The first two operators, && and ||, allow two or more
conditions to be combined in an if statement.

More Related Content

DOCX
C language assignment help
PPTX
CalStatementsNAV2013
PPTX
If Statements for PHP
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
PPTX
Lecture 2
PPTX
Php-Continuation
PPT
PPT
Visula C# Programming Lecture 3
C language assignment help
CalStatementsNAV2013
If Statements for PHP
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Lecture 2
Php-Continuation
Visula C# Programming Lecture 3

Similar to Fcp chapter 2 upto_if_else (2) (20)

PDF
C Programming Unit II Sharad Institute college
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
PDF
control statement
PPTX
control_structures_c_language_regarding how to represent the loop in language...
PDF
Control structure and Looping statements
PPT
CONTROLSTRUCTURES.ppt
DOCX
C Control Statements.docx
PPTX
Basics of Control Statement in C Languages
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
PPTX
computer programming Control Statements.pptx
PDF
Fundamental of Information Technology - UNIT 8
PDF
ICP - Lecture 7 and 8
RTF
control stucture in c language
PDF
Programming basics
PPTX
Control structures(class 02)
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
Conditional Statements in C.pptx
PPT
Lecture 9- Control Structures 1
C Programming Unit II Sharad Institute college
[ITP - Lecture 08] Decision Control Structures (If Statement)
control statement
control_structures_c_language_regarding how to represent the loop in language...
Control structure and Looping statements
CONTROLSTRUCTURES.ppt
C Control Statements.docx
Basics of Control Statement in C Languages
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
computer programming Control Statements.pptx
Fundamental of Information Technology - UNIT 8
ICP - Lecture 7 and 8
control stucture in c language
Programming basics
Control structures(class 02)
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
Conditional Statements in C.pptx
Lecture 9- Control Structures 1
Ad

Recently uploaded (20)

DOCX
Ibrahim Suliman Mukhtar CV5AUG2025.docx
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
Laparoscopic Colorectal Surgery at WLH Hospital
PDF
Nurlina - Urban Planner Portfolio (english ver)
PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PPTX
Climate Change and Its Global Impact.pptx
PPTX
Thinking Routines and Learning Engagements.pptx
PPTX
Reproductive system-Human anatomy and physiology
PPT
REGULATION OF RESPIRATION lecture note 200L [Autosaved]-1-1.ppt
PPTX
CAPACITY BUILDING PROGRAMME IN ADOLESCENT EDUCATION
PDF
Disorder of Endocrine system (1).pdfyyhyyyy
PDF
Journal of Dental Science - UDMY (2020).pdf
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PDF
Compact First Student's Book Cambridge Official
PPTX
PLASMA AND ITS CONSTITUENTS 123.pptx
PDF
Journal of Dental Science - UDMY (2022).pdf
Ibrahim Suliman Mukhtar CV5AUG2025.docx
What’s under the hood: Parsing standardized learning content for AI
Laparoscopic Colorectal Surgery at WLH Hospital
Nurlina - Urban Planner Portfolio (english ver)
ACFE CERTIFICATION TRAINING ON LAW.pptx
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Climate Change and Its Global Impact.pptx
Thinking Routines and Learning Engagements.pptx
Reproductive system-Human anatomy and physiology
REGULATION OF RESPIRATION lecture note 200L [Autosaved]-1-1.ppt
CAPACITY BUILDING PROGRAMME IN ADOLESCENT EDUCATION
Disorder of Endocrine system (1).pdfyyhyyyy
Journal of Dental Science - UDMY (2020).pdf
faiz-khans about Radiotherapy Physics-02.pdf
0520_Scheme_of_Work_(for_examination_from_2021).pdf
Compact First Student's Book Cambridge Official
PLASMA AND ITS CONSTITUENTS 123.pptx
Journal of Dental Science - UDMY (2022).pdf
Ad

Fcp chapter 2 upto_if_else (2)

  • 2. Control Instruction alter our actions by circumstances. If the weather is fine, then I will go for a stroll If the highway is busy I would take a diversion notice that all these decisions depend on some condition being met C language too must be able to perform different sets of actions depending on the circumstances C has three major decision making instructions If statement if-else statement SThe conditional operators. switch statement
  • 3. Decisions! Decisions! Last class programs steps are executed sequentially i.e. in the same order in which they appear in the program. Many a times, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation. This kind of situation is dealt in C programs using a decision control instruction (a) The if statement (b) The if-else statement (c) The conditional operators.
  • 4. The if Statement General form if ( this condition is true ) { execute this statement ; } But how do we express the condition itself in C? we express a condition using C’s ‘relational’ operators. this expression is true if x == y x is equal to y x != y x is not equal to y x < y x is less than y x > y x is greater than y x <= y x is less than or equal to y x >= y x is greater than or equal to y
  • 5. /* Demonstration of if statement */ #include<stdio.h> void main( ) { int num ; printf ( "Enter a number less than 10 " ) ; scanf ( "%d", &num ) ; if ( num <= 10 ) printf ( "What an obedient servant you are !" ) ; }
  • 7. While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses. void main( ) { int qty, dis = 0 ; float rate, tot ; printf ( "Enter quantity and rate " ) ; scanf ( "%d %f", &qty, &rate) ; if ( qty > 1000 ) dis = 10 ; tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ; printf ( "Total expenses = Rs. %f", tot ) ;
  • 9. The current year and the year in which the employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of service are not greater than 3, then the program should do nothing.
  • 10. #include<stdio.h> void main( ) { int bonus, cy, yoj, yr_of_ser ; printf ( "Enter current year and year of joining " ) ; scanf ( "%d %d", &cy, &yoj ) ; yr_of_ser = cy - yoj ; if ( yr_of_ser > 3 ) { bonus = 2500 ; printf ( "Bonus = Rs. %d", bonus ) ; } }
  • 11. The Real Thing General form if ( this condition is true ) { execute this statement ; } But if ( expression ) { execute this statement ; } expression can be any valid expression if ( 3 + 2 % 5 ) printf ( "This works" ) ; if ( a = 10 ) printf ( "Even this works" ) ; if ( -5 ) printf ( "Surprisingly even this works" ) ; C a non-zero value is considered to be true, whereas a 0 is considered to be false.
  • 12. The if-else Statement Can we execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the else statement General Form if ( this condition is true ) { execute this statement ; } else { execute this statement ; }
  • 13. The group of statements after the if upto and not including the else is called an ‘if block’. Similarly, the statements after the else form the ‘else block’. Notice that the else is written exactly below the if. Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces. As with the if statement, the default scope of else is also the statement immediately after the else.
  • 14. Example. In a company an employee is paid as under: If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary.
  • 15. #include<stdio.h> void main( ) { float bs, gs, da, hra ; printf ( "Enter basic salary " ) ; scanf ( "%f", &bs ) ; if ( bs < 1500 ) { hra = bs * 10 / 100 ; da = bs * 90 / 100 ; } else { hra = 500 ; da = bs * 98 / 100 ; } gs = bs + hra + da ; printf ( "gross salary = Rs. %f", gs ) ;
  • 16. Nested if-elses if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’ of ifs. if ( condition ) { if ( condition ) do this ; else { do this ; and this ; } } else do this ;
  • 17. #include<stdio.h>. void main( ) { int i ; printf ( "Enter either 1 or 2 " ) ; scanf ( "%d", &i ) ; if ( i == 1 ) printf ( "You would go to heaven !" ) ; else { if ( i == 2 ) printf ( "Hell was created with you in mind" ) ; else printf ( "How about mother earth !" ) ; }
  • 18. Forms of if The if statement can take any of the following forms: (a) if ( condition ) do this ; (b) if ( condition ) { do this ; and this ; } (c) if ( condition ) do this ; else do this ;
  • 19. Forms of if (d) if ( condition ) { do this ; and this ; } else { do this ; and this ; }
  • 20. Forms of if (e) if ( condition ) do this ; else { if ( condition ) do this ; else { do this ; and this ; } }
  • 21. (f) if ( condition ) { if ( condition ) do this ; else { do this ; and this ; } } else do this ;
  • 22. Use of Logical Operators C allows usage of three logical operators, namely, &&, || and !. These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. The first two operators, && and ||, allow two or more conditions to be combined in an if statement.