INTRODUCTION TO CONTROL STRUCTURES C++, Slides of C programming

The concepts of Rvalues and Lvalues in C++ programming. It explains the difference between expressions that appear on the left side of an equation (Lvalues) and those that appear on the right side (Rvalues). The document also covers the 'switch' multiple-selection structure and provides an example code for counting letter grades using the switch structure. likely to be useful for students studying programming or computer science.

Typology: Slides

2020/2021

Available from 08/21/2022

SamenKhan
SamenKhan 🇵🇰

231 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fundamentals of Programming
(FOP)
Military College of Signals, Rawalpindi
C++ Control Structures
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download INTRODUCTION TO CONTROL STRUCTURES C++ and more Slides C programming in PDF only on Docsity!

Fundamentals of Programming

(FOP)

Military College of Signals, Rawalpindi

C++ Control Structures

Rvalues and Lvalues

  • Lvalues
    • Expressions that appear on left side of equation
    • Can be changed (I.e., variables)
      • x = 4;
  • Rvalues
    • Expressions that appear on right side of equation
    • Constants, such as numbers (i.e. cannot write 4 = x;)
  • Lvalues can be used as rvalues, but not vice versa

‘switch’ Multiple-Selection Structure

  • Pseudocode example:

switch ( variable ) {

case value1: // taken if variable == value

statements

break; // necessary to exit switch

case value2:

case value3: // taken if variable == value2 or == value

statements

break;

default: // taken if variable matches no other cases

statements

break;

Did you notice?

1. You do not need to put braces around multiple

statements in each case

2. ‘Break’ allows you to break out of switch as

soon as you get a match

‘switch’ Multiple-Selection Structure true false . . . case a case a action(s) break case b case b action(s) break false false case z case z action(s) break true true default action(s) Remember: Select one out of many actions

22 // loop until user types end-of-file key sequence 23 while ( ( grade = cin.get() ) != EOF ) { 24 25 // determine which grade was input 26 switch ( grade ) { // switch structure nested in while 27 28 case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch 32 33 case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch 37 38 case 'C': // grade was uppercase C 39 case 'c': // or lowercase c 40 ++cCount; // increment cCount 41 break; // exit switch 42

Compares grade (an int)

to the numerical

representations of A and a.

break causes switch to end and

the program continues with the first

statement after the switch

structure.

We will discuss

this later

43 case 'D': // grade was uppercase D 44 case 'd': // or lowercase d 45 ++dCount; // increment dCount 46 break; // exit switch 47 48 case 'F': // grade was uppercase F 49 case 'f': // or lowercase f 50 ++fCount; // increment fCount 51 break; // exit switch 52 53 case '\n': // ignore newlines, 54 case '\t': // tabs, 55 case ' ': // and spaces in input 56 break; // exit switch 57 58 default: // catch all other characters 59 cout << "Incorrect letter grade entered." 60 << " Enter a new grade." << endl; 61 break; // optional; will exit switch anyway 62 63 } // end switch 64 65 } // end while 66

Notice the default statement, which

catches all other cases.

This test is necessary because

Enter is pressed after each

letter grade is input. This adds

a newline character that must

be removed. Likewise, we

want to ignore any

whitespace.

Enter the letter grades. Enter the EOF character to end input. a B c C A d f C E Incorrect letter grade entered. Enter a new grade. D A b ^Z Totals for each letter grade are: A: 3 B: 2 C: 3 D: 2 F: 1