|
Main Page Helpful Links Downloads Tutorials Grades Syllabus Contact Me |
Here is where we really begin programming. Up to now, our programs have run in a straight line, from start to finish. Here is where that changes and the programs come to life. Now, flow of the program will be option driven. Operators For Conditionals Conditional statements operate on truth and falsehood. A simple if statement only executes if it's condition is true. In C and many other languages, truth and falsehood are numerical. A number with the value 0 evaluates to false. Any other non-zero number evaluates to true. By convention, 1 is often used to represent truth. So, the following operators produce 1's and 0's based on the values of their operands. Comparison Operators
Again, it's important to remember that == and != are unsuitable for comparing floats or doubles. Also, any tests performed must be enclosed in parentheses (). And be sure not to confuse the == operator with the = operator for assignment. Logical Operators
These operands are important for forming complex logical statements. The '|' symbol is typically called the pipe symbol. One my keyboard, it's entered by holding shift and hitting the '\' key. An Example Complex Condition For an example, let's use a piece of medical advice. Men over 50 should always go in for an annual prostate exam, to detect early prostate cancer. How could we encode this in a program? Let's assume we have a char called sex containing 'm' for male or 'f' for female. We also have an unsigned integer called age. We want to print a message telling the user they whether they need the exam or not. if ((sex == 'm') && (age >= 50)) printf("You need a prostate examination.\n"); else printf("You don't need a prostate examination.\n"); The logic here is simple. We verify that sex is equal to 'm'. Then, we verify that age is greater than or equal to 50. Finally, we use the && operator to make sure both previous conditons are ture. The if/else clause then acts on that. If Statements A simple if statement consists of a condition, and a statement to execute. if (condition) statement; We can also use {} to form a compound statement or code block, allowing us to execute multiple statements for one if statment. if (condition) { statement; statement; statement; } There is also an else clause we can add to an if statement. This lets us add an additonal clause to execute should the original condition have been false. if (condition) statement; else statement; Like before, we can use compound statements with the if/else form. if (condition){ statement; statement; statement;} else { statement; statement;} Finally, remember that you may place whatever code you wish into an if statement, including other if statements. In programming, this is called nesting, when we place one thing inside another. Nested if statements can be important for specifying different kinds of logic. Switch Statements Along with if statements, switch statements can be used to fork program execution. Switch statments are particulary useful when only one variable is tested for many different values. One must remember that because switch statements operate on equality, they should not be used with floats or doubles. (Heard this enough? It's important.) switch (variable){ case value1: statement; // this only executes if variable == value1 break; // break out of the switch statement case value2: statement1; statement2; // these statements only execute if variable == value2 break; // break out of the switch statement default: statement; // executes only if no other matches were found } About the most important thing to touch on is the break statement. When executed, it causes execution to jump out of the switch statement. Otherwise, execution will just continue down through it. For instance, if variable == value1, but we didn't have any break statements, all the statements in the switch block would be executed. The final part uses the default: label. We will execute the default portion only if variable wasn't equal to any of the choices we supplied. Notice the final portion doesn't have a break statement. Since it's the end of the switch block, there's no point in having it. |