mesics lecture 6 control statement = if -else if__else

24
Control Statement in C www.eshikshak.co.in

Upload: eshikshak

Post on 19-May-2015

2.825 views

Category:

Technology


0 download

DESCRIPTION

Control Statement if, if...else, if...elseif...else

TRANSCRIPT

Page 1: Mesics lecture 6   control statement = if -else if__else

Control Statement in C

www.eshikshak.co.in

Page 2: Mesics lecture 6   control statement = if -else if__else

TYPES OF PROGRAMMING STRUCTURE

•All modern programming languages include support for three basic families of programming structures:•Sequential structures•Decision structures•Looping structures

Page 3: Mesics lecture 6   control statement = if -else if__else

WHAT IS A DECISION STRUCTURE?

•Decision structures consist of:•Some type of T/F test•One or more executable blocks of code

•Which block of code executes depends on the result of the T/F test (“the condition”).

Page 4: Mesics lecture 6   control statement = if -else if__else

CONTROL STATEMENT

•All the statements written in ‘C’ program executes from top to bottom one by one•Control statements are used to execute or transfer the execution control from one part of the program to another part based on a conditions, Such statements are known as Conditional Statements

Page 5: Mesics lecture 6   control statement = if -else if__else

CONTROL STATEMENT

•There are three types of control statements used in C language

1.if-else statement

2.switch statement

3.goto statement

Page 6: Mesics lecture 6   control statement = if -else if__else

IF / ELSE IF / ELSE SELECTION STRUCTURES

1) A simple if structure is called a single-selection structure because it either selects or ignores a single action.

2) The if / else structure is called a double-selection structure because it selects between two different actions.

3) Nested if / else structures test for multiple cases by placing if / else structures inside other if / else structures.

Page 7: Mesics lecture 6   control statement = if -else if__else

RELATIONAL OPERATORS

To develop valid conditions, we need to use relational operators.

Operator Meaning Operator Meaning

> Greater Than <=Less Than or Equal

to

>=Greater Than or

Equal to== Equality

< Less Than != Inequality

Page 8: Mesics lecture 6   control statement = if -else if__else

IF-ELSE STATEMENT

•It is used to execute a set of statements or single statement based on the evaluation of given condition•It is basically a two way decision statement and is used in conjunction with an expression

Page 9: Mesics lecture 6   control statement = if -else if__else

TWO WAY BRANCHING

Page 10: Mesics lecture 6   control statement = if -else if__else

SIMPLE IF STATEMENT

syntax :if(expression) { statement 1;

statement 2; .

statement N; } statement-x;Note : If expression is true than the set of statements will be executed after that statement-x will also be executed. Otherwise, the set of statements will be skipped and statements-x will be executed.

True

Page 11: Mesics lecture 6   control statement = if -else if__else

Result of expression

•Based on the evaluation of the conditional expression the result will be as follow

True Non-Zero

False Zero

Page 12: Mesics lecture 6   control statement = if -else if__else

EXAMPLE

void main(){

int i=5;if(i==5) {

printf(“You are inside the if true block”);printf(“\nvalue of i is equal to 5”);

}printf(“\nYou are outside the if block”);

}output :You are inside the if true blockvalue of i is equal to 5 You are outside the if block

Page 13: Mesics lecture 6   control statement = if -else if__else

EXAMPLE

void main(){

int i=-5;if(i==5) {

printf(“You are inside the if true block”);printf(“\nvalue of i is equal to 5”);

}printf(“\nYou are outside the if block”);

}output : You are outside the if block

Page 14: Mesics lecture 6   control statement = if -else if__else

IF-ELSE STATEMENT SYNTAX

if (expression){

statement 1;statement 2;..statement N;

}else{

statement 1;statement 2;..statement N;

}

True

False

Page 15: Mesics lecture 6   control statement = if -else if__else

IF-ELSE STATEMENT SYNTAX

The if selection structure is often written as:

if ( this logical expression is true )

statement ;

And, the if / else selection structure is often written as:

if ( this logical expression is true )

statement ;

else

statement ;

no semi-colon!

Page 16: Mesics lecture 6   control statement = if -else if__else

A VERY SIMPLE PROGRAM:

#include <stdio.h>int main ( ){ int a = 1, b = 2, c ;

if (a > b) { c = a; }

else { c = b; }}

Page 17: Mesics lecture 6   control statement = if -else if__else

A VERY SIMPLE PROGRAM:

#include <stdio.h>

int main ( )

{

int a = 1, b = 2, c ;

if (a > b) c = a; else

c = b;}

Page 18: Mesics lecture 6   control statement = if -else if__else

IF…ELSE IF…ELSE STATEMENT

•To perform multi-path decisions•It is collection of if statement with association of else statement

Page 19: Mesics lecture 6   control statement = if -else if__else

if(expression1){statement 1;}

else if(expression2){

statement 2;}

else if(expression3){

statement 3;} else { statement 4; //Also known as default

statement }

IF…ELSE IF…ELSE STATEMENT - SYNTAX

Page 20: Mesics lecture 6   control statement = if -else if__else

•The structure is also known as else if ladder•The conditions are evaluated from top of the ladder to downwards•if the expression1 evaluates to true than all the statements in this block will executed•Execution pointer will jump to the statement immediately after the closing curly braces•If all the given conditions evaluates to false than all the statements in else block will be executed•Else block is also known as default statement

IF…ELSE IF…ELSE STATEMENT

Page 21: Mesics lecture 6   control statement = if -else if__else

void main(){

int num;num=4;if(num>1){

printf(“It is positive value”);}

else if(num<1) {

printf(“It is negative value”); } else

{printf(“It is zero value”):

}getch();}

IF…ELSE IF…ELSE STATEMENT - EXAMPLE

Page 22: Mesics lecture 6   control statement = if -else if__else

EXAMPLE

•WAP to accept marks for 5 subjects from the user, calculate total and percentage and find the result as per the following criteria

Criteria Result

Greater than or equal to 70 Distinction

Between 60-70 First Class

Between 50-60 Second Class

Between 40-50 Pass Class

Less than 40 Fail

Page 23: Mesics lecture 6   control statement = if -else if__else

EXAMPLE

Calculate the comission of a salesman considering three regions X,Y and Z depending on the sales amount as follow

Area Code Sales Amount Comission

X <1000 10%

<5000 12%

>=5000 15%

Y <1500 10%

<7000 12%

>=7000 15%

Z <1200 10%

<6500 12%

>=6500 15%

Page 24: Mesics lecture 6   control statement = if -else if__else

EXAMPLE

•Big Bazzar gives festival discount on purchase of their products in the following percentages

i.If purchase amount < 1000 than 5% discount

ii.If purchase amount >=1000 than but <3000 then 10% discount

iii.If purchase amount >=3000 but <5000 then 12% discount

iv.If purchase amount > 5000 then 15% discount