0% found this document useful (0 votes)
56 views

C Learn It

This document discusses different types of decision-making and branching statements in programming, including if, if-else, nested if-else, and else if ladder statements. It provides syntax, flowcharts, and examples of each type of statement. The key points are: - Decision-making statements like if, switch, and ternary operator control program flow based on conditions. - If statements execute code if a condition is true, else if adds alternative conditions, and if-else adds a block for false conditions. - Nested if statements allow multiple levels of conditions, and else if ladders provide multiple options like a series of if-else statements. - Examples demonstrate using each type of statement to check conditions

Uploaded by

Ishtiaque Anwar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

C Learn It

This document discusses different types of decision-making and branching statements in programming, including if, if-else, nested if-else, and else if ladder statements. It provides syntax, flowcharts, and examples of each type of statement. The key points are: - Decision-making statements like if, switch, and ternary operator control program flow based on conditions. - If statements execute code if a condition is true, else if adds alternative conditions, and if-else adds a block for false conditions. - Nested if statements allow multiple levels of conditions, and else if ladders provide multiple options like a series of if-else statements. - Examples demonstrate using each type of statement to check conditions

Uploaded by

Ishtiaque Anwar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DECISION MAKING AND BRANCHING

In the monolithic program the sequences of instructions are executed in the same order as they appear in the program. Quite often it is desirable to alter the sequence of the statements in the program depending upon certain circumstances (condition).For example
1) If bank balance is zero

Borrow money

2) If code is 1 Person is male

3) If age is more than 55 Person is retired

These conditions can be placed in the program using decision-making statement.:1) 2) 3) 4) if statement switch statement Conditional operator statement (Ternary operator) goto statement

These statements are popularly known as Decision making statements. Since these statements control the flow of execution of statements. So they are also known as Control statements.

Different forms of if-statement 1) 2) 3) 4) Simple if statement. If_else statement Nested if_else statement. else if ladder. 1.) SIMPLE if STATEMENT Syntax : if (test expression) { statement-block ; */ ( may be single statement or group of statements ) */ } statement-X ; Note : 1) The conditional statement (Test expression ) should not be terminated with semi-colons (;) . 2) The default scope of statement is one statement. Flow-chart :

Demonstration : 1) Write a program to check whether the entered number is less than 10? If Yes, Display What an obedient servant you are .. 2) WAP to check equivalence of two numbers. 3) WAP to check that whether the entered number is divisible by 3 or not . 4) WAP to check whether the candidates age is greater than 17 or not. If Yes , Display message Eligible for voting .. 5) Enter only three numbers and calculate their sum and multiplication. 6) While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard. WAP to calculate the total expenses. 7) WAP to read four values a,b,c,d from the keyboard and evaluate the ratio of (a+b) to (c+d) and prints the result. If (c-d) is not equal to zero. 8) WAP that counts number of Boys whose weight is less than 50 kg and height is greater than 170 cm. 9) The current year and the year in which the employee joined the organization are entered through the keyboard. If the numbers of years for which the employee has served the organization is greater than 3 years than a bonus of Rs. 2500 is given to the employee . If the years of service are not greater than 3, then the program do not do anything . 10) WAP to find the largest of three numbers .

Conclusion : We observed that the if statement executes only when the condition following if is true. It does nothing when the condition is false.

2.) THE if__else STATEMENT The if_else statement is an extension of the simple if statement . It takes care of true as well as false conditions. It has two blocks. 1) One block is for if and it is executed when the condition is true . 2) The other block is of else and it is executed when the condition is false. Note : 1) The else statement cannot be used without if . 2) No multiple else statements are allowed with one if . Syntax : if ( Test expression ) { True-Block Statement (s) ; } else { False-Block Statement (s) ; } statement x ; Flow-Chart :

Demonstration : 1) Write a program to check whether the entered number is less than 10? If Yes, Display What an obedient servant you are . & If No, Dislpay You are not obedient servant . . 2) WAP to check equivalence of two numbers. Display the proper messages. 3) WAP to check that whether the entered number is divisible by 3 or not . Display the proper messages. 4) WAP to check whether the candidates age is greater than 17 or not. If Yes , Display message Eligible for voting . if No display message Not eligible for voting .. 5) Enter only three numbers and calculate their sum and multiplication. Display proper messages . 6) WAP to read four values a,b,c,d from the keyboard and evaluate the ratio of (a+b) to (c+d) and prints the result. If (c-d) may be equal to zero. 7) 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. 8) WAP to find the roots of a quadratic equation by using if_else condition. 9) WAP to calculate the square of those numbers only whose least significant digit is 5. 10) WAP to calculate the salary of medical representative based on the sales. Bonus and Incentives to be offered to him will be based on total sales. If the sale exceeds Rs.100000 follow the particulars of Table (1) otherwise (2) 11) In a company an employee is paid as under. If his Basic salary is less than Rs. 1500, then HRA=10% of Basic salary and DA=90% of Basic salary. If the salary is either equal to or above Rs. 1500, then HRA= Rs. 500 & DA= 98% of Basic salary. If the employees salary is input through the keyboard. WAP to find his gross salary. 12) WAP to evaluate the Power series :

13) WAP for checking for a float input using if_else . 14) WAP to calculate energy bill. Read the starting and ending meter reading. If the consumed electricity energy is greater than or equal to 200 units the rate should be 2.50/unit otherwise 1.50/unit .

3. Nested if_else Statements We can write an entire if_else construct within either the body of the if statement or the body of an else statement . Syntax : if ( Test condition-1 ) { if ( Test condition-2 ) { statement-1; } else { statement-2; } } else { statement-3; } statement-X; Flowchart :

Demonstration : 1) The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules : % above or equal to 60 - First Division % between 50 & 59 - Second Division % between 40 and 49 - Third Division % less than 40 Fail WAP to calculate the division obtained by the student. 2) A commercial bank has introduced an incentive policy of giving bonus to all its deposit holders. The policy is as follows: A bonus of 2% of the balance held on 31ST December is given to everyone, irrespective of their balance, and 5% is given to female account holders if their balance is more than Rs.5000 . 3) WAP to find out the largest number out of the three numbers. Read the number through the keyboard. 4) WAP to find out the smallest number out of the three numbers. Read the number through the keyboard. 5) WAP to calculate gross salary for the conditions given below : Basic Salary (Rs) DA (Rs) HRA (Rs) Conveyance (Rs) >=5000 110% of basic 20% of basic 500 bs=>3000 && bs<5000 100% of basic 15% of basic 400 bs<=3000 90% of basic 10% of basic 300 6) Calculate the total interest based on the following : Principle (Rs) Amount Rate of Interest >=10000 20% >=8000 && <=9999 18% <8000 16% 7) WAP to find the average of six subjects and display the results as following : Average Result >34 & <50 Third Division >49 & <60 Second Division >60 & <75 First Division >75 & <100 Distinction If marks of any subject less than 35 Fail 8)

4. The else_if Ladder Demonstration : 1)

You might also like