Introduction to Programming Lecture 5. In the Previous Lecture Basic structure of C program Basic structure of C program Variables and Data types Variables.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Nested if-else Statements.  Should be indented to make the logic clear.  Nested statement executed only when the branch it is in is executed. For example,
Revision.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Topic 8 :PROGRAMMING 8.4 Use of control structure Learning Outcome : Write a program segment by using appropriate control structure.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
1 CSC103: Introduction to Computer and Programming Lecture No 8.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
If Statements & Relational Operators, Part 2 Programming.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
CPS120: Introduction to Computer Science Decision Making in Programs.
Flowcharts! January 13, 2005 These are today’s notes! Do you think we will get more snow?
Programming Fundamentals Lecture 5. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Chapter 02 (Part III) Introduction to C++ Programming.
Simple Control Structures IF, IF-ELSE statements in C.
1 CSC103: Introduction to Computer and Programming Lecture No 7.
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Agenda Basic Logic Purpose if statement if / else statement
Computing with C# and the.NET Framework Chapter 3 Software Engineering with Control Structures.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Unary Not operator !  !true = false  !false = true.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Decision Making and Branching
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Decision making If.. else statement.
The Ohio State University
The if…else Selection Statement
More on the Selection Structure
REPETITION CONTROL STRUCTURE
Chapter 4: Making Decisions.
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
C# and the .NET Framework
Programming Fundamentals
Control Structure Senior Lecturer
Lecture 2: Logical Problems with Choices
Intro to Programming Week # 3 If-else Statement Lecture # 6
Introduction to Programming
Introduction to Programming
Conditional Construct
Flow Control Statements
Visual Basic – Decision Statements
Summary Two basic concepts: variables and assignments Basic types:
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Selection Control Structure
Structural Program Development: If, If-Else
Programming Fundamental-1
Presentation transcript:

Introduction to Programming Lecture 5

In the Previous Lecture Basic structure of C program Basic structure of C program Variables and Data types Variables and Data types Operators Operators ‘cout’ and ‘cin’ for output and input ‘cout’ and ‘cin’ for output and input Braces Braces

Decision

If Statement If condition is true statements If Ali’s height is greater then 6 feet Then Ali can become a member of the Basket Ball team

If Statement in C If (condition) statement ;

If Statement in C If ( condition ) { statement1 ; statement2 ; :}

If statement in C if (age1 > age2) cout<<“Student 1 is older than student 2” ;

Relational Operators < less than <= less than or equal to == equal to >= greater than or equal to > greater than != not equal to

Relational Operators a != b; X = 0; X == 0;

Example #include #include main ( ) { int AmirAge, AmaraAge; AmirAge = 0; AmaraAge = 0; cout<<“Please enter Amir’s age”; cin >> AmirAge; cout<<“Please enter Amara’s age”; cin >> AmaraAge; if AmirAge > AmaraAge) { cout << “\n”<< “Amir’s age is greater then Amara’s age” ; }}

Flow Chart Symbols Start or stop Process Continuation mark Decision Flow line

Flow Chart for if statement Condition Process IF Then Entry point for IF block Exit point for IF block Note indentation from left to right

Example If the student age is greater than 18 or his height is greater than five feet then put him on the foot balll team Else Put him on the chess team

Logical Operators AND&& OR||

Logical Operators If a is greater than b AND c is greater than d In C if(a > b && c> d) if(age > 18 || height > 5)

if-else if (condition) { statement ; --}else{ --}

if-else Condition Process 1 IF Then Entry point for IF-Else block Exit point for IF block Process 2 Else Note indentation from left to right

Code if (AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ; } if (AmirAge < AmaraAge) { cout<< “Amir is younger than Amara” ; } Example

Example Code if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ; cout<< “Amir is older than Amara” ;}else{ cout<<“Amir is younger than Amara” ; cout<<“Amir is younger than Amara” ;}

Make a small flow chart of this program and see the one to one correspondence of the chart and the code

Example Code if AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ; cout<< “Amir is older than Amara” ;}else{ cout<<“Amir is younger than or of the same age as Amara” ; cout<<“Amir is younger than or of the same age as Amara” ;}

Example If (AmirAge != AmaraAge) cout << “Amir and Amara’s Ages are not equal”; cout << “Amir and Amara’s Ages are not equal”;

Unary Not operator !  !true = false  !false = true

If (!(AmirAge > AmaraAge)) ?

Example if ((interMarks > 45) && (testMarks >= passMarks)) { cout << “ Welcome to Virtual University”; }

Example If(!((interMarks > 45) && (testMarks >= passMarks))) ?

Nested if If (age > 18) { If(height > 5) {:}} Make a flowchart of this nested if structure…

In Today’s Lecture Decision Decision – If – Else Flowcharts Flowcharts Nested if Nested if