Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

More on Algorithms and Problem Solving
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
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)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Structured Program Development in C
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures I (Selection)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
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 (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
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.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
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.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
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.
The if…else Selection Statement
- Standard C Statements
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.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Lecture 2: Logical Problems with Choices
Chapter 4: Control Structures I (Selection)
Structured Program
3 Control Statements:.
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Dale Roberts, Lecturer IUPUI
Structural Program Development: If, If-Else
Presentation transcript:

Programming Language C++ Lecture 3

Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.  Control structures are used to control the flow of execution in a program  All programs could be written in terms of only three control structures:  the sequence structure 2

3 - Programs executed sequentially by default  the selection structure - if, if/else, switch  the repetition structure - while, do/while, for

Control Structures  C++ provides three types of selection statements  The if selection statement is a single-selection statement because it selects or ignores a single action (or, as we’ll soon see, a single group of actions).  The if…else statement is called a double-selection statement because it selects between two different actions (or groups of actions).  The switch and (nested if) selection statement is called a multiple- selection statement because it selects among many different actions (or groups of actions). 4

Control Structures  C++ provides three types of repetition statements ( Lecture 4) 5

Conditional Statements (if and if…else) 6

if Selection Structure Choose among alternative courses of action example: -If student’s grade is greater than or equal to 40 Print “Passed” – If the condition is true Print statement executed, program continues to next statement – If the condition is false Print statement ignored, program continues – Indenting makes programs easier to read C++ ignores whitespace characters (tabs, spaces, etc.) 7

The syntax for the if statement is as follows: if (condition) { statement(s); } => If the Condition is true, then the compiler would execute the Statement. 8

Conditions: Conditions are expressions that evaluate to a boolean value — a true or false value (true and false are C++ keywords, representing the two possible values of a Boolean expression or variable). Simple conditions involve two operands, each of which can be a variable or a literal value, and an operator, typically a comparison operator. 9

== : true if and only if left operand is equal to right operand != : true if and only if left operand is not equal to right operand > : true if and only if left operand is greater than right operand < : true if and only if left operand is less than right operand >= : true if and only if left operand is greater than or equal than right operand <= : true if and only if left operand is less than or equal than right operand 10 The comparison operators are shown below:

C++ codes: If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed"; 11

Examples: 1)if ( a < 0 ) { cout<< “a is negative”; } 2)if ( a > 0 ) { cout<<“a is positive”; } 3)if ( a == 0 ) { cout<< "a is null”; } 4) if( a < b ) { // if condition is true then print the following cout << "a is less than b" << endl; } 12

Important: The if statement must NOT have a semicolon at the end of the line (i.e., after the closing bracket for the condition) This is one of the common mistakes that can give you a really hard time before identifying it. => The compiler sees that there is a statement after the closing bracket for the condition. if (number < 0) ; { cout << "Negative number" << endl; } 13

if/else Selection Structure The simple if statement covers the cases where we have a fragment of code that should be executed depending on a condition. If we have a situation where we have two possible actions, and we want to do one or the other, depending on a given condition, we use the if – else statement. 14

if/else Selection Structure if – Performs action if condition true if/else – Different actions if conditions true or false example if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code 15 if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

if/else Selection Structure Ternary conditional operator (?:) – Three arguments (condition, value if true, value if false) Code could be written: cout = 60 ? “Passed” : “Failed” ); 16 conditionValue if trueValue if false

The syntax for the if-else statement is : If (condition) { // block 1 } Else { //block 2 } => In the above, if the condition is true, then block 1 is executed, and then execution continues after the end of block 2; if the condition is false, then block 2 is executed (block 1 is skipped), and then execution continues after the end of block 2. 17

Compound statement – Set of statements within a pair of braces if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } – Without braces, cout << "You must take this course again.\n"; always executed Block – Set of statements within braces

Example: if ( age < 100 ) { // If the age is less than 100 cout<<"You are pretty young!\n"; } else { cout<<"You are old\n"; } 19

Else If (nested if) : Another use of else is when there are multiple conditional statements that may all evaluate to true, yet you want only one if statement's body to execute. You can use an "else if" statement following an if statement and its body; that way, if the first statement is true, the "else if" will be ignored, but if the if statement is false, it will then check the condition for the else if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else if statements to ensure that only one block of code is executed. 20

Nested if/else structures – One inside another, test for multiple cases – Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”

if ( ) { // Execute these statements if is TRUE } else if ( ) { // Execute these statements if is TRUE and // is FALSE } The syntax : 22

Example if ( grade >= 90 ) // 90 and above cout << "A"; else if ( grade >= 80 ) // cout << "B"; else if ( grade >= 70 ) // cout << "C"; else if ( grade >= 60 ) // cout << "D"; else // less than 60 cout << "F";

Example 24 if (x > 0) { cout << "x is positive"; } else if (x < 0) { cout << "x is negative"; } else { cout << "x is 0"; }

switch structure 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

26

switch structure -One or more statements may follow a case label. - Braces are not needed to turn multiple statements into a single compound statement. -switch, case, break, and default are reserved word.

28

Avoiding Bugs by Avoiding Partially Understood Concepts and Techniques: To output results correctly The switch structure must include a break statement after each cout statement

Program: Effect of break statements in a switch structure

Grade program with bugs

Exercice : write a c + + program that compare two values (num1 and num2) ​​using the operators we have seen ( using conditional statements ) 32