[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [CSci201] / 04
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] <04> [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Podcasts: [01] [02] [03] [04]
Labs: [01] [02] [03]
Thu Jan 17 15:34:01 PST 2008

Contents


    Chapter 2: pages 42-45 if statements

      Podcast on pages 42-44

      [ 04.mp3 ]

      Introduction

      The book has only three pages showing how to express a very important algorithmic structure: Selections. It hardly mentions how to do sequences in C++.

      Syntax of Sequences and Selections

      Reading

    1. ** page 42: a first example of a one way selection
    2. **** Box at top of page 43: Live by this advice. You won't regret it.
    3. ** page 43 -- you can omit the braces round a single statement.
    4. **** example of an if-else (two way selection).
    5. **** Box at top of page 44: Syntax of if statements
    6. +++ Keep It Simple -- always put in the brace!
    7. **** Page 44 -- the if-else-if-else structure
    8. +++ How to program a multiple choice situations:
       		if(condition1)
      			case1
       		else if(condition2)
      			case2
       		else if(condition3)
      			case3
       		else
      			case4
    9. ** Top of page 45 explains that if-else-if-else... is just putting and if-else in the else of an if-else.
      ++ You can also put an if inside an if -- as deep as you need to express the logic of you solution.
      +++ Braces are good for keeping track of complex nested ifs.
    10. +++ Comments also help:
       	if(A)
       	{
       		if(B)
       		{// A and B
       			...
       		}
       		else
       		{// A and not B
       			...
       		}//if(B)
       	}
       	else // not A
       	{// not A
       		if(B)
       		{// not A and B
       			...
       		}
       		else
       		{// not A and not B
       			...
       		}//if(B)
       	}//if(A)

      Trap -- == vs =

      Do not write
       		if( i = 1 )
      when you want to test for equality. The above compiles and runs but (1) the condition is always true and (2) it changes the variable i to be 1! Instead write
       		if( i == 1 )

      Questions on ifs

        What is a literal

        This is a value like 123, 1.23, or "Hello, World". It is a symbolic constant that represents itself. Numerical literals are normally typed using decimal notation (but C++ also does octal and hex if you need it). Strings like "Hello, World" as (currently) stored as a sequence of ASCII characters.

        When do I use if statements

        Any time that you need to make a program choose between two (or more) alternative sequences of actions.

        You should expect to be using if in every program you write.

        Can you use if statements for things other than sales

        YES. We discussed calculations of date, pressures in veessels, tax rules, .... etc etc .

        The example discounts in the book are wrong

        Quite possibly.

        Is there a limit on the number of if statements in a program

        No. You can use as many as you need to make it work the way it should work.

        Is C++ used in Graphics, Games, Special Effects, Movies

        Yes.

        However in Movies the C++ will be hidden inside an interface for movie makers to use.

        Do you terminate an if with something like an ENDIF

        No. An if-else ends at the end of the statement after the else ... which may be a sequence inside {braces}.

        This is historical and saves typing.

        What does -= mean on page 45

        This is the "subtract from" operator. It one of a series of operators:
        Short formLong form
        x -= e;x = x - e;
        x += e;x = x + e;
        x *= e;x = x * e;
        x /= e;x = x / e;
        etc.

        What syntax is used to construct if-statements

        See above. WHat separates two independent if statements Nothing. For example
         	if(A)
         	   B
         	else
         	   C
        
        
         	if(D)
         	   E
         	else
         	   F
        Flow diagram of above code

        If you have a } do you need a semicolon

        Semicolons (;) in C++ are used to terminate expressions:
         		x = e;
         		cin >> x...;
         		cout << e ....;
        So sometimes that will be appear before a brace, but not always. Statements can also end with braces and so you can see things like this
         	      }
         	   }
         	}
        Sometimes I've even done this
         	}  }  }

        What does nesting mean

        Here is the example from class
         	if( day == THU )
         	{
         	   if( time == 12 )
         	   {
         	      class = 201;
         	   }
         	   else if( time == 12 )
         	   {
         	      class = 375;
         	   }
         	   else
         	   {
         	      class = 0;
         	   }
         	}

        Isn't it not simpler to always use braces

        YES. But when i a hurray, in class, or short of space I often just forget them. Just like this: Here is the example from class
         	if( day == THU )
         	   if( time == 12 )
         	      class = 201;
         	   else if( time == 12 )
         	      class = 375;
         	   else
         	      class = 0;

        Can you avoid complex nestings

        Sometimes by writing complex conditions. See below.

        What are the logical operators on page 46 do in if statements

        This is subject of the next question and the next class.

        What is bool

        See the next class.

        Can I chain conditions together like I do in VBasic


        1. Can make this shorter by starting off with an example.

        2. Example: If you spend more then $100, bought exactly 5 items, and it was
        3. on the weekend. Only then would you win the super prize.

        4. Would a nested If inside a nested If be the only way to chain these 3 If's
        5. together? Or is it possible to somehow link them together in the first If
        6. statement like you can with AND in Visual Basic?

        YES you can chain these together, probably like this
      1. ammount > 100 and number == 5 and (day == SATURDAY or day==SUNDAY).

        This is what the next readings and class will be about: [ 05.html ] Logic.

      Exercise -- is this year a leap year

      The 2008 class developed the following program [ 04ex.cpp ] which calculate whether a year was a leap year in the Julian (pre 1582?) calendar.

      Next -- Comparison and Logical Operators

      [ 05.html ] with project 1 (straight forward program) due and quiz 2.

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. KDE::="Kommon Desktop Environment".
  3. TBA::="To Be Announced", something I should do.
  4. TBD::="To Be Done", something you have to do.
  5. UML::="Unified Modeling Language", [ uml.html ] (beginner's introduction to the UML).

End