2430 : Introduction to Programming in C

Operators Continued, Logical Expressions, Conditional Statements

Today we have gone through shorthand operators.
  • class = class + newstudent; can be written as: class += newstudent;
  • The same applies for -, *, /. For example: x /= 10; is the same as x = x / 10;
  • n = n + 1; may be written as n++; or ++n;
  • Know the difference between n++ and ++n.
    • int x, n = 7;
      x = 3 * n++;

      At the end, x = 21, n = 8.
    • int x, n = 7;
      x = 3 * ++n;

      At the end, x = 24, n = 8.
We then went over relational operators:
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • == Equality
  • != Inequality
As well as logical operators:
  • && and
  • || or (these two are pipes, can be typed by pressing shift-\ on your keyboard)
  • ! not

Conditional statements
if else conditional statements are covered today. Know how to use each of the following:
  • if (condition)
      program statement
  • if (condition)
      program statement
    else
      program statement
  • if (condition) {
      program statements
      program statements
    }
  • if (condition) {
      program statements
      program statements
    } else {
      program statements
      program statements
    }
Classwork program #1
Write a program that calculates the net pay for its employees. The inputs are number of hours worked and the hourly rate. The company you are writing this program for is very generous and pays its employees time and a half for all hours worked over 40. State tax is 10% and the Federal tax is 23%. Net pay is computed by subtracting both taxes from the gross pay.

Sample output of this program:
What is your name?
Enter the rate:
Enter the hours:
Name your net pay will be XX dollars!!!
Nested if statements
if statements can go into one another. For example:
if (condition) {
  statement1;
  if (condition)
    statement2;
}
else
  statement3;
else if statements
With multiple conditions that cause different outcomes, you can use else if statements. For example:
if (score > 89)
  printf("grade = A");
else if (score > 79)
  printf("grade = B");
else
  printf("Bad grade");
Classwork program #2
Write a program to calculate the commission on a stock Transaction. If the transaction amount is less than or equal to 5000 Dollars than the commission is 3%, but, there is a Minimum commission of $35. If the transaction is greater Than 5000 the commission is only 2% with no minimum Commission. (Hint: use a nested if statement)

Sample output of this program:
What is your transaction amount?
Your commission will be $XX dollars!

Homework Assignment
Your homework is to finish the two classwork programs (if you are not done), and read Chapter 4 in your textbook.