Download presentation
Presentation is loading. Please wait.
Published byMerilyn Shelton Modified over 6 years ago
1
CSC113: Computer Programming (Theory = 03, Lab = 01)
Momina Moetesum Computer Science Department Bahria University, Islamabad
2
Control structures – I if / if-else statements
Week # 3
3
Decision Making: Equality and Relational Operators
Lower precedence than arithmetic operators
4
2 // Using if statements, relational
1 // Fig. 1.14: fig01_14.cpp 2 // Using if statements, relational 3 // operators, and equality operators 4 #include <iostream> 5 6 using std::cout; // program uses cout 7 using std::cin; // program uses cin 8 using std::endl; // program uses endl 9 10 int main() 11 { 12 int num1, num2; 13 14 cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; 16 cin >> num1 >> num2; // read two integers 17 18 if ( num1 == num2 ) cout << num1 << " is equal to " << num2 << endl; 20 21 if ( num1 != num2 ) cout << num1 << " is not equal to " << num2 << endl; 23 24 if ( num1 < num2 ) cout << num1 << " is less than " << num2 << endl; 26 27 if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << endl; 29 30 if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << endl; 33 1. Load <iostream> 2. main 2.1 Initialize num1 and num2 2.1.1 Input data 2.2 if statements Notice the using statements. Enter two integers, and I will tell you the relationships they satisfy: 3 7 The if statements test the truth of the condition. If it is true, body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {}. 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7
5
2.3 exit (return 0) Program Output
34 if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl; 37 38 return 0; // indicate that program ended successfully 39 } 2.3 exit (return 0) Program Output Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7
6
Precedence and associativity of operrators
Operators Associativity Type ( ) Left to Right Parentheses * / % Multiplicative Additive << >> Stream insertion / extraction < <= > >= Relational == != Equality = Right to Left Assignment
7
Common programming errors
Syntax error will occur if there is a space between ==,<=,>= or != Another syntax error is to reverse their order Confusing equality operator (==) with assignment operator (=) causes logical error
8
Program control Specifies the order in which statements are to executed Sequential execution Statements executed one after the other in the order written Transfer of control When the next statement executed is not the next one in sequence
9
Structured-Programming
Programs are easier to understand, test, debug and, modify. Rules for structured programming Only single-entry/single-exit control structures are used (Bohm and Jacopini) Rules: 1) Begin with the “simplest flowchart”. 2) Any rectangle (action) can be replaced by two rectangles (actions) in sequence. 3) Any rectangle (action) can be replaced by any control structure (sequence, if, if/else, switch, while, do/while or for). 4) Rules 2 and 3 can be applied in any order and multiple times.
10
Control Structures All programs can be broken down into 3 control structures Sequence structure Built into C++. Programs executed sequentially by default. Selection structures C++ has three types - if, if/else, and switch Any selection can be rewritten as an if statement Repetition structures C++ has three types - while, do/while, and for Any repetition structure can be rewritten as a while statement
11
Flowchart Flowchart single-entry/single-exit control structures
Graphical representation of an algorithm Drawn using certain special-purpose symbols connected by arrows called flowlines. Rectangle symbol (action symbol) Indicates any type of action. Oval symbol indicates beginning or end of a program, or a section of code (circles). single-entry/single-exit control structures Connect exit point of one control structure to entry point of the next (control-structure stacking). Makes programs easy to build.
12
Structured-Programming
Representation of Rule 3 (replacing any rectangle with a control structure) Rule 3
13
The if Selection Structure
Single selection structure used to choose among alternative courses of action Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” If the condition is true print statement executed and program goes on to next statement If the condition is false print statement is ignored and the program goes onto the next statement Indenting makes programs easier to read C++ ignores whitespace characters
14
The if Selection Structure
Translation of pseudocode statement into C++: if ( grade >= 60 ) cout << "Passed"; Diamond symbol (decision symbol) indicates decision is to be made Contains an expression that can be true or false. Test the condition, follow appropriate path if structure is a single-entry/single-exit structure
15
The if Selection Structure
Flowchart of pseudocode statement true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true
16
Self Practice Test if the value of the variable count is greater than 10. If it is, print “Count is greater than 10”. Test if the variable number is not equal to 7, print “The variable number is not equal to 7”.
17
The if/else Selection Structure
Double Selection Structure if Only performs an action if the condition is true if/else A different action is performed when condition is true and when condition is false Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";
18
The if/else Selection Structure
true false print “Failed” print “Passed” grade >= 60
19
Ternary conditional operator (?:)
Takes three arguments condition, value if true, value if false Our pseudocode could be written: cout << ( grade >= 60 ? “Passed” : “Failed” );
20
Self practice Pseudocode Write C++ statement using: If/else statement
if person’s age is less than or equal to 18 print “Child” else print “Adult” Write C++ statement using: If/else statement Ternary conditional operator
21
The if/else Selection Structure
Nested if/else structures Test for multiple cases by placing if/else selection structures inside if/else selection structures. if student’s grade is greater than or equal to Print “A” else if student’s grade is greater than or equal to Print “B” else if student’s grade is greater than or equal to Print “C” else if student’s grade is greater than or equal to Print “D” else Print “F” Once a condition is met, the rest of the statements are skipped
22
The if/else Selection Structure
Compound statement: Set of statements within a pair of braces Example: if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } Without the braces, cout << "You must take this course again.\n"; would be automatically executed Block Compound statements with declarations
23
Errors Syntax errors Logic errors Errors caught by compiler
Errors which have their effect at execution time Non-fatal logic errors program runs, but has incorrect output Fatal logic errors program exits prematurely
24
Self Practice -Identify and correct errors
1) if (gender == 1) cout<<“woman”<<endl; else; cout<<“man”<<endl; 2) if (c< 7); cout<< “c is less than 7 \n”; 3) if (c=> 7) cout<< “c is equal to or greater than 7 \n”;
25
Self Practice Write a C++ program that takes two positive integers from the user and then prints the larger number followed by the words “is larger”. If the numbers are equal, prints the message “These numbers are equal”.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.