• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

C. Programming Concepts > Conditional Statements

Conditional Statements

Ordinarily, your program statements are carried out in the order in which they appear. Sometimes you may want to do different calculations depending upon some condition. You use the if statement to do this. Here’s a calculation for wages that depends upon the number of hours worked. We presume that all the var statements have been set up appropriately.

if (hours <= 40)     [1]
{
    pay = hours * rate;     [2]
}
else     [3]
{
    pay = 40 * rate + (hours - 40) * rate * 1.5;     [4]
}

[1] The expression in the parentheses is called the condition. It always asks a yes-or-no question. In this case, the question is “is the value of the hours variable less than or equal to 40?” Other comparison operations are less than (<), greater than (>) greater than or equal (>=), exactly equal (==), and not equal (!=). Note that asking if two things are equal to each other requires two equal signs, not one!


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint