Skip to content

Commit 36afa6c

Browse files
committed
Added materail for Section08 thus far.
1 parent 42b0f0d commit 36afa6c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2854
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Statements and Operators
2+
3+
## Overview
4+
5+
### Expressions, Statements and Operators
6+
7+
- Expressions
8+
- building blocks to statements
9+
- Statements and block statements
10+
- building blocks to C++
11+
-
12+
- Operators
13+
- Assignment
14+
- Arithmetic
15+
- Increment and decrement
16+
- Equality
17+
- Relational
18+
- Logical
19+
- Compound assignment
20+
- Precedence
21+
22+
## Expressions, Statements
23+
24+
- Expressions
25+
- the most basic building block of a program
26+
- "a sequence of operatros and operands that specifies a computation"
27+
- computes a value from a number of operands
28+
- There is much, much more to expressions - not necessary at this level...
29+
30+
```c++
31+
/* Expressions - examples */
32+
33+
34 // literal
34+
favorite_number // variable
35+
1.5 + 2.8 // addition
36+
2 * 5 // multiplication
37+
a > b // relational
38+
a = b // assignment
39+
```
40+
41+
- Statements
42+
- a complete line of code that performs some action
43+
- usually terminated with a semi-colon (`;`)
44+
- usually contain expressions
45+
- C++ has many types of statments
46+
- expression, null, compound, selection, iteration, declaration, jump, try blocks, and others.
47+
48+
```c++
49+
/* Statements - examples */
50+
51+
int x; // declaration
52+
favorite_number = 12; // assignment
53+
1.5 + 2.8; // expression
54+
x = 2 * 5; // assignment
55+
if (a > b) cout << "a is greater than b"; // if statement
56+
; // null statement
57+
```
58+
59+
## Using Operators
60+
61+
- C++ has a rich set of operators
62+
- unary, binary, ternary
63+
- Common operators can be grouped as follows:
64+
- assignment
65+
- arithmetic
66+
- increment/decrement
67+
- relational
68+
- logical (`not`, `and`, `or`, etc.)
69+
- member access (`.`)
70+
- other
71+
72+
## Assignment Operator (`=`)
73+
74+
**LHS = RHS**
75+
- RHS is an expression that is evaluated to a value
76+
- the value of the RHS is stored to the LHS
77+
- the value of the RHS must be type compatible with the LHS
78+
- the LHS must be assignable
79+
- assignment expressions is evaluated to what was just assigned
80+
- more than one variable can be assigned in a single statement
81+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Session Name="C:\Users\frank\Desktop\CPPExamples\Section8\Section8.workspace">
3+
<int Value="0" Name="m_selectedTab"/>
4+
<wxString Value="C:\Users\frank\Desktop\CPPExamples\Section8\Section8.workspace" Name="m_workspaceName"/>
5+
<TabInfoArray Name="TabInfoArray"/>
6+
<SerializedObject Name="m_breakpoints">
7+
<long Value="0" Name="Count"/>
8+
</SerializedObject>
9+
</Session>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
##
2+
## Auto Generated makefile by CodeLite IDE
3+
## any manual changes will be erased
4+
##
5+
## Debug
6+
ProjectName :=ArithmeticOperators
7+
ConfigurationName :=Debug
8+
WorkspacePath :=C:/Users/frank/Desktop/CPPExamples/Section8
9+
ProjectPath :=C:/Users/frank/Desktop/CPPExamples/Section8/ArithmeticOperators
10+
IntermediateDirectory :=./Debug
11+
OutDir := $(IntermediateDirectory)
12+
CurrentFileName :=
13+
CurrentFilePath :=
14+
CurrentFileFullPath :=
15+
User :=frank
16+
Date :=31/01/2018
17+
CodeLitePath :="C:/Program Files/CodeLite"
18+
LinkerName :=C:/MinGW/bin/g++.exe
19+
SharedObjectLinkerName :=C:/MinGW/bin/g++.exe -shared -fPIC
20+
ObjectSuffix :=.o
21+
DependSuffix :=.o.d
22+
PreprocessSuffix :=.i
23+
DebugSwitch :=-g
24+
IncludeSwitch :=-I
25+
LibrarySwitch :=-l
26+
OutputSwitch :=-o
27+
LibraryPathSwitch :=-L
28+
PreprocessorSwitch :=-D
29+
SourceSwitch :=-c
30+
OutputFile :=$(IntermediateDirectory)/$(ProjectName)
31+
Preprocessors :=
32+
ObjectSwitch :=-o
33+
ArchiveOutputSwitch :=
34+
PreprocessOnlySwitch :=-E
35+
ObjectsFileList :="ArithmeticOperators.txt"
36+
PCHCompileFlags :=
37+
MakeDirCommand :=makedir
38+
RcCmpOptions :=
39+
RcCompilerName :=C:/MinGW/bin/windres.exe
40+
LinkOptions :=
41+
IncludePath := $(IncludeSwitch). $(IncludeSwitch).
42+
IncludePCH :=
43+
RcIncludePath :=
44+
Libs :=
45+
ArLibs :=
46+
LibPath := $(LibraryPathSwitch).
47+
48+
##
49+
## Common variables
50+
## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables
51+
##
52+
AR := C:/MinGW/bin/ar.exe rcu
53+
CXX := C:/MinGW/bin/g++.exe
54+
CC := C:/MinGW/bin/gcc.exe
55+
CXXFLAGS := -std=c++14 -Wall -g -O0 -std=c++14 -Wall $(Preprocessors)
56+
CFLAGS := -g -O0 -Wall $(Preprocessors)
57+
ASFLAGS :=
58+
AS := C:/MinGW/bin/as.exe
59+
60+
61+
##
62+
## User defined environment variables
63+
##
64+
CodeLiteDir:=C:\Program Files\CodeLite
65+
Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix)
66+
67+
68+
69+
Objects=$(Objects0)
70+
71+
##
72+
## Main Build Targets
73+
##
74+
.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs
75+
all: $(OutputFile)
76+
77+
$(OutputFile): $(IntermediateDirectory)/.d $(Objects)
78+
@$(MakeDirCommand) $(@D)
79+
@echo "" > $(IntermediateDirectory)/.d
80+
@echo $(Objects0) > $(ObjectsFileList)
81+
$(LinkerName) $(OutputSwitch)$(OutputFile) @$(ObjectsFileList) $(LibPath) $(Libs) $(LinkOptions)
82+
83+
MakeIntermediateDirs:
84+
@$(MakeDirCommand) "./Debug"
85+
86+
87+
$(IntermediateDirectory)/.d:
88+
@$(MakeDirCommand) "./Debug"
89+
90+
PreBuild:
91+
92+
93+
##
94+
## Objects
95+
##
96+
$(IntermediateDirectory)/main.cpp$(ObjectSuffix): main.cpp $(IntermediateDirectory)/main.cpp$(DependSuffix)
97+
$(CXX) $(IncludePCH) $(SourceSwitch) "C:/Users/frank/Desktop/CPPExamples/Section8/ArithmeticOperators/main.cpp" $(CXXFLAGS) $(ObjectSwitch)$(IntermediateDirectory)/main.cpp$(ObjectSuffix) $(IncludePath)
98+
$(IntermediateDirectory)/main.cpp$(DependSuffix): main.cpp
99+
@$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) -MG -MP -MT$(IntermediateDirectory)/main.cpp$(ObjectSuffix) -MF$(IntermediateDirectory)/main.cpp$(DependSuffix) -MM main.cpp
100+
101+
$(IntermediateDirectory)/main.cpp$(PreprocessSuffix): main.cpp
102+
$(CXX) $(CXXFLAGS) $(IncludePCH) $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) $(IntermediateDirectory)/main.cpp$(PreprocessSuffix) main.cpp
103+
104+
105+
-include $(IntermediateDirectory)/*$(DependSuffix)
106+
##
107+
## Clean
108+
##
109+
clean:
110+
$(RM) -r ./Debug/
111+
112+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CodeLite_Project Name="ArithmeticOperators" Version="10.0.0" InternalType="User templates">
3+
<Plugins>
4+
<Plugin Name="qmake">
5+
<![CDATA[00010001N0005Debug000000000000]]>
6+
</Plugin>
7+
</Plugins>
8+
<Description/>
9+
<Dependencies/>
10+
<Settings Type="Executable">
11+
<GlobalSettings>
12+
<Compiler Options="-std=c++14;-Wall" C_Options="" Assembler="">
13+
<IncludePath Value="."/>
14+
</Compiler>
15+
<Linker Options="">
16+
<LibraryPath Value="."/>
17+
</Linker>
18+
<ResourceCompiler Options=""/>
19+
</GlobalSettings>
20+
<Configuration Name="Debug" CompilerType="MinGW ( MinGW )" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
21+
<Compiler Options="-g;-O0;-std=c++14;-Wall" C_Options="-g;-O0;-Wall" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="0">
22+
<IncludePath Value="."/>
23+
</Compiler>
24+
<Linker Options="" Required="yes"/>
25+
<ResourceCompiler Options="" Required="no"/>
26+
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
27+
<BuildSystem Name="Default"/>
28+
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
29+
<![CDATA[]]>
30+
</Environment>
31+
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="yes">
32+
<DebuggerSearchPaths/>
33+
<PostConnectCommands/>
34+
<StartupCommands/>
35+
</Debugger>
36+
<PreBuild/>
37+
<PostBuild/>
38+
<CustomBuild Enabled="no">
39+
<RebuildCommand/>
40+
<CleanCommand/>
41+
<BuildCommand/>
42+
<PreprocessFileCommand/>
43+
<SingleFileCommand/>
44+
<MakefileGenerationCommand/>
45+
<ThirdPartyToolName>None</ThirdPartyToolName>
46+
<WorkingDirectory/>
47+
</CustomBuild>
48+
<AdditionalRules>
49+
<CustomPostBuild/>
50+
<CustomPreBuild/>
51+
</AdditionalRules>
52+
<Completion EnableCpp11="no" EnableCpp14="yes">
53+
<ClangCmpFlagsC/>
54+
<ClangCmpFlags/>
55+
<ClangPP/>
56+
<SearchPaths/>
57+
</Completion>
58+
</Configuration>
59+
<Configuration Name="Release" CompilerType="MinGW ( MinGW )" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append">
60+
<Compiler Options="-O2;-Wall" C_Options="-O2;-Wall" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="0">
61+
<IncludePath Value="."/>
62+
<Preprocessor Value="NDEBUG"/>
63+
</Compiler>
64+
<Linker Options="" Required="yes"/>
65+
<ResourceCompiler Options="" Required="no"/>
66+
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Release" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
67+
<BuildSystem Name="Default"/>
68+
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
69+
<![CDATA[]]>
70+
</Environment>
71+
<Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath="" IsExtended="yes">
72+
<DebuggerSearchPaths/>
73+
<PostConnectCommands/>
74+
<StartupCommands/>
75+
</Debugger>
76+
<PreBuild/>
77+
<PostBuild/>
78+
<CustomBuild Enabled="no">
79+
<RebuildCommand/>
80+
<CleanCommand/>
81+
<BuildCommand/>
82+
<PreprocessFileCommand/>
83+
<SingleFileCommand/>
84+
<MakefileGenerationCommand/>
85+
<ThirdPartyToolName>None</ThirdPartyToolName>
86+
<WorkingDirectory/>
87+
</CustomBuild>
88+
<AdditionalRules>
89+
<CustomPostBuild/>
90+
<CustomPreBuild/>
91+
</AdditionalRules>
92+
<Completion EnableCpp11="no" EnableCpp14="no">
93+
<ClangCmpFlagsC/>
94+
<ClangCmpFlags/>
95+
<ClangPP/>
96+
<SearchPaths/>
97+
</Completion>
98+
</Configuration>
99+
</Settings>
100+
<VirtualDirectory Name="src">
101+
<File Name="main.cpp"/>
102+
</VirtualDirectory>
103+
<Dependencies Name="Debug"/>
104+
<Dependencies Name="Release"/>
105+
</CodeLite_Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./Debug/main.cpp.o
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Section 8
2+
// Arithmetic operators
3+
/*
4+
+ addition
5+
- subtraction
6+
* multiplication
7+
/ division
8+
% modulo or remainder (works only with integers)
9+
10+
+, -. * and / operators are overloaded to work with multiple types such as int, double, etc.
11+
% only for integer types
12+
*/
13+
#include <iostream>
14+
15+
using namespace std;
16+
17+
int main() {
18+
19+
int num1 {200};
20+
int num2 {100};
21+
22+
// cout << num1 << " + " << num2 << " = "<< num1+ num2 << endl;
23+
24+
int result {0};
25+
26+
result = num1 + num2;
27+
cout << num1 << " + " << num2 << " = "<< result << endl;
28+
29+
result = num1 - num2;
30+
cout << num1 << " - " << num2 << " = "<< result << endl;
31+
32+
result = num1 * num2;
33+
cout << num1 << " * " << num2 << " = "<< result << endl;
34+
35+
result = num1 / num2;
36+
cout << num1 << " / " << num2 << " = "<< result << endl;
37+
38+
result = num2 / num1;
39+
cout << num2 << " / " << num1 << " = "<< result << endl;
40+
41+
result = num1 % num2;
42+
cout << num1 << " % " << num2 << " = "<< result << endl;
43+
44+
num1 = 10;
45+
num2 = 3;
46+
47+
result = num1 % num2;
48+
cout << num1 << " % " << num2 << " = "<< result << endl;
49+
50+
result = num1 * 10 + num2;
51+
52+
cout << 5/10 << endl;
53+
54+
cout << 5.0/10.0 << endl;
55+
56+
cout << endl;
57+
return 0;
58+
}
59+

0 commit comments

Comments
 (0)