1 / 8

C++ Language Tutorial

C++ Language Tutorial. Lesson 4 – Input and selection. Getting Input Selection - Switch Selection - if. Getting input. We can get input from the user from the same library that we use for output • < iostream > • From that library we use ‘ cin ’

foy
Download Presentation

C++ Language Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Language Tutorial Lesson 4 – Input and selection • Getting Input • Selection - Switch • Selection - if

  2. Getting input We can get input from the user from the same library that we use for output • <iostream> • From that library we use ‘cin’ • ‘cin’ for input, ‘cout’ for output

  3. Example • Include the library (you should be doing this anyway by now) – #include <iostream> • • Declare a variable – IntaNum; • • Use cin to put input into that variable – cin >> aNum; • cin will continue to read until return is pressed

  4. Data Types • Most basic data types can be read in • But beware, the wrong data in can cause a crash • The most generic data type is string • You can read numbers and text into a string • But, you then have to get the numeric data out • There are tools for this, we will not worry about them now S

  5. Selection You often need to make decisions in a program There are two ways to do this: • if / else • switch case • ‘if’ can use any logical statement to make a choice • ‘switch’ takes the value of a single variable

  6. Example (if) if(true) { // If some condition is true do something } else if(true) { // If some other condition is true do // something else } else // Otherwise do this

  7. Example (switch) intaNum = 1; switch(aNum) { case 0: // Do something break; case 1: // Do another thing break; case 2: // Do something else break; default: // Do this if all else fails }

  8. The end That concludes are last lesson you should now understand how to get input from the user And also make use of selection such as if or a switch. As allways the source code for the demonstration with fully explained comments (more extensive than the video), along with the execution able file is available on the site for download.

More Related