Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1
The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful only if your code is testing something simple and does not require multiple statements. if (y!= 0) z = x/y; else z = 0; SAME z = (y!=0)? x/y : 0; The two code segments above have the same effect exactly. However, the one on right is shorter. Pay attention to conditional operator format: Since it has three operands it is classified as a ternary operator. 2
The Conditional Operator z = (y!=0)? x/y : 0; condition is a normal Boolean expression that might appear in an if statement. Parentheses around the condition are not required but should be used to improve the readability. expression 1 is the overall value of the conditional expression if condition is true. expression 2 is the overall value of the conditional expression if condition is false. 3
The Conditional Operator Example: Write a program to enter an integer number and print Big if the number is greater than 10 otherwise print Small. Notice that a single statement for conditional operator is enough to do the required code. 4
if..else ladder and switch statement We studied if..else before and below is an example of nested or ladder if..else code (because it is similar to a ladder) 5
if..else ladder and switch statement The if..else ladder is like having multiple execution paths based on condition check: There is another way of writing this code by using the switch statement instead of the if..else ladder. 6
if..else ladder and switch statement Below is the same code by using switch statement: 7
if..else ladder and switch statement Notice the following important notes: The integral expression MUST be one of the integer types such as int, long, char ONLY. None-integer types are not acceptable. The body of the switch is enclosed by required curly braces { }. case key word is followed by an integral constant and a colon (:). Any number of statements can follow (:) and the last statement for each case must be break (otherwise it will continue executing next case). ONLY on of the case statements will be executed and if no one found the default will be executed and it s optional (if you want). 8
switch statement mistakes Some common mistakes (WRONG code) Not integer type Only constant values allowed of type integer This example can be solved by using if..else ladder ONLY. 9
switch statement mistakes Some common mistakes (WRONG code) What is wrong with this code? Can you spot the mistakes? 10
Using continue and break with loops In any of the loops we studied before we can make use of two statements: continue : will cause the loop to take the next cycle and ignore (skip) any statements that are in the loop after continue. break: will cause the loop to terminate and exit. Both statements can be used with for, while and do..while loops. These statements are usually used with if to test a condition and are executed if the condition is true. 11
Using continue with loops Compare the output of the following programs. The left is without using continue, the right is with continue. skip 12
Using break with loops We studied break with switch statement. The same statement can be used with loop to terminate the loop (stop it). Infinite loop 13
Homework (for LAB) Problem 1: Re-write the program given in Lecture 6-2, slide 10 to use the switch statement instead of the if..else ladder. Problem 2: Write a program to find the sum of some numbers (float), positive or negative, such that the program will print the total sum only if the user enters 0. Use infinite loop with break. Problem 3: Repeat the program in problem 2 above such that it sums only the positive numbers entered and skips the negative ones. Make use of continue. 14
Remember: If you don t practice C++ programming on the computer, YOU WILL NOT LEARN anything. To become a professional programmer you must try all the examples by yourself. 15
Thank You My Office, 3 rd floor, 303 Email: ali.kattan@nobleinst.com Website: www.alikattan.org 16