THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
C++ for Engineers and Scientists Third Edition
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Computer Science Department Relational Operators And Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Flow of Control Part 1: Selection
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.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Decision making If.. else statement.
Control Structures I Chapter 3
Rules for Constructing Variable Names
Java Programming Fifth Edition
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 2 - Introduction to C Programming
Selections Java.
Decisions Chapter 4.
EGR 2261 Unit 4 Control Structures I: Selection
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Boolean Expressions and If
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 2 - Introduction to C Programming
Control Structures.
Chapter 2 - Introduction to C Programming
Conditions and Boolean Expressions
Decision making If statement.
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
ICS103: Programming in C 4: Selection Structures
Switch Case Structures
Presentation transcript:

THE DECISIONS CONTROL STRUCTURE! CHAPTER 2

Transfer of control statement: o The statement of computer program are executed one after the other in the order in which they are written and this is known as sequential flow or default flow. o The order of execution of statements in a program can be changed. o This is done by the help of transfer of control statement. o The control structure are statements that are used to control the flow of execution in a program. 2

Decision Control Instruction(ToCS) The decision control instruction can be implemented in C using: The if statement The if-else statement The conditional operators

The if Statement Transfer of control statement to another place. The “if statement” is used to execute(or ignore) a set of statements after testing a condition. The syntax of “if statement “is: If(condition) statement; 4

The if Statement. The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed, instead the program skips it.

What is the condition?? The condition is relational condition. What is relational condition??  A condition in which relational operators has been used is called relational condition. What is relational operators???

Relational operator THIS EXPRESSIONIS TRUE IF X==y X!=y X<y X>y X<=y X>=y X is equal to y X is not equal to y X is less than y X is greater than y X is less than or equal to y X is greater than or equal to y

Example The relational operators should be familiar to you except for the equality operator == and the inequality operator !=. Note that = is used for assignment, whereas, == is used for comparison of two quantities. Here is a simple program, which demonstrates the use of if and the relational operators. /* Demonstration of if statement */ main( ) { int num ; printf ( "Enter a number less than 10 " ) ; scanf ( "%d", &num ) ; if ( num <= 10 ) printf ( “you have entered the correct number !" ) ; }

Example On execution of this program, if you type a number less than or equal to 10, you get a message on the screen through printf( ). If you type some other number the program doesn’t do anything means that compiler will ignore the statements in the if block.

The if-else Statement The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false? Of course! This is what is the purpose of the if else statement.

The if-else Statement It is another form of decision control statements. In this statement one condition and two statements are given and is used for making two way decisions. The first statement is the “if statement” with a given condition. The second statement is the else statement.

Syntax of “if-else” statement; If(condition) statement-1; else statement-2; If the condition is true the statement-1 is executed. If the condition is false then the if statement-1 is ignored and the else statement(statement-2) is executed.

Example #include void main() { Char xyz; Scanf(“%c”,&xyz); if(xyz==‘a’) { Printf(“value of xyz=%c is matches of a”, xyz); } else { Printf(“you have entered invalid character”); } getch(); }

Example 2 If his basic salary is less than 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 basic salary. If the employee's salary is input through the keyboard write a program to find his gross salary.

Example 2

Nested if-elses It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement. This is called ‘nesting’ of if s. This is shown in the following program.

Program main( ) { int i ; printf ( "Enter either 1 or 2 " ) ; scanf ( "%d", &i ) ; if ( i == 1 ) printf ( "You would go to heaven !" ) ; else { if ( i == 2 ) printf ( "Hell was created with you in mind" ) ; else printf ( "How about mother earth !" ) ; }

Note: Note that the second if-else construct is nested in the first else statement. If the condition in the first if statement is false, then the condition in the second if statement is checked. If it is false as well, then the final else statement is executed.

Forms of if if ( condition ) do this ; if ( condition ) { do this ; and this ; } if ( condition ) do this ; else do this ; if ( condition ) { do this ;

FORMS and this ; } else { do this ; and this ; } (e) if ( condition ) do this ; else { if ( condition ) do this ; else { do this ; and this ; } this ;

(f) if ( condition ) { if ( condition ) do this ; else { do this ; and this ; } else do THIS

Logical Operators The logical operators are used to combine more the one relational conditions. The following are the logical operators 1.logical AND (&&) 2.logical OR(||) 3.NOT(!)

Hierarchy of operators revisited Since we have now added the logical operators to the list of operators we know, it is the time to review these operators and their priorities fig 2.7 A full – fledged precedence table of operators is given appendix.)

Operators and its types !Logical not */%Arithmetic and modulus + -Arithmetic =Relational = ! =Relational &&Logic and llLogic or =assignment

The Conditional Operators The conditional operators? and: are sometimes called ternary operators since they take three arguments. In fact, they form a kind of foreshortened if-then-false. Their general form is Expression 1? expression2: expression 3

Examples Int x, y; Scan f ( “%d”,&x); Y=(x>5?3:4); This statement will store 3 in y if x is greater then 5. other wise it will store 4 in y. The equivalent if statement will be, If (x>5) Y=3; Else Y=3; Else Y=4:

Summary There are three ways for taking decision in a program first way is to use the if-else statement second way is to use the conditional operators and third way is to use the switch statement the default scope of the if statement is only the next statement An if block need not always be associated with an else else block In c every test expression is evaluated in terms of zero and non-zero values a zero value will be considered to be false and anon-zero value will be considered true Assignment statements used within conditional operators must be enclosed within a pair of parenthesis

THE END