Method of work Divide and conqer! Start with the core - - PDF document

method of work
SMART_READER_LITE
LIVE PREVIEW

Method of work Divide and conqer! Start with the core - - PDF document

2013-09-10 Method of work Divide and conqer! Start with the core part of the program. Lecture 1 Add variables and libraries only


slide-1
SLIDE 1

2013-­‑09-­‑10 ¡ 1 ¡

Lecture ¡1 ¡

Basic ¡C++ ¡

Method ¡of ¡work ¡

  • Divide ¡and ¡conqer! ¡
  • Start ¡with ¡the ¡core ¡part ¡of ¡the ¡program. ¡
  • Add ¡variables ¡and ¡libraries ¡only ¡when ¡they ¡

become ¡needed ¡(as ¡opposed ¡to ¡”just ¡in ¡case”). ¡

  • Compile ¡aMer ¡each ¡line ¡(oMen!) ¡
  • Solve ¡compilaNon ¡problems ¡before ¡you ¡add ¡

more ¡code. ¡

Terminal ¡

  • Provide ¡a ¡command ¡line ¡where ¡you ¡can ¡enter ¡text ¡

commands, ¡right-­‑click ¡on ¡desktop ¡to ¡get ¡it ¡

  • Stops ¡and ¡wait ¡for ¡programs ¡to ¡finish ¡unless ¡you ¡add ¡

an ¡& ¡last ¡on ¡line ¡to ¡start ¡it ¡in ¡background ¡

  • Up-­‑arrow ¡retrieve ¡previuos ¡command ¡
  • Ctrl-­‑a ¡goes ¡to ¡beginning ¡of ¡line ¡
  • Ctrl-­‑e ¡goes ¡to ¡end ¡of ¡line ¡
  • Ctrl-­‑c ¡terminates ¡currently ¡running ¡program ¡
  • Ctrl-­‑z ¡suspends ¡currently ¡running ¡program ¡
  • ”fg” ¡resume ¡suspended ¡programs ¡in ¡foreground ¡
  • ”bg” ¡resume ¡suspended ¡programs ¡in ¡background ¡

Tool: ¡Emacs ¡

  • Invoked ¡from ¡command ¡line ¡as ¡”emacs” ¡
  • Emacs ¡is ¡the ¡extensible, ¡customizable, ¡self-­‑

documenNng ¡real-­‑Nme ¡display ¡editor. ¡

  • Emacs ¡can ¡do ¡anything, ¡everything ¡you ¡want ¡but ¡
  • ne ¡thing: ¡it ¡will ¡not ¡tell ¡you ¡how ¡to ¡do ¡it ¡
  • Emacs ¡work ¡without ¡using ¡graphical ¡menus ¡or ¡

mouse, ¡most ¡is ¡hidden ¡”under ¡the ¡hood” ¡

  • Only ¡a ¡few ¡basic ¡things ¡are ¡available ¡in ¡menus ¡
  • Be ¡curious ¡and ¡ask: ¡”How ¡do ¡I ¡... ¡in ¡emacs.” ¡

Tool: ¡Compiler ¡

  • Invoked ¡from ¡command ¡line ¡as ¡”g++11” ¡
  • Invokes ¡a ¡preprocessing ¡program ¡that ¡can ¡generate ¡

errors ¡

  • Translates ¡C++ ¡text ¡to ¡executable ¡binary ¡code ¡

– Issues ¡warnings ¡when ¡something ¡is ¡wrong ¡or ¡weird ¡ – Issues ¡errors ¡when ¡you ¡do ¡have ¡syntax ¡error(s) ¡ – Correct ¡errors ¡and ¡warnings ¡in ¡order! ¡

  • Invokes ¡a ¡postprocessing ¡linker ¡that ¡may ¡generate ¡

further ¡errors ¡

  • ”a.out” ¡is ¡the ¡default ¡name ¡of ¡generated ¡programs ¡

Comment ¡

  • Wri]en ¡along ¡with ¡C++ ¡code ¡
  • Ignored ¡by ¡compiler ¡
  • Tell ¡humans ¡something ¡useful ¡about ¡the ¡code ¡

// comment to end of line /* multi-line comment */ #if 0 not a real comment but a useful preprocessing directive to get the same effect #end

slide-2
SLIDE 2

2013-­‑09-­‑10 ¡ 2 ¡

Sequence ¡and ¡block ¡

  • A ¡sequenNal ¡list ¡of ¡statements ¡
  • Each ¡statement ¡is ¡terminated ¡with ¡a ¡semicolon ¡
  • All ¡statements ¡are ¡inside ¡some ¡block ¡
  • EnNre ¡block ¡form ¡a ¡compound ¡statement ¡
  • Statements ¡are ¡executed ¡one ¡by ¡one ¡in ¡order: ¡

{ // beginning of block

statement1; statement2; statement3;

} // end of block

Standard ¡skeleton ¡

// A minimal C++ program int main() { // your statements // ... return 0; }

Variable ¡(1 ¡of ¡2) ¡

Consists ¡of: ¡

  • Name ¡

– so ¡we ¡can ¡refer ¡to ¡it’s ¡storage ¡locaNon ¡ – at ¡lower ¡level ¡converted ¡to ¡an ¡adress ¡in ¡memory ¡ – can ¡be ¡aliased ¡by ¡way ¡of ¡reference ¡

  • Value ¡

– what ¡we ¡store ¡in ¡the ¡locaNon ¡ – will ¡never ¡be ¡empty, ¡not ¡even ¡before ¡we ¡fill ¡it ¡

  • Type ¡

– size ¡of ¡storage ¡locaNon ¡ – interpretaNon ¡of ¡stored ¡value ¡

Variable ¡(2 ¡of ¡2) ¡

Three ¡kinds ¡of ¡variables: ¡

  • Fundamental ¡(basic) ¡

– stores ¡a ¡value ¡of ¡fundamental ¡type, ¡nothing ¡more ¡

  • Object ¡

– stores ¡values ¡Ned ¡to ¡an ¡derived ¡type ¡(struct, ¡class, ¡union, ¡enum) ¡ – operaNons ¡associated ¡to ¡the ¡type ¡are ¡provided ¡ – more ¡later ¡in ¡the ¡course ¡

  • Pointer ¡

– stores ¡just ¡the ¡adress ¡of ¡some ¡other ¡variable ¡ – requires ¡cauNon: ¡what ¡if ¡the ¡adress ¡does ¡not ¡contain ¡said ¡ variable? ¡ – more ¡later ¡in ¡the ¡course ¡

Naming ¡of ¡variables ¡

  • The ¡name ¡should ¡inform ¡humans ¡of ¡the ¡purpose ¡of ¡the ¡
  • variable. ¡
  • Good ¡names ¡are ¡typically ¡5-­‑20 ¡characters. ¡
  • Choosen ¡name ¡become ¡an ¡iden4fier. ¡
  • Some ¡idenNfiers ¡are ¡reserved ¡for ¡use ¡by ¡the ¡language ¡
  • itself. ¡You ¡can ¡not ¡use ¡those ¡keywords. ¡
  • Name ¡must ¡begin ¡with ¡a ¡le]er ¡(or ¡underscore) ¡and ¡

contain ¡only ¡le]ers ¡and ¡underscore ¡

  • Names ¡are ¡case ¡sensiNve ¡
  • Case ¡is ¡by ¡convenNon ¡used ¡to ¡carry ¡informaNon ¡for ¡the ¡

programmer ¡according ¡to ¡coosen ¡style ¡rules ¡

Variable ¡declaraNon ¡and ¡definiNon ¡

  • You ¡specify ¡the ¡type ¡of ¡the ¡variable ¡followed ¡by ¡

the ¡name ¡of ¡the ¡variable. ¡

– Both ¡declares ¡and ¡defines ¡the ¡variable ¡in ¡most ¡cases. ¡ – The ¡type ¡must ¡be ¡one ¡in ¡a ¡predefined ¡set. ¡ – The ¡name ¡is ¡yours ¡to ¡choose ¡wisely. ¡ – The ¡variable ¡may ¡opNonally ¡be ¡iniNalized ¡(wise). ¡ – IniNalizaNon ¡use ¡curly ¡braces ¡{} ¡

  • DeclaraNon ¡

– Tells ¡the ¡compiler ¡the ¡variable ¡exists ¡somewhere. ¡

  • DefiniNon ¡

– Creates ¡a ¡variable ¡in ¡program ¡memory. ¡

slide-3
SLIDE 3

2013-­‑09-­‑10 ¡ 3 ¡

Constants ¡

  • A ¡variable ¡can ¡be ¡declared ¡const ¡
  • ModificaNon ¡of ¡a ¡const ¡variable ¡will ¡give ¡compilaNon ¡
  • error. ¡
  • The ¡compiler ¡can ¡treat ¡constant ¡variables ¡more ¡
  • efficiently. ¡
  • The ¡programmer ¡have ¡less ¡worries ¡with ¡constant ¡

variables ¡than ¡other. ¡Big ¡benefit! ¡

  • A ¡const ¡variable ¡is ¡much ¡be:er ¡than ¡a ¡literal ¡because ¡

you ¡refer ¡to ¡it ¡by ¡name, ¡and ¡change ¡it ¡at ¡one ¡place. ¡

  • Constants ¡use ¡upper ¡case ¡le]ers ¡by ¡convenNon. ¡

const int SIZE{1000};

Fundamental ¡type: ¡bool ¡

  • stores ¡either ¡1 ¡or ¡0 ¡
  • literal ¡true ¡alias ¡for ¡1 ¡
  • literal ¡false ¡alias ¡for ¡0 ¡
  • any ¡integer ¡is ¡automaNcally ¡converted ¡
  • integer ¡with ¡zero ¡value ¡becomes ¡false ¡
  • integers ¡not ¡zero ¡becomes ¡true ¡

bool i_am_legend; bool i_am_a_programmer{true}; bool awful{4711}; // will be true

Variable ¡lifeNme, ¡scope ¡

  • Each ¡variable ¡have ¡a ¡limited ¡scope. ¡It ¡is ¡only ¡usable ¡in ¡it’s ¡scope. ¡
  • Scope ¡is ¡determined ¡by ¡placement ¡in ¡code. ¡
  • Variables ¡start ¡to ¡exist ¡when ¡defined. ¡
  • Variables ¡cease ¡to ¡exist ¡at ¡the ¡end ¡of ¡the ¡block ¡it ¡was ¡defined ¡inside. ¡

{ // no variables exist bool b; // b exists { // b exists bool c; // both b and c exist } // only b exists }

Fundamental ¡type: ¡int ¡

  • usually ¡4 ¡byte ¡
  • signed ¡or ¡unsigned ¡
  • short ¡or ¡long ¡or ¡long ¡long ¡
  • stored ¡as ¡two’s ¡complement ¡
  • limited ¡range ¡
  • stores ¡exact ¡representaNon ¡of ¡any ¡integer ¡in ¡range ¡
  • wraps ¡around ¡when ¡overflow ¡or ¡underflow ¡

int x; int y{-4711}; unsigned long long int big{9876543210L};

Fundamental ¡type: ¡char ¡

  • very ¡limited ¡range ¡
  • stores ¡any ¡integer ¡in ¡range ¡
  • signed ¡or ¡unsigned ¡
  • displayed ¡as ¡symbol ¡in ¡ascii ¡table ¡(’ ’, ’0’, ’A’, ’a’) ¡
  • a ¡character ¡literal ¡fetch ¡corresponding ¡integer ¡from ¡ ¡

ascii ¡table ¡(32, ¡48, ¡65, ¡97) ¡

  • some ¡character ¡literals ¡must ¡be ¡escaped ¡with ¡backslash ¡
  • ne ¡byte ¡in ¡memory ¡

char c; char first_letter{’A’}; char digit_zero{48}; // ’0’ char single_quote{’\’’};

Fundamental ¡type: ¡float ¡

  • stored ¡as ¡sign, ¡exponent ¡(8 ¡bit) ¡ ¡

and ¡fracNon ¡(23 ¡bit) ¡

  • limited ¡precision ¡(only ¡6-­‑9 ¡significant ¡digits) ¡
  • large ¡but ¡limited ¡range ¡

float f, g, h; float rounded{1.1f};

slide-4
SLIDE 4

2013-­‑09-­‑10 ¡ 4 ¡

Fundamental ¡type: ¡double ¡

  • stored ¡as ¡sign, ¡exponent ¡(11 ¡bit) ¡ ¡

and ¡fracNon ¡(52 ¡bit) ¡

  • limited ¡precision ¡(only ¡15-­‑16 ¡significant ¡digits) ¡
  • huge ¡but ¡limited ¡range ¡
  • automaNcally ¡rounded ¡to ¡closest ¡storable ¡value ¡
  • Inf ¡or ¡–Inf ¡when ¡overflow ¡
  • NaN ¡when ¡not ¡representable ¡

double d; double pi{3.1415};

Fundamental ¡types: ¡void ¡and ¡auto ¡

  • void ¡

– Merely ¡a ¡placeholder ¡when ¡type ¡is ¡not ¡needed ¡ ¡

  • r ¡pointed ¡to ¡type ¡is ¡unknown ¡
  • auto ¡

– Merely ¡a ¡placeholde ¡when ¡compiler ¡should ¡figure ¡ ¡

  • ut ¡type ¡from ¡context ¡

– Do ¡not ¡use ¡unNl ¡you ¡know ¡how ¡to ¡live ¡without ¡it! ¡

void nothing; // compile error! auto something; // compile error!

Expressions ¡and ¡assignment ¡

  • A ¡variable ¡can ¡be ¡assigned ¡the ¡result ¡of ¡an ¡

expression ¡by ¡using ¡the ¡assignment ¡opertor. ¡ variable = expression;

  • 1. The ¡expression ¡is ¡evaluated ¡first ¡and ¡produce ¡a ¡

result ¡(rvalue). ¡

  • 2. The ¡variable ¡is ¡the ¡desNnaNon ¡(lvalue). ¡
  • 3. The ¡result ¡is ¡stored ¡in ¡the ¡variable. ¡

int x{5+7}; x = 8 * 3;

lvalue ¡and ¡rvalue ¡

  • An ¡expression ¡will ¡give ¡a ¡result ¡
  • The ¡result ¡is ¡oMen ¡just ¡a ¡value. ¡You ¡can ¡not ¡store ¡

something ¡in ¡a ¡value. ¡Such ¡value ¡is ¡called ¡a ¡ ”rvalue”. ¡

  • SomeNmes ¡the ¡result ¡is ¡a ¡reference ¡to ¡a ¡

storagelocaNon, ¡to ¡a ¡variable, ¡where ¡you ¡can ¡ either ¡read ¡what’s ¡there, ¡or ¡store ¡something ¡

  • new. ¡Such ¡value ¡is ¡called ¡an ¡”lvalue”. ¡
  • lvalues ¡normally ¡occur ¡to ¡leM ¡on ¡= ¡sign ¡
  • rvalues ¡normally ¡occur ¡to ¡right ¡of ¡= ¡sign ¡

Operators ¡// ¡Lecture ¡break ¡

ArithmeNc ¡

+ - * / %

Logic ¡

&& || !

Comparision ¡

== != < > <= >= ¡

Input ¡/ ¡Output ¡

>> <<

Assignment ¡

= += -= *= /= %=

Convenience... ¡

++ -- ?: [] ()

Pointer ¡

& * -> new delete new[] delete[]

Bitwise ¡

& | ~ ^ << >>

Operator ¡precedence ¡

  • Operators ¡are ¡not ¡applied ¡in ¡leM ¡to ¡right ¡order ¡
  • ”Important” ¡operators ¡are ¡applied ¡first ¡
  • Parenteses ¡are ¡used ¡to ¡change ¡precedence ¡
  • Parenteses ¡are ¡used ¡to ¡clarify ¡precedence ¡

1-2 * 3+4 * 5-6 + 7*8 // 65 (1-2)*(3+4)*(5-6)+(7*8) // 63 x<y && y<z || z<y && y<x (x<y && y<z) || (z<y && y<x)

slide-5
SLIDE 5

2013-­‑09-­‑10 ¡ 5 ¡

Short ¡circuit ¡

  • Logic ¡expressions ¡are ¡only ¡evaluated ¡if ¡needed ¡

true || (y < 5) // y will never be compared x < 7 || y < 5 // y will be compared iff x >= 7 false && (y < 5) // y will never be compared x < 7 && y < 5 // y will be compared iff x < 7

Derived ¡types ¡

  • Available ¡in ¡a ¡library ¡

– Library ¡must ¡be ¡included ¡in ¡program ¡ #include <iostream> // typical

  • Organized ¡in ¡a ¡namespace ¡

– Namespace ¡to ¡use ¡must ¡be ¡secified ¡ using namespace std; // typical

Derived ¡type: ¡string ¡

  • #include <string>
  • stores ¡a ¡sequence ¡of ¡characters ¡
  • some ¡characters ¡must ¡be ¡escaped ¡with ¡backslash ¡
  • literals ¡wri]en ¡inside ¡double-­‑quotes ¡
  • basic ¡associated ¡operaNons ¡(non-­‑exhausNve) ¡

int length() // number of characters char& at(int pos) // get specific character string substr(int pos, int length)

string empty; string month{”january”}; string quoted_string{”\”quotetion\””}; string six_quotes{R”xyz(””””””)xyz”}; month.length() // result will be 7 month.at(1) // result will be 97 (’a’)

Type ¡conversion ¡

from ¡\ ¡to ¡ string ¡ double ¡ float ¡ int ¡ char ¡ bool ¡ bool ¡ x?”t”:”f” ¡ ? ¡ ? ¡ x ¡ x?’t’:’f’ ¡ x ¡ char ¡ string(1,x) ¡ ? ¡ ? ¡ x-­‑’0’ ¡ x ¡ x!=’f’ ¡ int ¡ to_string(x) ¡ ¡ double(x) ¡ float(x) ¡ x ¡ // ¡single ¡digit ¡ x+’0’ ¡ x ¡ float ¡ to_string(x) ¡ double(x) ¡ x ¡ ceil(x) ¡ floor(x) ¡ round(x) ¡ int(x) ¡ ? ¡ ? ¡ double ¡ to_string(x) ¡ x ¡ x ¡// ¡warns ¡ float(x) ¡ ¡ ? ¡ ? ¡ string ¡ x ¡ stod(x) ¡ stof(x) ¡ stoi(x) ¡ // ¡char ¡at ¡n ¡only ¡ x.at(n) ¡ // ¡>> ¡ Convert ¡variable ¡x ¡from ¡one ¡type ¡to ¡another. ¡ #include <string> #include <cmath> // ceil, floor, round

AutomaNc ¡conversion ¡

  • Conversion ¡is ¡oMen ¡automaNc ¡
  • Expressions ¡converted ¡to ¡”highest” ¡type ¡
  • Conversion ¡to ¡desNnaNon ¡variable ¡happen ¡last! ¡

// first integer division // then automatic conversion float x{5/3}; // x = 1.00 // fist floating point division // then warn about precision loss int y{5.0/2*6}; // y = 15

Numeric ¡(type) ¡limits ¡

#include <limits>

  • Replace ¡”type” ¡with ¡a ¡fundamental ¡type ¡
  • Minimum ¡value ¡possible ¡to ¡store ¡

numeric_limits<type>::min()

  • Maximum ¡value ¡possible ¡to ¡store ¡

numeric_limits<type>::max() ¡ ¡

  • The ¡difference ¡between ¡1 ¡and ¡the ¡least ¡value ¡

greater ¡than ¡1 ¡that ¡is ¡representable ¡

numeric_limits<type>::epsilon()

¡

slide-6
SLIDE 6

2013-­‑09-­‑10 ¡ 6 ¡

Derived ¡type: ¡istream ¡

  • #include <iostream>
  • type ¡of ¡the ¡predefined ¡object ¡”cin” ¡
  • ”cin” ¡is ¡used ¡to ¡read ¡from ¡keyboard ¡
  • associated ¡operaNons ¡

>> // formatted input void ignore(int count, char stop) int get()

  • related ¡operaNons ¡

– getline(istream in, string line, char stop)

int age; cin >> age; cin.ignore(10, ’\n’); string name; getline(cin, name, ’\n’);

Derived ¡type: ¡ostream ¡

  • #include <iostream>
  • type ¡of ¡the ¡predefined ¡objects ¡”cout” ¡and ¡”cerr” ¡
  • ”cout” ¡is ¡used ¡to ¡write ¡normal ¡output ¡to ¡screen ¡
  • ”cerr” ¡is ¡used ¡to ¡write ¡error ¡output ¡to ¡screen ¡
  • associated ¡operaNons ¡

<< // formatted output

int age{7}; cout << age << ” years old\n”;

Forma]ed ¡input ¡

  • How ¡to ¡think ¡

– Keyboard ¡load ¡”train” ¡with ¡characters ¡ – Loading ¡”enter” ¡key ¡give ¡signal ¡for ¡train ¡departure ¡ – Train ¡stop ¡at ¡programs ¡staNon ¡ – Program ¡unload ¡every ¡character, ¡one ¡at ¡a ¡Nme ¡

  • Forma]ed ¡input ¡

– Discards ¡blank ¡characters ¡ – Tries ¡to ¡convert ¡nonblank ¡characters ¡to ¡requested ¡ type ¡(depend ¡on ¡desNnaNon ¡variable!) ¡ – Leaves ¡remaining ¡characters ¡on ¡train ¡

Forma]ed ¡output ¡

  • How ¡to ¡think ¡

– Program ¡load ¡”train” ¡with ¡characters ¡ – Loading ¡special ¡”endl” ¡or ¡”flush” ¡give ¡signal ¡for ¡ train ¡departure ¡ – Train ¡stop ¡at ¡screen ¡ – Screen ¡unloads ¡and ¡display ¡characters ¡

  • Forma]ed ¡output ¡

– Converts ¡values ¡to ¡a ¡character ¡sequence ¡ – Applies ¡formavng ¡according ¡to ¡set ¡rules ¡

Manipulators ¡for ¡formavng ¡

  • #include <iomanip>
  • Manipulate ¡input ¡

– hex, ¡oct, ¡dec ¡ – ws ¡

  • Manipulate ¡output ¡

– hex, ¡oct, ¡dec, ¡boolalpha, ¡noboolalpha ¡ – endl, ¡flush ¡ – setw(int ¡width) ¡ – leM, ¡right ¡ – sewill(char ¡symbol) ¡ – setprecision(int ¡digits) ¡ – fixed, ¡scienNfic ¡

Example ¡

  • Write ¡a ¡program ¡to ¡calculate ¡how ¡many ¡hours ¡
  • f ¡this ¡course ¡you ¡are ¡expected ¡to ¡work ¡on ¡

your ¡own ¡Nme ¡

– 4 ¡scheduled ¡sessions ¡each ¡week ¡on ¡average ¡ – 2 ¡hours ¡per ¡scheduled ¡session ¡ – 14 ¡weeks ¡of ¡scheduled ¡sessions ¡ – 6hp ¡for ¡enNre ¡course ¡ – 2p ¡for ¡each ¡3hp ¡ – 40hours ¡for ¡each ¡1p ¡

slide-7
SLIDE 7

2013-­‑09-­‑10 ¡ 7 ¡

SoluNon ¡1 ¡

2/3*6*40-14*4*2;

  • Compile ¡error ¡
  • Hard ¡to ¡understand ¡

SoluNon ¡2 ¡

int main() { 2/3*6*40-14*4*2; return 0; }

  • Compile ¡warning ¡
  • Hard ¡to ¡understand ¡

SoluNon ¡3 ¡

int main() { int x = 2/3*6*40-14*4*2; return 0; }

  • SNll ¡hard ¡to ¡understand ¡
  • Compile ¡warning ¡

SoluNon ¡4-­‑10 ¡

  • Missing ¡include ¡
  • Compile ¡error ¡due ¡to ¡namespace ¡
  • ComputaNon ¡error ¡
  • Hard ¡to ¡understand ¡
  • Not ¡generally ¡usable ¡
  • Not ¡user ¡friendly ¡

SelecNon ¡(1 ¡of ¡4) ¡

  • Choose ¡to ¡execute ¡a ¡block ¡only ¡if ¡some ¡

condiNon ¡is ¡evaluated ¡to ¡true. ¡

  • CondiNon ¡is ¡an ¡expressing ¡with ¡bool ¡result. ¡

if ( condition ) { // statements only if true }

SelecNon ¡(2 ¡of ¡4) ¡

  • Choose ¡to ¡execute ¡either ¡one ¡block ¡or ¡the ¡other ¡

based ¡on ¡a ¡condiNon. ¡ if ( condition ) { // statements if true } else { // statements if false }

slide-8
SLIDE 8

2013-­‑09-­‑10 ¡ 8 ¡

SelecNon ¡(3 ¡of ¡4) ¡

  • Choose ¡to ¡execute ¡exactly ¡one ¡of ¡several ¡blocks. ¡Avoid ¡this. ¡
  • Can ¡select ¡from ¡any ¡number ¡of ¡blocks. ¡Avoid ¡this. ¡

if ( condition1 ) { // statements if condition 1 is true } else if ( condition2 ) { // statements if condition 1 is false // but condition2 is true } else if ( condition3 ) { // statements if conditions 1 and 2 // are false but condition 3 is true } else { // statements otherwise (all conditions are false) }

SelecNon ¡(4 ¡of ¡4) ¡

  • Select ¡based ¡on ¡a ¡scalar ¡value. ¡Hesitate. ¡
  • Scalar ¡value ¡can ¡come ¡from ¡any ¡expression. ¡
  • Any ¡number ¡of ¡case ¡parts ¡can ¡be ¡included. ¡

switch ( scalar_expression ) { case const_scalar_1: { // statements if expression evaluates to const_scalar_1 break; } case const_scalar_2: { // statements if expression evaluates to const_scalar_2 break; } default: { // statements only if nothing else matched break; } }

RepeNNon ¡(1 ¡of ¡4) ¡

  • Repeat ¡a ¡block ¡a ¡predetermined ¡number ¡of ¡Nmes. ¡

for ( start; go_on_test; next ) { // statements to repeat } for ( int i{0}; i < 10; i = i + 1 ) { // statements executed 10 times // i will be in [0..9] here }

RepeNNon ¡(2 ¡of ¡4) ¡

  • Repeat ¡a ¡block ¡as ¡long ¡as ¡a ¡condiNon ¡is ¡fulfilled. ¡
  • Executed ¡zero ¡or ¡more ¡Nmes. ¡

// start; while ( go_on_test ) { // statements to repeat // next; } int i{0}; while ( i < 10 ) { // just as previous for-loop i = 1 + 1; }

RepeNNon ¡(3 ¡of ¡4) ¡

  • Repeat ¡a ¡block ¡as ¡long ¡as ¡a ¡condiNon ¡is ¡fulfilled. ¡
  • Executed ¡one ¡or ¡more ¡Nmes. ¡

do { // statements always executed at least once } while ( go_on_test ); int i{0}; do { // statements always executed at least once i = 1 + 1; } while ( i < 10 );

RepeNNon ¡(4 ¡of ¡4) ¡

  • RepeNNon ¡can ¡be ¡restarted ¡early. ¡Do ¡not ¡use. ¡
  • RepeNNon ¡can ¡be ¡stopped ¡early. ¡Avoid. ¡
  • Modify ¡your ¡condiNon ¡instead! ¡

while ( condition ) { if ( restart_condition ) { continue; // Do not use. } if ( stop_condition ) { break; // Avoid. } }

slide-9
SLIDE 9

2013-­‑09-­‑10 ¡ 9 ¡

NesNng ¡

  • Any ¡statement ¡can ¡be ¡part ¡of ¡any ¡other ¡statement. ¡
  • Nested ¡statements ¡are ¡indented ¡to ¡be ¡more ¡visible. ¡

if ( condition ) { do { // statements to repeat one or more } while ( condition ); } else { // statements if no repeat will take place }

Style ¡rules ¡in ¡the ¡course ¡

  • Nested ¡statements ¡are ¡properly ¡indented ¡
  • One ¡declaraNon ¡on ¡each ¡line ¡
  • Spaces ¡around ¡operators ¡
  • Write ¡short ¡lines ¡
  • Always ¡use ¡braces ¡
  • Every ¡brace ¡on ¡it’s ¡own ¡line ¡
  • Cohesive ¡statements ¡grouped ¡together ¡
  • Empty ¡lines ¡separate ¡groups ¡

Hints ¡for ¡Emacs ¡

(setq c-default-style ”ellemtel”) (global-linum-mode 1) (show-paren-mode 1) Install ¡auto-complete ¡in ¡emacs ¡ Tab ¡to ¡intend ¡ M-x indent-region M-/ Complete ¡word ¡(long ¡variable ¡names) M-% Query ¡replace ¡(refactor ¡variable ¡names) C-x ( Start ¡keyboard ¡macro C-x ) End ¡keyboard ¡macro

To ¡be ¡conNnued. ¡

This ¡page ¡is ¡not ¡leM ¡blank. ¡IntenNonally. ¡