C Progrmming Unit II: K. Karunakar

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

C progrmming

Unit II
K. Karunakar
ptl.karuna@gmail.com
SELECTION – MAKING DECISIONS :

In a program all the instructions are executed


sequentially by default, when no repetition of some
calculations is necessary.

In some situation we may have to change the execution


order of statements based on condition or to repeat a
set of statements until certain conditions are met.

In that situation conditional and control statements are


very useful.

C language provides four general categories of control


structures.
Sequential structure:
Instructions are executed in sequence.
Ex: i=i+1;
J=j+1;
selection structure:
The sequence of the instructions are
determined by using the result of the condition.
Ex: if(x>y)
i=i+1;
else
j=j+1;
If the condition true, then the statement i=i+1 is
executed otherwise it executes j=j+1.
Iteration structures :
Statements are repeatedly executed. These
forms program
loops.
Ex: for(i=1; i<=5; i++)
{
i=i+1;
}
4. Encapsulation structure:
The other compound structures are included.
We can include an if statement in a for loop or a for
loop in a if statement.
C language provides the following conditional (Decision
making) Statements.
Simple if statement
if….else statement
nested if statement
else..if ladder
  The if statement:

The if statement is a powerful decision making


statement and is used to control the flow of execution of
statements. It is basically a two-way decision statement
and is used conjunction with an expression.
If the condition is true then true statements are
executed. The true
statements may be a single or multiple statements .
statements may be a single or multiple
statements .
condition

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.

But if there is more than one statement


the braces are compulsory.
Properties of an if statement:
If the condition is true then the simple or
compound condition statements are
executed.
If the condition is false it does not do any
thing.
The condition is given in parentheses and
must be evaluated as true(non-zero value)
or false(zero value).
If a compound structure is provided it must
be enclosed in opening and closing braces.
Write a program using simple if with single
statements .
 
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf (“enter a number < 10 \n”);
scanf (“%d”, &a);
if ( a < 10)
printf (“ given no is less than 10 \n”);
}
Write a program using simple if with
multiple statements.
#include<stdio.h>
#include<conio.h>
void main( )
{
int i, j, m ;
clrscr( );
printf(“Enter two numbers”);
m=scanf(“%d%d”, &i, &j);
if(m==2)
{
Printf(“the addition of numbers:%d”, i+j);
Printf(“the multiplication of numbers:%d” ,i*j);
}
getch ( );
}
The if- else statement:
 It is basically two way decision making statement and
always used in conjunction with condition.

It is used to control the flow of execution and also


used to carry out the logical test and then pickup one
of two possible actions depending on the logical test.

If the test-expression is true then the true –block


statement executed.
Otherwise the false – block statement are executed.
Syntax: false
True
Condition
 
if(condition)
{
True statements; True statements False statement
}
else
{
False statement;
}
Write a program using if-else statements.
#include<stdio.h>
#include<conio.h>
void main( )
{
int a, b;
clrscr( );
printf(“Enter two numbers”);
scanf(“%d%d”, &a, &b);
if(a>=b)
{
Printf(“a is big”);
}
else
{
Printf (“b is big” );
}
getch( );
}
Write a program using if-else statements.
#include<stdio.h>
#include<conio.h>
void main( )
{
int num, rem;
clrscr( );
printf(“Enter the number”);
scanf(“%d”, &num);
rem=num%2;
if(rem==0)
{
Printf(“it is even”);
}
else
{
Printf(“it is odd” );
}
getch( );
}
Nested if . . else statement:
 

When a series of if…else statements are occurred in a


program, we can write an entire if…else statement in
another if….else statement called nesting, and the
statement is called Nested if.
if (condition 1)
{
if (condition 2)
{
True Statement 2;
}
else
{
False Statement 2;
}
else
{
if(condition 3)
{
True statement 3;
}
else
{
False Statement 3;
}
}
Condition1 false

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 1 true False


Condition 2

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( );
}

You might also like