CMP 131 Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Selection If Flowchart A diagram that shows step-by-step progression through a procedure or system especially using connecting lines and a set of conventional.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Introduction to Computing Science and Programming I
1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem
Subject: Information Technology Grade: 10
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
Program Design and Development
Pseudocode and Algorithms
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
An Introduction to Textual Programming
WML II (“Son of WML”) WML WMLScript. WML - A Quick Review Document structure ,... Text and image controls ...,,..., Navigation controls ,,, Events.
Chapter 5: Control Structures II (Repetition)
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Computer Programming TCP1224 Chapter 3 Completing the Problem-Solving Process and Getting Started with C++
Pascal language Slides of Omar Al-Nahal. Components of Pascal Language Components of Pascal Language 1. Pascal Character set: - English Letters. - Decimal.
Summer Assignment Review
Lecture 26 Final review. Problem 1 (10 points) How many times would the FOR-DO loops beginning with the following statements be executed? If the loop.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Introduction to Pascal The Basics of Program writing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Structures CPS120: Introduction to Computer Science Lecture 5.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Programming, an introduction to Pascal
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Computer Programming Control Structure
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Introduction to Computing Dr. Nadeem A Khan. Lecture 2.
Counting Loops.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 6, Lecture 1 (Monday)
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
2 nd Semester Module3 Selection Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
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.
FOR LOOP STRUCTURE For := to do eg. for I := 1 to 100 do begin writeln(‘This is a loop’); end;
Chapter 4: Control Structures I
REPETITION CONTROL STRUCTURE
Chapter 4: Control Structures I
Decisions Chapter 4.
Chapter 4 MATLAB Programming
The Selection Structure
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Chapter 5 Repetition.
Chapter 4: Control Structures I
Selection CIS 40 – Introduction to Programming in Python
Chapter 9 Control Structures.
Introduction to Programming
An Introduction to Python
Faculty of Computer Science & Information System
Visual Basic – Decision Statements
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 7, Lecture (Wednesday)

TODAY IF-THEN and IF-THEN-ELSE statements Using compound statements with them IF-THEN-ELSE for multiple choice CASE statement for multiple choice

Yes/No Choice The logic: IF some condition or test is TRUE THEN perform some action [ ELSE do nothing ] Examples: If the car is running out of gas, put in gasoline. If you are failing your class and you cannot drop it, study more. If someone says hello and you don’t want to seem impolite, say hello back.

Yes/No Choice: The IF-THEN Statement Syntax: IF <condition> THEN <statement> Syntax graph: Flowchart: IF condition THEN statement statement 1 yes condition statement 2 no statement 3

{File: ABSVAL1.PAS} PROGRAM AbsoluteValue; VAR Number : real; BEGIN writeln('Type a number, then press enter: '); readln(Number); write('The absolute value of ', Number, ' is '); IF Number < 0 THEN Number := -Number; writeln(Number); readln; END.

Either/Or Choice The logic: IF some condition or test is TRUE THEN perform some action ELSE perform a different action Examples: If the weather is hot wear light clothes, else wear warm clothes. If a number is odd increment OddCount else increment EvenCount If a noun starts with a capital letter categorize it as a proper noun (name) else as a common noun.

Either/Or Choice : The IF-THEN-ELSE Statement Syntax: IF <condition> THEN <statement> ELSE <statement> Syntax graph: Flowchart: IF condition THEN statement ELSE statement 1 condition yes statement 4 statement 2 no statement 3

{File: ABSVAL2.PAS} PROGRAM AbsoluteValue; VAR Number : real; BEGIN writeln('Type a number, then press enter: '); readln(Number); write('The absolute value of ', Number, ' is '); IF Number < 0 THEN writeln(-Number) ELSE writeln(Number); readln; END.

IF-THEN(-ELSE) and Compound Statements Conditional statements: IF <condition> THEN <statement> IF <condition> THEN <statement> ELSE <statement> What is <statement>? A single statement: An input or output statement An assignment Another IF … THEN … ELSE statement (nested conditional statements) A looping statement (FOR, WHILE, REPEAT) … A compound statement: BEGIN <statement> ; <statement> ; …. <statement> END

{File: ORDER1.PAS} PROGRAM Order2Numbers; VAR Number1, Number2 : real; BEGIN writeln('Enter 2 numbers, then press ENTER: '); readln(Number1, Number2); IF Number1 < Number2 THEN writeln('The ordered numbers are:', Number1, Number2) ELSE writeln('The ordered numbers are:', Number2, Number1); readln END.

{File: ORDER2.PAS} PROGRAM Order2Numbers; VAR Number1, Number2, Temp : real; BEGIN writeln('Enter 2 numbers, then press ENTER: '); readln(Number1, Number2); IF Number2 < Number1 THEN Temp := Number2; Number2 := Number1; Number1 := Temp; END; writeln('The ordered numbers are:', Number1, Number2); readln END.

Ordering 3 numbers… What can happen? Foo =< Bar =< Nem Nem =< Foo =< Bar Foo =< Nem =< Bar Bar =< Foo =< Nem Nem =< Bar =< Foo Bar =< Nem =< Foo So you have to test all the possible input combinations!!!

{File: ORDER3.PAS} PROGRAM Order3Numbers; VAR Foo, Bar, Nem, Temp : real; BEGIN writeln('Enter 3 numbers, then press ENTER: '); readln(Foo, Bar, Nem); IF Foo < Bar THEN IF Bar < Nem THEN writeln('The ordered numbers are:', Foo, Bar, Nem) ELSE IF Nem < Foo THEN writeln('The ordered numbers are:', Nem, Foo, Bar) writeln('The ordered numbers are:', Foo, Nem, Bar) ELSE { Foo >= Bar } IF Foo < Nem THEN writeln('The ordered numbers are:', Bar, Foo, Nem) IF Nem < Bar THEN writeln('The ordered numbers are:', Nem, Bar, Foo) writeln('The ordered numbers are:', Bar, Nem, Foo); readln END.

Test Set Foo Bar Nem Foo < Bar < Nem 1 2 3 Nem < Foo < Bar Foo = Nem = Bar

If you want to add a statement {File: ORDER3.PAS} PROGRAM Order3Numbers; VAR Foo, Bar, Nem, Temp : real; BEGIN writeln('Enter 3 numbers, then press ENTER: '); readln(Foo, Bar, Nem); IF Foo < Bar THEN writeln(‘Foo < Bar’); IF Bar < Nem THEN writeln('The ordered numbers are:', Foo, Bar, Nem) ELSE IF Nem < Foo THEN writeln('The ordered numbers are:', Nem, Foo, Bar) writeln('The ordered numbers are:', Foo, Nem, Bar) END ELSE { Foo >= Bar } writeln(‘Foo >= Bar’); IF Foo < Nem THEN writeln('The ordered numbers are:', Bar, Foo, Nem) IF Nem < Bar THEN writeln('The ordered numbers are:', Nem, Bar, Foo) writeln('The ordered numbers are:', Bar, Nem, Foo); readln END. compound statement compound statement