DKT121: Fundamental of Computer Programming

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Week 3 – Selection Structures UniMAP SemPGT C PROGRAMMING1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Selection Structures (if & switch statements) (CS1123)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Week 4 Program Control Structure
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Branching statements.
Chapter 4: Control Structures I
The if…else Selection Statement
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Selections Java.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Week 3 C Program Structures (Selection Structures)
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
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.
Week 3 – Selection Structures
Chapter 2.1 Control Structures (Selection)
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Control Structures.
Chapter 4: Control Structures I
Lecture 2: Logical Problems with Choices
Chapter 4: Control Structures I (Selection)
Chapter 4 Selection.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Week 3 – Program Control Structure
Structured Program Development in C++
Branching statements Kingdom of Saudi Arabia
Control Structure.
Structural Program Development: If, If-Else
Presentation transcript:

DKT121: Fundamental of Computer Programming Selection UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement Nested if Conditional operator Switch structure UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

RECALL.. Selection structure Used to choose among alternative courses of action C has three types: if, if..else, and switch UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

The if selection structure if structure is a single-entry/single-exit structure If student’s grade is greater than or equal to 60 Print “Passed” true false grade >= 60 print “Passed”   UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

The if..else selection structure Specifies an action to be performed both when the condition is true and when it is false If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed” true false print “Failed” print “Passed” grade >= 60 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Selection Stmts Used to control the flow of a program Also called as decision or branches Branches are conditions or choices used to enable selection of program flow UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Types of selection One-way selection = if Two-way selection = if..else Multi-selection Nested if Switch structure = switch UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming One-way Selection = if In C, a condition is represented by a logical (Boolean) expression true and false are logical (Boolean) values The syntax of one-way selection is: if (expression) statement; If the value of the expression is true, statement is executed; if false, statement is not executed and the computer goes on to the next statement in the program. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming One-way Selection = if If student’s grade is greater than or equal to 60 Print “Passed” true false grade >= 60 print “Passed” UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming One-way Selection = if ….. if(grade >= 60) printf(“Passed\n”); UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming One-way Selection = if Another example: char grade; …… if(markah>= 90) grade = 'A'; …... printf(“Grade is : %c\n”, grade); UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming One-way Selection = if Another example: if (temperature is greater than 70 degree and it is not raining) recommended activity is golfing bool rain=false; … if((temp > 70) && !(rain)) printf(“recommended activity is golfing”); UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming One-way Selection = if Common Errors if score >= 90 //no parentheses grade = 'A'; if(score >= 90); //; not here UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Two-way Selection = if..else The syntax of two-way selection is: if (expression) statement1; else statement2; If the value of the expression is true, statement1 is executed; if false, statement2 is executed UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Two-way Selection = if..else If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed” true false print “Failed” print “Passed” grade >= 60 UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Two-way Selection = if..else ……… if(grade >=60) printf(“Passed\n”); else printf(“Failed\n”); …… UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Two-way Selection = if..else Another example: if (hours > 40.0) //Line 1 wages = 40.0 * rate +1.5 * rate * (hours - 40.0); //Line 2 else //Line 3 wages = hours * rate; //Line 4 If hours is 50, then the statement at Line 2 executes If hours is 30, then the statement at Line 4 executes UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Multi-selection = if-else if The syntax is: if(exp1) stmt1; else if(exp2) stmt2; else if(exp3) stmt3; … else stmt n; An if-else if control structure shifts program control, step by step,through a series of statement blocks. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Multi-selection = if-else if E.g. temp >30 true Print “hot” temp display >30 0c hot 20-30 0c mild 10-20 0c cold <10 0c very cold false true temp > 20 Print “mild” false temp >10 true Print “cold” false Print “very cold” UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Multi-selection = if-else if if(temp > 30) printf( “hot\n”); else if(temp >=20) printf( “mild\n”); else if(temp >=10) printf(“cold\n”); else printf( “very cold\n”); UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Compound (Block of) Stmt A compound statement (also called a block of statements) takes the form { statement 1; statement 2; . statement n; } It is considered a single statement UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

Compound (Block of) Stmt Example: if (age > 18) { printf("Eligible to vote.\n“); printf("No longer a minor.\n“); } else printf("Not eligible to vote.\n“); printf(“Still a minor\n”); UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Nested if When one control statement is within another, it is said to be nested if(exp1) if(exp2) stmt1; OR { stmt1; stmt2; } UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Nested if Eg. if (temperature >= 50) { if (temperature >= 80) printf( "Good day for swimming.\n”); else printf( "Good day for golfing.\n“); } printf("Good day to play tennis.\n“); UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Nested if Another example UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

The Conditional Operator (? :) The syntax of using the conditional operator is: expression1 ? expression2 : expression3; This is called a conditional expression. The statement: if (a >= b) max = a; else max = b; Is equivalent to the statement: max = (a >= b) ? a : b; UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming switch Structures Similar to if-else if control structure The general form (syntax): switch (expression) { case value1: statements1; break; case value2: statements2; break; . case valuen: statementsn; break; default: statements; } UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming switch Structures The break statement has a special meaning and may or may not appear after each statement. In C, switch, case, break, and default are reserved words. In a switch structure, first the expression is evaluated. The value of the expression is then used to perform the corresponding action. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming Switch Structures The expression is usually an identifier. The value of the expression can be only integral. The expression is sometimes called the selector. Its value determines which statement is selected for execution. A particular case value should appear only once. One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement. The break statement may or may not appear after each statement. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming switch Structures Example: switch (grade) { case 'A': printf("The grade is A.“); case 'B': printf("The grade is B.“); break; case 'C': printf("The grade is C.“); break; case 'D': printf("The grade is D.“); break; case 'F': printf("The grade is F.“); break; default: printf("The grade is invalid.“); } where, grade is a variable of the type char. If the value of grade is, say 'A', the output is The grade is A. UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming switch Structures The switch statement executes according to the following rules: When the value of the expression is matched against a case value (also called a label), the statements execute until either a break statement is found or the end of the switch structure is reached. If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped. A break statement causes an immediate exit from the switch structure UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming What’s wrong?? UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming

DKT121: Fundamental of Computer Programming End Selection Q & A! UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming