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.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
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
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
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.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Introduction to C Programming CE Lecture 3 Control Structures in C.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Control statements Mostafa Abdallah
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
COMP Loop Statements Yi Hong May 21, 2015.
CPS120: Introduction to Computer Science Decision Making in Programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Control Statements: Part1  if, if…else, switch 1.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 5.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
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.
CNG 140 C Programming (Lecture set 3)
Decisions Chapter 4.
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
JavaScript: Control Statements I
JavaScript: Control Statements I
Structured Program
3 Control Statements:.
Decision making If statement.
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Presentation transcript:

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

Overview (Midterm) Midterm Examination 30 % Data Type, Declaration, Display Interactive Input SelectionRepetition Analyze Problem Solving Problem Solving int float double char printf() int float double char printf() scanf() if…else switch…case if…else switch…case while statement for statement do-while statement while statement for statement do-while statement

IF-ELSE StatementsIF-ELSE Statements 1 Switch StatementSwitch Statement 2 Today’s Overview 4

Introduction Flow of control refers to the order in which a program’s statements are executed Any algorithm can be built using combinations of four standardized flow of control structures: – Normal flow of control for all programs is sequential – Selection is used to select which statements are performed next based on a condition – Repetition is used to repeat a set of statements – Invocation is used to invoke a sequence of instructions using a single statement, as in calling a function

Relational Expressions Simplest decision structure: if (condition) statement executed if condition is true – The condition is evaluated to determine its numerical value, which is interpreted as either true (non-zero) or false (0) – If condition is “true” the statement following the if is executed; otherwise, statement is not executed The condition used in all of C’s if statements can be any valid C expression – Most commonly, a relational expression (can yield only 0 or 1)

Relational Expressions (cont.) Relational expressions are also known as conditions A relational expression evaluates to 1 (true) or 0 (false) – The expression 3 < 4 has a value of 1 – The expression 2.0 > 3.3 has a value of 0 – The value of hours > 0 depends on the value of hours Character data can also be compared using relational operators

Relational Expressions (cont.)

Relational expressions are evaluated to yield a numerical result – 0 interpreted as false – Nonzero interpreted as true Example printf(“The value of 3 < 4 is %d”, 3 < 4); printf(“\nThe value of 2.0 > 3.3 is %d”, 2.0 > 3.3); The value of 3 < 4 is 1 The value of 2.0 > 3.3 is 0

Logical Operators Can be called “Boolean Algebra” More complex conditions can be created using the logical operations AND (&&), OR (||), and NOT (!) Expression one (Logical Operators) Expression two

Logical Operators (cont.)

int i = 15, j = 30; double a = 12.0, b = 2.0, complete = 0.0;

Logical Operators (cont.)

char key = 'm'; int i = 5, j = 7, k = 12; double x = 22.5;

The if Statement The structure of an if statement is as follows: if ( statement is TRUE ) Execute this line of code Here is a simple example that shows the syntax: if ( 5 < 10 ) printf( "Five is now less than ten, that's a big surprise" );

The if-else Statement Sometimes when the condition in an if statement evaluates to false, it would be nice to execute some code The "else" statement effectively says that whatever code after it (whether a single line or code between brackets) is executed if the if statement is FALSE. 20

The if-else Statement general form if (expression) statement1; else statement2; – If value of expression is nonzero, statement1 is executed – If value of expression is zero, statement2 is executed

The if-else Statement (cont.)

The if-else Statement - Examples Problems: Print the result of ‘pass’ or ‘fail’ base on the student score (score >= 50 then pass) int score; scanf("%d",&score); if (score >= 50) printf("You have passed the exam >.<"); else printf("You have failed the exam T_T"); int score; scanf("%d",&score); if (score >= 50) printf("You have passed the exam >.<"); else printf("You have failed the exam T_T");

Compound Statements To have more than one statement execute after an if statement that evaluates to true, use braces { }

Compound Statements (cont.) Problem: Check if the input type is circle then calculate the area = pi * r * r if (type == ‘c’) { area = pi * r * r; // Statement1 printf(“The area of circle is: %d”, area); //Statement2 }

Problems Associated with the if-else Statement Misunderstanding the full implications of what an expression is Using the assignment operator (=), in place of the relational operator (==) Example age = 18; printf(“\nThe value of the first expression is %d”, age + 5); printf(“\nThe value of the second expression is %d”, age = 30); printf(“\nThe value of the third expression is %d”, age == 40); The value of the first expression is 23 (arithmetic operator) The value of the second expression is 30 (assignment operator) The value of the third expression is 0 (relational operator)

Nested if Statements One or more if-else statements can be included within either part of if-else statement Example if (condition1) { if (condition2) printf(“snap”); } else printf(“pop”);

The if-else Chain (Else-IF) General form if (expression1) statement1; else if (expression2) statement2; else if (expression3) statement3;. else if (expressionn) statementn; else lastStatement; Another use of else is when there are multiple conditional statements that may all evaluate to true,

Example if-else chain: display a person’s marital status corresponding to a letter input

Example

The switch Statement Terminated with a colon default is optional If the break statement was omitted, the following case would be executed

The switch Statement (cont.)

Summary Relational Expression Logical Operators if…else statement switch…case statement

? || // 36