Chapter - 2.1.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
CEUC101: COMPUTER CONCEPTS & PROGRAMMING Chapter – 2.1 Branch & Loop statements Devang Patel Institute of Advance Technology and Research Objectives To get understanding of decision making in ‘C’ Language. To develop programming skills using different types of i...
CEUC101: COMPUTER CONCEPTS & PROGRAMMING Chapter – 2.1 Branch & Loop statements Devang Patel Institute of Advance Technology and Research Objectives To get understanding of decision making in ‘C’ Language. To develop programming skills using different types of if Conditions. To impart the knowledge of switch statement in C. To understand the use of if…else instead of conditional operator. Chapter – 8 : Conditional Statements & Branching Chapter – 8 : Conditional Statements & Branching Decision making in C “Decision making and branching” is one of the most important concepts of computer programming. Programs should be able to make logical (true/false) decisions based on the condition provided. Every program has one or few problems to solve. In order to solve those particular problems important decisions have to be made depending on the nature of the problems. So controlling the execution of statements based on certain condition or decision is called decision making and branching. Chapter – 8 : Conditional Statements & Branching Decision making in C Chapter – 8 : Conditional Statements & Branching Holiday trip Problem Consider the fact that you and some of your friends have planed to go out for a holiday trip after the Semester. You have also decided that if you have got received money 10,000 Rupees or more from your parent then you will go out for a foreign trip. Otherwise, if the allotted money is less than 10,000 then you will go out for a country side trip. Now you are supposed to design a program to solve this problem. Chapter – 8 : Conditional Statements & Branching Chapter – 8 : Conditional Statements & Branching Decision making in C C language possesses decision making and branching capabilities by supporting the following statements: If statement Switch statement Conditional operator statement goto statement These statements are knows as decision making statements. They are also called control statements as the control the flow of execution. Chapter – 8 : Conditional Statements & Branching Decision making using if The if statement is a powerful statement for decision making and is used to control the flow of execution of statements. It is basically a two-way decision making statement and is used in conjunction with an expression. It takes the following structure: if (test-condition) It allows the computer to evaluate the expression first and then depending on whether the value of the expression or condition is true or false, it transfer the control to a particular statement. This point of program has two paths to follow, one for the true condition and the other for the false condition. Chapter – 8 : Conditional Statements & Branching Decision making using if Chapter – 8 : Conditional Statements & Branching Decision making using if The if statement can be implemented if four different forms depending on the complexity of the conditions to be tested. The four forms are: Simple if statement If else statement Nested if else statement Else if ladder Chapter – 8 : Conditional Statements & Branching Simple if statement The general form of a simple if statement is: if (test_condition) { statement-block; } statement x; Chapter – 8 : Conditional Statements & Branching Simple if statement The general form of a simple if statement is: The statement block may consists of a single statement or a group of statements. If the test_condition is satisfied then the statement block will be executed first then the statement-x will be executed after completing the execution of statement block. Otherwise if the test_condition doesn’t satisfied then, the statement block will be skipped and the execution will jump to the statement-x. So in short when the condition is true then both the statement block and the statement-x are executed but in sequence. Chapter – 8 : Conditional Statements & Branching Simple if statement - Flowchart Chapter – 8 : Conditional Statements & Branching Simple if statement - Example #include void main() { int number = 0; printf("\nEnter an integer between 1 and 10: "); scanf("%d",&number); if (number > 7) printf("You entered %d which is greater than 7\n", number); if (number < 3) printf("You entered %d which is less than 3\n", number); } Chapter – 8 : Conditional Statements & Branching if…else statement If…else is an extension of the simple if statement. If the test condition is true then the true block statements, immediately following the if statements are executed. Otherwise the false block statements are executed. In short either true-block or false-block of statements will be executed, not both. But in both cases the control is transferred subsequently to the statement-x as it is an independent (not controlled by the if else statement) statement. It is also called two way conditional branching. Chapter – 8 : Conditional Statements & Branching if…else statement - Structure The if else statement is an extension of the simple if statement. The general form is : if (test_condition) { True block statements; } else { False block statements; } statement-x; Chapter – 8 : Conditional Statements & Branching if…else statement- Flowchart Chapter – 8 : Conditional Statements & Branching if…else statement- Example Chapter – 8 : Conditional Statements & Branching Nesting of if…else If the series of decisions are involved, we may have to use more than one if…else statement in nested form. Using “if…else statement” within another “if…else statement” is called ‘nested if statement’. “Nested if statements” is mainly used to test multiple conditions. It is also called nested conditional branching. Chapter – 8 : Conditional Statements & Branching Nesting of if…else - Structure Chapter – 8 : Conditional Statements & Branching Nesting of if…else – Execution Flow Chapter – 8 : Conditional Statements & Branching Nesting of if…else - Flowchart Chapter – 8 : Conditional Statements & Branching Nesting of if…else - Example if (gender==female) { if (age=50 && mark=60 && mark=70 && mark=80 && mark