a program is nothing but the execution of sequence of one or more instructions. on the basis of...

8
Unit 2 The Decision Control Structure LECTURE – 13 A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program. Test the logical conditions Control the flow of execution as per the selection. These conditions can be placed in the program using decision making statements. C Language too much be able to perform different sets of action depending on the circumstances. The major decision making instructions are listed below. The if statement The if –else statements The if-else-if ladder statements(nested if) The switch ( ) case statements. 1 Basic of Computer & 'C' programming

Upload: betty-norton

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 1

Unit 2 The Decision Control Structure LECTURE – 13

A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential.

To Alter the flow of a program.

Test the logical conditions

Control the flow of execution as per the selection.

These conditions can be placed in the program using decision making statements. C Language too much be able to perform different sets of action depending on the circumstances. The major decision making instructions are listed below.

The if statement

The if –else statements

The if-else-if ladder statements(nested if)

The switch ( ) case statements.

Page 2: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 2

Unit 2 The Decision Control Structure LECTURE – 13

1.The if statement:- C uses the keyword if to execute a set of command lines or one command line when the logical condition is true. It has only one option. The set of command lines or command lines are executed only when the logical condition is true.

Syntax of if statement

if(condition is true) [Note: – No semicolon]

statements;

Page 3: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 3

Unit 2 The Decision Control Structure LECTURE – 13

Program using if :-

Q. WAP to check whether the candidate age is > than 17 or not. If yes, display message “Eligible for voting”.

 

#include<stdio.h>

void main() {

int age;

printf(“ Enter your Age”);

scanf(“%d”, &age);

if (age>17)

printf(“ Eligible For Voting”); }

Page 4: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 4

Unit 2 The Decision Control Structure LECTURE – 13

Q. WAP to check two no are equal.

#include<stdio.h>

void main()

{

int m,n;

printf(“ Enter Two No”);

scanf(“%d%d”, &m,&n);

if (m-n= =0)

printf(“ \nTwo No are Equal”);

}

 

Page 5: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 5

Unit 2 The Decision Control Structure LECTURE – 13

2.The if –else statements:- The if –else statements takes care of true as well as false condition. It has two blocks, one block is for if and it is executed when the condition is true. The other block is of else and it is executed when the condition is false. The else statement cannot be used without if. No multiple else statements are allowed with one if.

Syntax of if-else statement

if(condition is true) [Note: – No semicolon]

execute the statements;

else

execute the statements;

Page 6: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 6

Unit 2 The Decision Control Structure LECTURE – 13

Programs using if-else :-

Q. Read the values of a, b, c through the keyboard. Add them and after addition check if it is in the range of 100 & 200 or not print separate message for each.

#include<stdio.h>

void main()

{

int a,b,c,d;

printf(“ Enter Three No a b c”);

scanf(“%d%d%d”, &a,&b,&c);

d=a+b+c;

if (d<=200 & d>=100)

printf(“ \nSum is %d which is in between 100 &200”, d);

else

printf(“\n Sum is %d which is out of range”, d );

}

Page 7: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 7

Unit 2 The Decision Control Structure LECTURE – 13

Q. If his basic salary is less then 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 salary. Find the 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 ); }

Page 8: A Program is nothing but the execution of sequence of one or more Instructions. On the basis of application it is essential. To Alter the flow of a program

Basic of Computer & 'C' programming 8

Unit 2 The Decision Control Structure LECTURE – 13

Thanks