Outline CS11600: Introduction to � Introduction to C and C++ Computer Programming (C++) � “Hello World!” program � Basic types Lecture 4 � Variables, constants and assignments � Expressions and operators � Control flow Svetlozar Nestorov � Input/Output (I/O) University of Chicago 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 2 Introduction to C and C++ “Hello World” Programs � The history of C and C++ � C program #include <stdio.h> • http://cm.bell-labs.com/cm/cs/who/dmr/chist.html int main() { • C was devised in 70’s at Bell Labs int year = 2003; • Predecessors of C: BCPL, B. printf("Hello %d World!\n“, year); } • Standardization: ANSI C in 1989. C++ program � • C++ is the most widely used successor of C. #include <iostream.h> � Differences between C and C++ int main() { int year = 2003; cout << “Hello ” << year << “ World!” << endl; } 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 3 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 4 Built-in Types Variables � Integers (signed or unsigned): � Variable definition: Type name [= value]; int, short, long � Examples: � Real numbers (always signed): int year = 2003; float, double, long double int year; � Characters (signed or unsigned): char c = ‘c’; char double pi = 3.14; � Others: � Note: variable name must start with a letter or _ and cannot be a C++ keyword. wchar_t, bool 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 5 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 6 1
More Variable Definitions Constants � Constants (aka constant variables): � Alternative initialization syntax: const Type name = value; Type name(value); � Examples: � Multiple variable definitions: const int year = 2003; Type name1(val1), name2(val2)…; const char c = ‘c’; � Examples: const char c2 = c; int year = 2003, nextYear; � Note: constant must be initialized and this char c(‘c’), d=‘d’, e; initial value cannot be changed. 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 7 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 8 Assignments Basic Math Operators � Arithmetic � Basic form: -, +, -, *, /, % name = expr; � Logical � expr can be a value (literal constant), ! (not), && (and), || (or) constant, variable, or an expression. � Bitwise � Another form: ~ (compl), & (bitand), | (bitor), ^ (xor) <<, >> name1 = name2 = name3 = expr; � Relational (comparisons) � Assigns expr to all three variables. <, >, <=, >=, ==, != (not_eq) 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 9 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 10 Update Operators Increment and Decrement � Update operators: � There are 4 variations: • Prefix increment: ++name +=, -=, *=, /=, %=, <<=, >>=, &=, |=, ^= � General form: • Prefix decrement: --name • Postfix increment: name++ name update_op expr; � Equivalent to: • Postfix decrement: name-- � Prefix updates the value before it is used name = name op expr; � Examples: in an expression while postfix updates it after it is used. i += 5; (same as i = i + 5;) c *= 10; (same as c = c * 10;) � Work for numbers and characters. 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 11 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 12 2
More Operators Control Flow � Conditional operator � Three kinds of control flow constructs: • Conditional: if, if-else, switch cond ? expr1 : expr2 � Evaluates to expr1 if cond is true, • Loop: for, while, while-do otherwise evaluates to expr2 • Jump: break, continue, goto � Comma operator expr1, expr2, … � Evaluates expressions left to right; value of operator is the last expression. 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 13 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 14 If Switch � If has three forms: � General form: switch (expr) { case constant1: if (condition) { if (condition) { if (cond1) { statements1; statements; statements1; statements1; break; } } } case constant2: else { else if (cond2){ statements2; statements2; statements2; break; } } … default: … statements1; else { break; statements3; } } � Default is optional; order of cases is arbitrary, several cases may share statements. 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 15 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 16 While For � General form: � Two forms: for (expr1; cond; expr3) { while (condition) { do { statements; statements; statements; } } } while (condition) � Equivalent to: expr1; � Evaluate condition, if true � Execute statements, while (cond) { execute statements and evaluate condition, if true repeat. repeat. statements; expr3; } 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 17 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 18 3
Break and Continue Input and Output (I/O) � Break jumps out of the innermost loop � Standard C++ IOStream library (type-safe I/O) immediately. cin >> name; (standard input) � Continue jumps to the next iteration of the cout << name; (standard output) innermost loop. cerr << name; (standard error) � Cascading form: � Goto jumps to a label anywhere in the program. cout << name1 << name2 …; cerr << name1 << name2 << …; � Goto is used very rarely . � Predefined constants: endl 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 19 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 20 Include Directives Puzzle � What is the output of this program? � Handled by C++ preprocessor. � Two forms: #include <iostream.h> int main() { #include “file” (current and standard dir) char e = ‘1'; int y(10), a=5, r; #include <file> (standard dir only) y--, --a; for (int i=0, r=y; i<a; r--); r = (y - a++ >= a ? --y - a : y + a); y-= 2*r, a-= y+r, e--; cout << y << e << a << r << endl; } 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 21 1/15/2003 Svetlozar Nestorov, CS 116: Intro to Programming II 22 4
Recommend
More recommend