1 CSC103: Introduction to Computer and Programming Lecture No 8.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Logic & program control part 3: Compound selection structures.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Selection in C.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Computer Science Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Computer Science Department Relational Operators And Decisions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
Chapter 5: Making Decisions
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Agenda Basic Logic Purpose if statement if / else statement
Week 4 Program Control Structure
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.
Chapter 2 Excel Fundamentals Logical IF (Decision) Statements Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Decision Making and Branching
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Control Statements: Part1  if, if…else, switch 1.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Fundamental Programming Fundamental Programming Data Processing and Expressions.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Random Functions Selection Structure Comparison Operators Logical Operator
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.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Decision making If.. else statement.
‘C’ Programming Khalid Jamal.
The if…else Selection Statement
More on the Selection Structure
- Standard C Statements
Decisions Chapter 4.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Control Structures: Selection Statement
Decision making If statement.
Visual Basic – Decision Statements
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Control Structures: Selection Statement
Dale Roberts, Lecturer IUPUI
Selection Control Structure
Lecture 9: Implementing Complex Logic
Programming Fundamental
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 8

2 Previous Lecture Example programs – printf, scanf Decision control structure Relational operator in C if statement if-else statement

3 Today’s lecture outline Nested if else statement Logical operator in C Conditional operator

4 Nested if else It is possible to write an entire if-else with either the if statement block or else statement block

5 Nested if else flowchart Start Display “Enter either 1 or 2” Read x Display “you entered other than 1 or 2” Go to program x == 1 Yes No x == 2 NoYes Display “you entered 1” End Display “you entered 2”

6 Forms of if statement

7 Cont.

8 Logical Operators && (logical AND ) – true if both conditions are true if ( gender == 1 && age >= 65 ) senior++; || (logical OR ) – true if either of condition is true if (semesterAvg >= 90 || finalExam >=90 ) printf("Student grade is A“);

9 Cont. ! (logical NOT, logical negation) – Returns true when its condition is false, & vice versa if ( !( grade == 20 ) ) printf(“hello world“); Alternative: if ( grade != 20 ) printf(“hello world“); False!  if (True) True !  if (False)

10 Logical Operators..

11 Sample Program -1 To calculate the division Input: marks of 5 different subjects Rules – Percentage above or equal to 60 - First division – Percentage between 50 and 59 - Second division – Percentage between 40 and 49 - Third division – Percentage less than 40 – Fail Solution – Nested if-else – Logical operators Go to Program

12 Sample Program -2 A company insures its drivers in the following cases: – If the driver is married – If the driver is unmarried, male & above 30 years of age – If the driver is unmarried, female & above 25 years of age If the marital status, gender and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not.

13 Flowchart Go to Program without logical operator Go to Program with logical operator (ms is married) OR (ms is unmarried and g is male and age is > 30) OR (ms is unmarried and g is female and age is > 25)

14 && and || && and || are useful in the following programming situations – When after testing several conditions the outcome is only one of the two answers – When it is to be tested whether a value falls within a particular range or not Write a Program

15 Example program 3 Write a program to calculate the salary as per the following table:

16 Revised Hierarchy

17 Caution Between assignment /equality operator if ( i = 5 ) printf ( "You entered 5" ) ; else printf ( "You entered something other than 5" ); Null statement if ( i == 5 ) ; printf ( "You entered 5" )

18 Conditional Operators General form is, ( expression 1 ? expression 2 : expression 3); Conditional operators ? and : are sometimes called ternary operators if expression 1 is true, then the value returned will be expression 2, otherwise the value returned will be expression 3

19 Examples A

20 Cont. B C D

21 Cont.. E The limitation of the conditional operators is that after the ? or after the : only one C statement can occur.

22 Example main( ) { int x, y; printf ("Enter the salary" ) ; scanf ( "%d", &x ) ; if ( x > 10 ) { printf ( “%d“, x ) ; y = x+10; } else y = x; } (expression 1 ? expression 2 : expression 3); condition Single statement

23 Example Program Using conditional operators determine: Whether the character entered through the keyboard is a Upper case alphabet or not. Write a Program

24 Nested conditional operator (expression 1 ? expression 2 : expression 3); Single condition or compound condition (expression 1 ? expression 2 : expression 3);

25 Example program main( ) { float sal ; printf ("Enter the salary" ) ; scanf ( "%f", &sal ) ; if ( sal ) printf ( "Manager" ) ; else if ( sal ) printf ( "Accountant" ) ; else printf ( "Clerk" ) ; }

26 Cont. main( ) { float sal ; printf ("Enter the salary" ) ; scanf ( "%f", &sal ) ; ( (sal 25000) ? printf ( "Manager" ) : ( ( sal ) ? printf ( "Accountant") : printf ( "Clerk" ) ) ) ; }