FCP 2 If Else
FCP 2 If Else
FCP 2 If Else
Programming
Control Instruction
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 Cs 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 !" ) ;
}
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 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.
#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 ;
void main( )
{ int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division ") ;
else
{
if ( per >= 50 )
printf ( "Second division" ) ;
else
{
if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "Fail" ) ;
}
}
}
Results
!x
!y
x && y
x || y
0
non-zero
non-zero
non-zero
0
non-zero 0
void main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
if ( ( per >= 50 ) && ( per < 60 ) )
printf ( "Second division" ) ;
if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "Third division" ) ;
if ( per < 40 )
printf ( "Fail" ) ;
}
Control Instruction
void main( )
{
int c = 3 ;
switch ( c )
{
case 'v' :
printf ( "I am in case v \n" ) ;
break ;
case 3 :
printf ( "I am in case 3 \n" ) ;
break ;
case 12 :
printf ( "I am in case 12 \n" ) ;
break ;
default :
printf ( "I am in default \n" ) ;
}
void main( )
{
int k, j = 2 ;
switch ( k = j + 1 )
{
case 0 :
printf ( "\nTailor") ;
case 1 :
printf ( "\nTutor") ;
case 2 :
printf ( "\nTramp") ;
default :
printf ( "\nPure Simple Egghead!" ) ;
}
void main( )
{
int i = 0 ;
switch ( i )
{
case 0 :
printf ( "\nCustomers are dicey" ) ;
case 1 :
printf ( "\nMarkets are pricey" ) ;
case 2 :
printf ( "\nInvestors are moody" ) ;
case 3 :
printf ( "\nAt least employees are good" ) ;
}
void main( )
{
int i = 1 ;
int ch = 'a' + 'b' ;
switch ( ch )
{
case 'a' :
case 'b' :
printf ( "\nYou entered b" ) ;
case 'A' :
printf ( "\na as in apple" ) ;
case 'b' + 'a' :
printf ( "\nYou entered a and b" ) ;
}
There are three ways for taking decisions in a program. First way
is to use the if-else statement, second way is to use conditional
operators and third way is to use the switch statement
The default scope of the if statement is only the next statement.
So, to execute more than one statement they must be written in a
pair of braces.
An if block need not always be associated with an else block.
However, an else block is always associated with an if
statement.
&& and || are binary operators, whereas, ! is a unary
operator.
In C every test expression is evaluated in terms of zero and nonzero values. A zero value is considered to be false and a non-zero
value is considered to be true.
Assignment statements used with conditional operators must be
enclosed within a pair of parenthesis.
Increment Operator
If we want to add one to a variable, we can say:
count = count + 1 ;
Programs often contain statements that increment
variables, so to save on typing, C provides these shortcuts:
count++ ; OR
++count ;
Both do the same thing. They change the value of count by
adding one to it.
count = 3 ;
amount = 2 * count++ ;
Preincrement Operator
If the ++ is before the variable, then the
incrementing is done first (a preincrement).
count = 3 ;
amount = 2 * ++count ;
#define
These macro definitions allow constant values to be declared
for use throughout your code.
Syntax:
#define CNAME value
#define CNAME (expression)
#include <stdio.h>
#define NAME "TechOnTheNet.com"
#define AGE 10
void main()
{
printf("%s is over %d years old.\n", NAME, AGE);
}
Review
Branching/Decision
Logical Operator
Conditional Operator
Increment and Decrement Operator
Branching
Condition
Loops
Statement
list
Condition
Statement
list
Condition
Statement
list
while(Condition)
{
Statement list
}
Example 1: while
char ans = n;
Printf(Great!!);
Can I put
; here?
No!!
Example 2: while
Will there be any problem?
int no_times;
Example 3: while
int a, b, sum;
printf(This program will return the );
Printf(summation of integers from a to b.\n\n);
printf(Input two integers a and b:);
printf(%d%d, &a, &b);
sum = 0;
while (a <= b)
{
sum += a;
a++;
}
sum = 0;
sum = sum + a;
+= ,*=,-=,/=,%=
compound assignment operator
They modify the value of the operand to
the left of them.
Example 4: while
int a, b, sum=0;
printf(This program will return the sum );
printf( of odd numbers between a and b.\n\n);
printf(Input two integers a and b:);
scanf(%d%d, &a ,&b);
while (a <= b)
{
if (a % 2)
sum += a;
a++;
}
3, 4, 5, 6 , 7
a
2, 3, 4, 5, 6 , 7
if (a % 2 == 0)
a++;
while (a <= b)
{
sum += a;
a += 2;
}
Example 5: while
3N+1 problem
long n,i=0;
printf(Input an integer:);
scanf(%d,&n);
while (n > 1)
{
if (n % 2)
n = 3*n+1;
else
n /= 2;
printf(%d:%d,++i,n);
}
printf(Done!!);
Input an integer:7
1:22
2:11
3:34
Input an integer:11
4:17
1:34
5:52
2:17
6:26
Input an integer:3759
3:52
7:13
1:11278
4:26
8:40
2:5639
5:13
9:20
3:16918
6:40
10:10
4:8459
7:20
11:5
5:25378
8:10
12:16
6:12689
9:5
13:8
7:38068
10:16
14:4
.....
11:8
15:2
.....
12:4
16:1
83:16
Done!! 13:2
84:8
14:1
Press any
key to
continue
85:4
Done!!
86:2
Press any key to continue
87:1
Done!!
Press any key to continue
Review
Loops
While loop
}
....
....
Printf(Great!!);
n(n-1)(n-2)...21
int n,f=1;
Printf(ENter a number to find factorial);
scanf(%d,&n);
for (i=2; i<=n; i++)
{
f *= i;
}
printf(The factorial of %d is %d ,n,f);
int n,f=1;
cin >> n;
for (i=2; i<=n; i++)
{
f *= i;
}
i=2;
while (i<=n)
{
f *= i;
i++;
}
int n,i;
int n,i;
n = 100;
n = 100;
i = 1;
v.s.
while (i <= n)
{
statement_list;
i++;
}
64
do-while
do
{
Statement
list
Condition
printf(Great!!);
do
{
Statement list
} while (Condition);
; is required
66
Example 1: do-while
int i;
....
do
{
printf( Please input a number between 10 and 20:;
printf(%d,i);
} while (i < 10 || i > 20);
......
67
Suppose your Rs 10000 is earning interest at 1% per month. How many months until you double
your money ?
my_money=10000.0;
n=0;
while (my_money < 20000.0) {
my_money = my_money*1.01;
n++;
}
printf (My money will double in %d months.\n,n);
The
statement in a for loop will force the
program to jump out of the for loop immediately.
continue
The
statement in a for loop will force the
program to update the loop condition and then
check the condition immediately.
printf(%d%d,a,b);
*************
*************
*************
*************
70
int a,b;
Printf(Enter two values);
scanf(%d%d,&a,&b);
for (int i = 0; i < a; i++)
{
for (int j=0; j<b; j++)
{
if (j > i)
break;
printf(*);
}
}
*
**
***
****
int a,b;
Printf(Enter two values);
scanf(%d%d,&a,&b);
*
**
***
****
if (j > i) break;
int a,b;
*************
************
***********
**********
73
int a,i,j;
scanf(%d,a);
*
***
*****
*******
*********
***********
74
Break in a loop
break
The
statement in a loop will force the
program to jump out of the loop immediately.
do
{
r = p % i;
if (r == 0) break;
i++;
} while (i < p);
do {
printf(Will you pass this exam?);
scanf(%c,&ans);
if (ans == y || ans == Y) break;
} while (ans != Y && ans != y);
cout << Great!!;
75
Continue in a loop
continue
The
statement in a loop will force the
program to check the loop condition immediately.
do {
printf(Will you pass this exam?);
scanf(%c,&ans);
if (and != Y && ans != y) continue;
printf(Great?);
break;
} while (true);
....
76
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,ch;
float x,t,s,r;
printf("\nENTER THE LIMIT");
scanf("%d",&n);
printf("\nENTER THE VALUE OF x:");
scanf("%f",&x);
r=((x*3.1415)/180);
t=r;
s=r;
i=2;
for(j=2;j<=n;j++)
{
t=(t*(-1)*r*r)/(i*(i+1));
s=s+t;
i=i+2;
}
printf("\nSUM OF THE GIVEN SINE SERIES IS %4f",s);
Formatted output
%d (print as a decimal integer)
%6d (print as a decimal integer with a width of at least 6 wide)
%f (print as a floating point)
%4f (print as a floating point with a width of at least 4 wide)
%.4f (print as a floating point with a precision of four characters after
the decimal point)
%3.2f (print as a floating point at least 3 wide and a precision of 2)