Lecture 2

By default white space is skipped variant for char ch

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 4.5; int y; y = int(x); • Cast Format: (type) expression • float x = 4.5; int y; y = (int) x; 16 Elementary I/O • #include <iostream.h> • cin >> X >> Y • Read first input into X, and second into Y. • What is read depends on the type of X, Y. • By default, white space is skipped. • Variant for char: ch = cin.get(); 18 I/O continued • cout << “A text “ << X << “\n” • For char you can also use cout.put(‘ ); \n’ • Flags and manipulators control formatting. • cout << setprecision(2) : 2 positions after . • cout.setf(ios::right); right justified • cout << setw(6) << “ABC” : field width • #include <iomanip.h> 19 The IF Statement if (A > B) { // do something } else { // do something else } 21 WHILE Functions (value and refererence) int max(int X, int Y) { return (X > Y) ? X : Y; } void max2 (int X, int Y, int& Larger) { Larger = ((X > Y) ? X : Y); } 20 Switch Statement switch (points) { case 0: grade = ‘ ; break; F’ case 1: case 2: grade = ‘ ; break; D’ default: cout << “error\n”; } 22 FOR LOOP cin >> nextvalue; sum = 0; for (int counter = 1; counter <= N; ++counter) cout << counter; while (nextvalue > 0) { sum += nextvalue; cin >> nextvalue; } Elements may be omitted, but not the ;; 23 Elements may contain several statements, separated by , 24...
View Full Document

This document was uploaded on 03/31/2014 for the course CS 114 at NJIT.

Ask a homework question - tutors are online