1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Logic & program control part 3: Compound selection structures.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection)
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Control Structures I (Selection)
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Advanced Programming LOOP.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
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.
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 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Basic Of Computer Science
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
C++ Programming: Basic Elements of C++.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
1 Chap 4. Data Should know Declare & Initialize variables Declare constants Assignment Operators Increment and Decrement Operators Precedence of Operators.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 Programming Application Overview of Structure Programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 4 – Control Structures Part 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Conditions, Logical Expressions, and Selection Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Control Statements: Part1  if, if…else, switch 1.
1 Advanced Programming Windows Applications. Create a new Project Select a C# Windows Application.
C++ Programming Control Structures I (Selection).
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I
The if…else Selection Statement
Chapter 4: Control Structures I
Chapter 4 – Control Structures Part 1
Chapter 4: Control Structures I
Chapter 4: Control Structures I (Selection)
SELECTION STATEMENTS (2)
Structured Program
3 Control Statements:.
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Control Statements.
Presentation transcript:

1 Advanced Programming IF

2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different statement other than the next one executes Selection structure –The if and if/else statements –The goto statement No longer used unless absolutely needed Causes many readability problems Repetition structure –The while and do/while loops –The for and foreach loops

Control Statements If, Else While For Foreach

4 using System; class Comparison { static void Main( string[] args ) { int number1, number2; number1 = Int32.Parse( Console.ReadLine() ); number2 = Int32.Parse( Console.ReadLine() ); if ( number1 == number2 ) Console.WriteLine( number1 + " == " + number2 ); if ( number1 > number2 ) Console.WriteLine( number1 + " > " + number2 );

Operators Precedence CategoryOperators Additive Add: + Subtract: - Shift Shift bits left: << Shift bits right: >> Relational Less than: < Greater than: > Less than or equal to: <= Greater than or equal to: >= Type equality/compatibility: is Type conversion: as

6 Decision Making: Equality and Relational Operators

Operators Precedence CategoryOperators Equality Equals: == Not equals: != Bitwise AND& Bitwise XOR^ Bitwise OR| Logical AND&& Logical OR||

8 Example Your city classifies a pollution index –less than 35 as “Pleasant”, –35 through 60 as “Unpleasant”, – and above 60 as “Health Hazard.” –Display the correct description of the –pollution index value.

9 Assignment Operators Assignment operators –Can reduce code x += 2 is the same as x = x + 2 –Can be done with all the math operators ++, -=, *=, /=, and %=

10 Assignment Operators

11 Increment and Decrement Operators Increment operator –Used to add one to the variable –x++ –Same as x = x + 1 Decrement operator –Used to subtract 1 from the variable –y-- Pre-increment vs. post-increment –x++ or x-- Will perform an action and then add to or subtract one from the value –++x or --x Will add to or subtract one from the value and then perform an action

12 Conditional Operator (?:) –Similar to an if/else structure –The syntax is: (boolean value ? if true : if false) Console.WriteLine ( studentGrade >=60? “Passed” : “Failed”);

13 Example Display one word to describe the integer value of number as “Positive”, “Negative”, or “Zero”

14 Example Write a program to ask a student for his grades in 3 exams ( each out of 50 ), get their total and inform the student whether he passed or failed the course.

15 Example Compute the Grade of students as A, B, C, D, and F in a class depending on their scores

16 Write a program that calculates bills for customer of the Electricity company. There are 3 types of customers: residential (code R), commercial (code C), and Industrial (code I). - For a code R customer, the bill is $10 plus $0.05 for each kilowatt used. - For a code C customer, the bill is $1000 for the first 2000 kilowatt, and $0.005 for each additional kilowatt used. - For a code I customer, the bill is $1000 if he used less than 4000 kilowatt, $2000 if he used between 4000 and kilowatt, or $3000 if he used more than kilowatt. The inputs of the program should be the type of customer ( R, C or I) and the kilowatts used. The output should be the amount of money the customer has to pay.

Predefined Types char Escape sequence characters (partial list) CharMeaningValue \’ Single quote0x0027 \” Double quote0x0022 \\ Backslash0x005C \0 Null0x0000 \n New line0x000A \r Carriage return0x000D \t Tab0x0009

18 If--Else for a mail order Write a program to calculate the total price of a certain purchase. There is a discount and shipping cost:  The discount rate is 25% and the shipping is if purchase is over  Otherwise, The discount rate is 15% and the shipping is 5.00 pounds.

19 Example The Air Force has asked you to write a program to label aircrafts as military or civilian. Your program input is the plane’s speed and its estimated length. For planes traveling faster than 1100 km/hr, you will label those shorter than 52 m “military”, and longer as “Civilian”. For planes traveling less than 1100, you will issue an “aircraft unknown” statement.

Conditional Operator (?:) Conditional operator ( ?: ) takes three arguments –Ternary operator Syntax for using the conditional operator: expression1 ? expression2 : expression3 If expression1 is true, the result of the conditional expression is expression2 –Otherwise, the result is expression3

switch Structures switch structure: alternate to if-else switch (integral) expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector

switch Statement Can branch on any predefined type (including string ) or enum –User-defined types can provide implicit conversion to these types Must explicitly state how to end case –With break, goto case, goto label, return, throw or continue –Eliminates fall-through bugs –Not needed if no code supplied after the label

Statements switch Statement int Test(string label) { int result; switch(label) { case null: goto case “runner-up”; case “fastest”: case “winner”: result = 1; break; case “runner-up”: result = 2; break; default: result = 0; } return result; }

Light bulbs Write a program to ask the user for the brightness of a light bulb (in Watts), and print out the expected lifetime: BrightnessLifetime in hours , , otherwise0

Program Write a C# program to calculate the average of three test grades and print out a report with the student’s ID number, average, and how well is the student progress. “Very Good” is a 70- point average or better, “Good” is an average between 60 and 70, and “Failing” is 50 point average or less.

Write a program that reports the content of a compressed-air cylinder based upon the first letter of the cylinder’s color. The program input is a character representing the observed color of the cylinder: ‘Y’ or ‘y’ for yellow, ‘G’ or ‘g’ for green and so on. Given: ColorContent OrangeAmmonia BrownCarbon Monoxide YellowHydrogen GreenOxygen