DCIT104 Lecture 1
DCIT104 Lecture 1
PROGRAMMING
FUNDAMENTALS
College of Education
School of Continuing and Distance Education
2020/2021 – 2022/2023
Lecture Overview
• Input
• Process
• Output
...
z=x+y
Add 1 more to z (new instruction)
Put z to output
a) Multiply x by 2
b) Add 2 to x
c) Multiply x by ½
a) a = (x + y + z) / 3
b) a = (x + y + z) / 2
c) a=x+y+z
#include <iostream>
using namespace std;
2. The statement int wage; creates a variable named wage that is used to _____ the value 20.
a) input
b) output
c) Hold
Copy and paste the code below into your IDE and click RUN
#include <iostream>
using namespace std;
int main() {
int wage;
wage = 20;
return 0;
}
The following statement gets an input value and puts that value into
variable x: cin >> x; cin is short for characters in.
#include <iostream>
using namespace std;
int main() {
int dogYears;
int humanYears;
return 0;
} Google Confidential and Proprietary
Learn.zybooks.com
Basic Input (Contd.)
#include <iostream>
using namespace std;
int main() {
int dogYears;
int humanYears;
return 0;
} Google Confidential and Proprietary
Learn.zybooks.com
Basic Output
• The cout construct supports output; cout is short for characters out.
Outputting text is achieved via: cout << "desired text";.
• Text in double quotes " " is known as a string literal. Multiple cout
statements continue printing on the same output line.
• The statement cout << endl starts a new output line, called
a newline.
• The notation cout << ... gives the appearance of the item on the
right being "streamed" to cout (like items flowing along a stream
into a lake), where cout represents the computer's screen.
#include <iostream>
using namespace std;
int main() {
return 0;
} Google Confidential and Proprietary
Learn.zybooks.com
Outputting a Variable’s Value
Outputting a variable's value is achieved via: cout << x;. Note that no
quotes surround x.
Note that the programmer intentionally did not start a new output line
after outputting "Wage is: " so that the wage variable's value would
appear on that same line.
#include <iostream>
using namespace std;
int main() {
int userAge;
cin >> userAge; // Program will be tested with values: 15, 40.
return 0;
}
Google Confidential and Proprietary
Learn.zybooks.com
Outputting Multiple Items With One Statement
• In your IDE, modify the program on the next slide to use only two
output statements, one for each output sentence.
In 2014, the driving age is 18.
#include <iostream>
using namespace std;
int main() {
int drivingYear;
int drivingAge;
int numStates;
drivingYear = 2014;
drivingAge = 18;
numStates = 10;
return 0;
Google Confidential and Proprietary
} Learn.zybooks.com
Challenge Activity 3: Read Multiple User Inputs
• In the program below, write two cin statements to get input values
into birthMonth and birthYear. Then write a statement to output the
month, a dash, and the year. End with newline.
The program will be tested with inputs 1 2000 and then with inputs
5 1950.
Note: The input values come from user input, so be sure to use cin
statements, as in cin >> birthMonth, to get those input values
(and don't assign values directly, as in birthMonth = 1).
Learn.zybooks.com Google Confidential and Proprietary
Challenge Activity 3
#include <iostream>
using namespace std;
int main() {
int birthMonth;
int birthYear;
return 0;
}
Learn.zybooks.com www.id-book.com
Whitespace
Learn.zybooks.com 37
Bad Use of Whitespace
38
Learn.zybooks.com
Compiling code with comments and whitespace
Learn.zybooks.com
Syntax Errors
Learn.zybooks.com
Compiler Reporting a Syntax Error
Above, the 6 refers to the 6th line in the code, and the 27 refers to the 27th
column in that line
Learn.zybooks.com
In-Class Activity
#include <iostream>
using namespace std;
int main() {
• Compiler error messages are often unclear or even misleading. The message is
like the compiler's "best guess" of what is really wrong. Below is an example
of a misleading compiler error.
• The compiler indicates a missing semicolon ';'. But the real error is the missing
<< symbols.
• Sometimes the compiler error message refers to a line that is actually many
lines past where the error actually occurred. Not finding an error at the
specified line, the programmer should look to previous lines.
Learn.zybooks.com
Unclear Error Messages (Contd.)
• In the example below, the actual error is on line 3, but the compiler
continues processing until line 5
• On line 5, the compiler is confused, so it generates a message. But
the reported line number is past the actual syntax error.
• Upon not finding an error at line 5, the programmer should look at
earlier lines.
Learn.zybooks.com
Good Practice on Fixing Error Reported by the Compiler
45
Learn.zybooks.com
Fixing Syntax Errors
46
Learn.zybooks.com
Challenge Activity 5: Basic Syntax Error
#include <iostream>
using namespace std;
int main() {
int userNum;
userNum = 5;
return 0;
}
Using the code stub above, insert the following statements, correcting the one
syntax error in each statement.
Google Confidential and Proprietary
Learn.zybooks.com
Challenge Activity 5
• Note: These activities may test code with different test values. This
activity will perform two tests: the first with userNum = 5, the
second with userNum = 11.
48
Learn.zybooks.com
Challenge Activity 6: More Syntax Errors
#include <iostream>
using namespace std;
int main() {
int songNum;
songNum = 5;
return 0;
}
Note: These activities may test code with different test values. This activity will
perform two tests: the first with songNum = 5, the second with songNum = 9.
Google Confidential and Proprietary
Learn.zybooks.com
More Syntax Errors
Compile-time error
• Because a syntax error is detected by the compiler, a syntax error is known as
a type of compile-time error.
Logic Error
• A logic error, also called a bug, is an error that occurs while a program runs.
The program below has a bug that causes a logic error. The incorrect output is:
“500 beans in 3 jars yields totalBeans total”.
Can you fix the bug?
#include <iostream>
using namespace std;
numBeans = 500;
numJars = 3;