forked from faranakR/full-cpp-tutorial-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_10_operators.cpp
More file actions
127 lines (98 loc) · 3.45 KB
/
chapter_10_operators.cpp
File metadata and controls
127 lines (98 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*=============================================================================
* CHAPTER 10: OPERATORS
* =============================================================================
*
* C++ Development Course - Consolidated Examples
* Original Author: Faranak Rajabi
*
* This file contains 7 code example(s) from Chapter 10.
*
* USAGE:
* - Review each example section
* - Uncomment the code you want to test
* - Compile: g++ -std=c++17 -Wall thisfile.cpp -o program
* - Run: ./program
*
* NOTE:
* Some examples are code snippets designed to illustrate specific
* concepts and may need additional context to compile.
*
=============================================================================*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// ============================================================================
// EXAMPLE 1: ERROR : defined a variable twice
// ============================================================================
int main () {
// ERROR : defined a variable twice
int x ;
int x { 5 }; // Compiler error : redefinition
int y { 6 };
std :: cout << add (x , y ) << ’\ n ’;
return 0;
// ============================================================================
// EXAMPLE 2: Function: main
// ============================================================================
int main () {
for ( int i {1}; i <= 10; ++ i ) {
if ( i == 4)
break ; // Exit loop when i reaches 4
std :: cout << i << ’\ n ’;
}
std :: cout << " After the loop \ n " ;
return 0;
// ============================================================================
// EXAMPLE 3: Function: main
// ============================================================================
int main () {
for ( int i {1}; i <= 10; ++ i ) {
if ( i == 4)
continue ; // Skip printing 4
std :: cout << i << ’\ n ’;
}
std :: cout << " After the loop \ n " ;
return 0;
// ============================================================================
// EXAMPLE 4: Function: main
// ============================================================================
int main () {
tryAgain : // This is a label
std :: cout << " Enter a non - negative number : " ;
double x {};
std :: cin >> x ;
// ============================================================================
// EXAMPLE 5: Function: main
// ============================================================================
int main () {
int x {5};
modifyValue ( x ) ;
std :: cout << x << ’\ n ’;
// ============================================================================
// EXAMPLE 6: Shorter way ( null pointers convert to false )
// ============================================================================
/*
// Shorter way ( null pointers convert to false )
if (! ptr2 ) {
std :: cout << " ptr2 is null \ n " ;
}
*/
// ============================================================================
// EXAMPLE 7: Always check before dereferencing
// ============================================================================
/*
// Always check before dereferencing
if ( ptr1 ) {
* ptr1 = 42; // Safe only if not null
}
*/
// ============================================================================
// MAIN FUNCTION
// ============================================================================
// Uncomment the examples above and add your test code here
int main() {
std::cout << "Chapter 10: Operators" << std::endl;
// Your test code here
return 0;
}