C Progrmming Unit II: K. Karunakar
C Progrmming Unit II: K. Karunakar
C Progrmming Unit II: K. Karunakar
Unit II
K. Karunakar
ptl.karuna@gmail.com
SELECTION – MAKING DECISIONS :
Syntax:
if (condition is true)
True
{ statements
True statements;
}
If there is only one statement in the if
blocks (or) else block then the braces is
optional.
true
true Condition3 false
false
true Conditio2
True
Statement 3 False
statement 3
True False
Statement2 statement2
Write a program to find biggest of
the given 3 numbers
else
#inlcude<stdio.h> {
#include<conio.h> printf ( “ c is big %d \n” , c);
void main() }
{ }
int a,b,c; else
clrscr(); {
if ( b > c)
printf (“ enter 3 numbers \n”);
{
scanf (“%d %d %d”, &a, &b, &c);
printf ( “ b is big %d \n”, b );
if ( a > b)
}
{ else
if ( a> c) {
{ printf ( “ c is big %d \n”, c);
printf ( “ a is big %d \n” , a); }
} }
The else.. if ladder:
There is another way of putting if’s together when
multipath decisions are involved.
A multipath decision is a chain of ifs in which the
statement associated which the statement associated
with each else is an if.
This construct is known as the else if ladder.
The conditions are evaluated from the top downwards.
As soon as a true condition is found, the statement
associated with it is executed and the control is
transferred to the statement-x.
when all the n conditions become false, then the final
else containing the default-statement will be executed.
Syntax: if(condition 1)
{
Statement 1;
}
else if(condition 2)
{
Statement 2;
}
else if(condition 3)
{
Statement 3;
}
else
{
Default-statements;
}
true False
Condition 1
Statement 2 true
False
Condition 3
Statement 3 Default
Statement
W.a.p using If else ladder to find biggest of three
numbers.
#include<stdio.h>
#include<conio.h>
Void main( )
{
int a,b,c;
printf (“ enter a, b, c values”);
scanf (“%d %d %d”, &a, &b, &c);
if((a>b)&&(a>c))
{
Printf (“a is big”);
}
else if(b>c)
{
Printf (“b is big”);
}
else
{
Printf(“c is big”);
}
getch( );
}
W.A.P using If else ladder to grade the student according
to the following rules.
Marks Grade
70 to 100 DISTINCTION
60 to 69IST CLASS
50 to 59 IIND CLASS
40 to 49 PASS CLASS
0 to 39 FAIL
#include <stdio.h>
Void main ()
{
int marks; //variable declaration
printf ("Enter marks\n"); //message to the user
scanf ("%d", &marks); //read and store the input marks.
if (marks <= 100 && marks >= 70)
printf ("\n Distinction") ;
else if (marks >= 60)
printf("\n First class");
else if (marks >= 50) //else if marks is greater than 50 print
printf ("\n second class"); //Second class
else if (marks >= 35) //else if marks is greater than 35 print
printf ("\n pass class"); //pass class
else
printf ("Fail"); //If all condition fail default condition
}
Switch:
The switch statement is used to execute a particular
group of statements from several available group of
statements.
It allows us to make a decision from the number of
choices.
It is a multiway decision statement.
It tests the value of given variable or expression against
a list of case values and when a match is found, a block
of statements associated with that case is executed.
The expression follows the keyword switch.
Syntax:
Switch(expression)
{
Case 1:
Block1;
Break;
Case 2:
Block2;
Break;
.
.
.
Default:
Default block;
Break;
}
Switch
Case 1 statements
Case 2 statements
Default statements
*
*
*
Rules for writing switch ( ) statements:
The expression in switch statements must be an integer value or a
character constant.
No real numbers are used in an expression.
Each case block and default blocks must be terminated with break
statement.
The default is optional and can be placed anywhere, but usually placed
at end.
The case keyword must terminate with colon(:).
No two case constants are identical.
The case labels must be constants.
The switch can be nested.
The value of switch expression is compared with the case constant
expression in the order specified. i.e. from top to bottom.
In the absence of break statement, all statements that are followed by
matched cases are executed.
W.a.p to print the given number is odd/even
using switch statement
#include<stdio.h>
#include<conio.h>
Void main( )
{
int i, n;
clrscr ( );
printf (“enter a number”);
scanf (“%d”, &n);
for(i=1; i<=n; i=i+1)
{
Switch(i%2)
{
Case 0:
Printf(“the number %d is even”, i);
Break;
Case 1:
Printf(“ the number %d is odd”, i);
Break;
} }
getch( );
}