[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] / [CSE201] / 05
[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]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Tue Apr 16 09:30:56 PDT 2013

Contents


    cs201/05 -- if-then-else etc

      Previous -- objects

      [ 04.html ]

      Study Pages 99-115 if--then--else Selections

        This topic is vital -- all real programs have if-statements. They will be in quizzes, labs, projects, ....., and the final.

        3.1 The if Statement

        Making choices and decisions.

        Syntax 3.1 if statement

      1. if_statement::= "if" "(" condition ")" statement O( "else" statement ), an if_statement starts with an "if" and a parenthesis, then a condition and a close parenthesis and a statement, if may end with an optional "else" and another statement.
         		if(x<0) x= -x;

        Semantics

         		if(A) B else C
        A is evaluated and if true then B is executed but if A was false then C is executed.

         		if(A) B
        A is evaluated and if true B is executed otherwise nothing is done.

        Syntax 3.2 Block Statement

        A vital tool -- combines a sequence of statements into a single statement. Allows an "if" to select a complex piece of a program.
      2. block::= "{" # statement "}", A block starts with an open brace and ends with a matched close brace and contains a sequence of C++ statements.

        Most "if"s and "else"s have braces after them.

        Quality Tip 3.1 -- Layout braces correctly

        Each open brace should be vertically above it's closing brace.

        Productivity Hint 3.1 Tabs

        Experiments indicate you should indent the statements in a block 3 spaces

        Advanced topic 3.1 -- The selection operator

        Later

        3.2 Relational operators

        Take note of "<=", ">=", "==", "!=".

        Common Error 3.1 -- Confusing = with ==

        Quality tip 3.2 -- Zero Warnings

        Common Error 3.2 -- Comparing Floating point numbers

        Never use "==" with real numbers.

        3.3 Multiple Alternatives

        Advanced topic 3.2 -- the switch statement

        Later

        Common Error 3.3 -- Dangling Else

        3.4 Nested branches

        Take great care and use indenting!

        Testing for failed input

        Notice that if cin.fail() is true that any data that was input is suspect -- and will probably be garbage. So never write code like this:
         	cin >> a;
         	cout << sqrt(a);
         	if(cin.fail()) cout << "input error";

        Instead, something like this is more likely to work.

         	cin >> a;
         	if(cin.fail()) cout << "input error";
         	else cout << sqrt(a);

      Review Questions

      R3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9. Do all, hand in one.

      Review Exercise -- Syntax

      Here is the syntax of an assignment statement
    1. variable = expression;

      What is the syntax of an if-else statement?

      What is the syntax of an if statement with no else?

      What is the syntax of a block?

      Simple Exercise -- common error

      What is wrong with [ 05ex4.cpp ]

      Tricky Exercise -- Leap years

      What is wrong with [ 05ex5.cpp ]

      Exercise -- solving a quadratic equation

      [ 05ex3.cpp ]

      Exercise on if-else-if-else

      [ 05ex0.cpp ]

      Exercise on if-if-if

      [ 05ex1.cpp ]

      Note -- how to diagram complex logic

      [ 05Flow.gif ]
    2. -- an Activity Diagram of the following code
       	if(A) B else C
       	if(D) E else F

      Project 2 Due next time

      [ projects.html#P2 ]

      Quiz 2 next time on sequences of assignments

      1. Cheat sheet OK.
      2. Closed book.
      3. Calculator OK.
      4. No electronic communication.

      5. There will be no if's in this quiz.
      6. Follow the instructions: find the mistakes and correct them first.
      7. Walk through/trace the corrected program, one assignment statement at a time.
      8. Record the values of the variables, after each line is executed, in the given table. [ 03.html#Sequences of Assignments ]
      9. Demo Generating the start of the Fibbonaci series [ 05ex.cpp ]

      . . . . . . . . . ( end of section Quiz 2 next time on sequences of assignments) <<Contents | End>>

      Next -- Boolean Operations

      [ 06.html ]
  1. O::="Optional".

    Abbreviations

  2. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  3. Class::=A description of a type of object that includes the data it knows and the functions it can execute.
  4. Function::programming=A selfcontained and named piece of program that knows how to do something.
  5. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  6. OOP::="Object-Oriented Programming", Current paradigm for programming.
  7. Semantics::=Rules determining the meaning of correct statements in a language.
  8. SP::="Structured Programming", a previous paradigm for programming.
  9. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  10. Q::software="A program I wrote to make software easier to develop",
  11. TBA::="To Be Announced", something I should do.
  12. TBD::="To Be Done", something you have to do.
  13. UML::="Unified Modeling Language", industry standard design and documentation diagrams.
  14. void::C++Keyword="Indicates a function that has no return value".

( End of document ) <<Contents | Top