CSC 113 COMPUTER PROGRAMMING THEORY 03 LAB 01
CSC 113: COMPUTER PROGRAMMING (THEORY = 03, LAB = 01) Momina Moetesum Computer Science Department Bahria University, Islamabad
CONTROL STRUCTURES –I IF / IF-ELSE STATEMENTS Week # 3
DECISION MAKING: EQUALITY AND RELATIONAL OPERATORS Equality and relational operators � Lower precedence than arithmetic operators
1 // Fig. 1. 14: fig 01_14. cpp 2 // Using if statements, relational 3 // operators, and equality operators 4 #include <iostream> 1. Load <iostream> 5 6 using std: : cout; // program uses cout Notice the using statements. 7 using std: : cin; // program uses cin 2. main 8 using std: : endl; // program uses endl 9 10 int main() 2. 1 Initialize num 1 and 11{ num 2 12 int num 1, num 2; 13 2. 1. 1 Input data 14 cout << "Enter two integers, and I will 15 << "the relationships they satisfy: "; tell youn" 16 cin >> num 1 >> num 2; Enter // read two integers, and 2. 2 I if will statements 17 18 if ( num 1 == num 2 ) tell you relationships they satisfy: 19 cout << num 1 << " the is equal to " << num 2 The if statements test the 20 3 7 << endl; truth of the condition. If it is 21 if ( num 1 != num 2 ) 22 cout << num 1 << " is not equal to "3 true, body of if statement << is not equal to 7 is executed. If not, body is 23 num 2 << endl; 24 if ( num 1 < num 2 ) skipped. 25 cout << num 1 << " is less than " << To include multiple 3 num 2 is less than 7 26 << endl; statements in a body, 27 if ( num 1 > num 2 ) 28 cout << num 1 << " is greater than " delineate them with braces << {}. 4 29 num 2 << endl; 30 if ( num 1 <= num 2 ) 31 cout << num 1 << " is less than or equal 32 << num 2 << endl; to "
34 if ( num 1 >= num 2 ) 35 cout << num 1 << " is greater than or equal to " 36 << num 2 << endl; 37 38 return 0; // indicate that program ended successfully 39} Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 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 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 2. 3 exit (return 0) Program Output 5
PRECEDENCE AND ASSOCIATIVITY OF OPERRATORS Operators Associativity Type ( ) Left to Right Parentheses * / % Left to Right Multiplicative + - Left to Right Additive << >> Left to Right Stream insertion / extraction < <= > >= Left to Right Relational == != Left to Right Equality = Right to Left Assignment
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
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 8
STRUCTURED-PROGRAMMING 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/else, switch, while, do/while or for). 4) Rules 2 and 3 can be applied in any order and multiple times. 9
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 10
FLOWCHART Flowchart � 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. 11
STRUCTURED-PROGRAMMING Representation of Rule 3 (replacing any rectangle with a control structure) Rule 3 12
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 13
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 14
THE IF SELECTION STRUCTURE Flowchart of pseudocode statement grade >= 60 true print “Passed” A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true false 15
SELF PRACTICE 1. 2. 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”.
THE IF/ELSE SELECTION STRUCTURE Double Selection Structure if � if/else � Only performs an action if the condition is true 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"; 17
THE IF/ELSE SELECTION STRUCTURE false true grade >= 60 print “Failed” print “Passed” 18
TERNARY CONDITIONAL OPERATOR (? : ) Takes three arguments � condition, � value if true, � value if false Our pseudocode could be written: cout << ( grade >= 60 ? “Passed” : “Failed” );
SELF PRACTICE Pseudocode 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
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 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” � Once a condition is met, the rest of the statements are skipped 21
THE IF/ELSE SELECTION STRUCTURE Compound statement: Set of statements within a pair of braces � Example: � if ( grade cout << else { cout << again. n"; } >= 60 ) "Passed. n"; "Failed. n"; "You must take this course Without the braces, cout << "You must take this course again. n"; would be automatically executed � Block � Compound statements with declarations 22
ERRORS Syntax errors � Errors caught by compiler Logic errors � Errors which have their effect at execution time Non-fatal logic errors program runs, but has incorrect output Fatal logic errors program exits prematurely 23
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”;
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”.
- Slides: 25