Csc201 Introduction To Programming Csc201 Futo 2020
Csc201 Introduction To Programming Csc201 Futo 2020
INTRODUCTION TO PROGRAMMING
Programming Errors
1. Syntax Error: Grammatical error or the set of rules that governs the structure and
meaning of statements in a program.
2. Semantic error: Meaningless program e.g division by zero or incorrect use of
brackets
3. Logic error: incorrect use of a token or keyword or operator e.g using = instead of
==
4. Compile time error: syntax and semantic error during compilation
5. Run time error: error during execution of program
Constants: A constant is a named location in memory that stores a fixed value. Unlike a
variable, the value of a constant cannot be changed during program execution. In Java,
you use the final keyword to declare a constant. Although you can use a variable instead
of a constant, the compiler will not permit you to inadvertently change the value of a
constant. For instance, given the code,
Operators: Operators refer to symbols that have a specific meaning. They are used to
define the kind of mathematical or logical operation performed on one or more operands.
The most common operators used to create expressions are arithmetic, relational, and
logical.
2. Relational operators are symbols used to make comparisons between operands and
return a boolean value depending on the result of the comparison. Expressions that are
created using these operators are called relational expressions. Only numeric values are
compared using these operators. Relational operators have lower precedence than
arithmetic operators.
5. After the installation is complete, in the Java Setup - Complete dialog box, click
Close.
4. On the Java (TM) SE Development Kit 7 Update 1 (64-bit) – Setup page, click
Next.
Overview of Programming:
1. Program: is a collection of ordered instructions that directs a computer to perform
a specific task. The instructions in a program are written using a programming language
and are later converted into a machine language that can be processed by a computer.
Create a source Code: Enter the following source codes using a programming text
editor (such as TextPad or NotePad++ for Windows
8. }
In Java, the name of the source file must be the same as the name of the public class with
a mandatory file extension of ".java". Hence, this file MUST be saved as "Hello.java".
public static void main(String[] args) { ...... }
Lines 5-7 defines the so-called main() method, which is the starting point, or entry point
for program execution. The {...} is the body of the method which contains programming
statements.
System.out.println("Hello, world!");
In Line 6, the statement System.out.println ("Hello, world!") is used to print the message
string "Hello, world!" to the display console. A string is surrounded by a pair of double
quotes and contain texts. The text will be printed as it is, without the double quotes
Do not enter the line numbers (on the left panel), which were added to help in the
explanation. Save the source file as "Hello.java". A Java source file should be saved with
a file extension of ".java". The filename shall be the same as the classname - in this case
"Hello".
If a Java development environment is being used, the program is compiled by invoking a menu
function rather than by running Java directly. The folder containing javac must be in the
programmer’s PATH environment variable.
Example 2:
To compile HelloWorld.java, type:
javac HelloWorld.java
The System.out.print() method displays an output and the cursor remains in the same line.
The output of the following print statement is displayed in the same line.
Example 4:
/* Test System.out.println() and System.out.print() */
public class PrintTest { // Save as "PrintTest.java"
public static void main(String[] args) {
System.out.println("Hello, world!"); // Advance the cursor to the beginning of next
line after printing
System.out.println(); // Print a empty line
System.out.print("Hello, world!"); // Cursor stayed after the printed string
System.out.println("Hello,");
System.out.print(" "); // Print a space
System.out.print("world!");
System.out.println("Hello, world!");
}
}
Output
Hello, world!
Hello, world! Hello,
world! Hello, world!
Output
The sum is 165
Example: Write a program that prints the area and circumference of a circle, given its
radius.
/*
* Print the area and circumference of a circle, given its radius.
*/
public class CircleComputation { // Saved as "CircleComputation.java"
public static void main(String[] args) {
// Declare variables
double radius, area, circumference;
final double PI = 3.14159265;
// Print results
System.out.print("The radius is "); // Print description
System.out.println(radius); // Print the value stored in the variable
System.out.print("The area is ");
System.out.println(area);
System.out.print("The circumference is ");
System.out.println(circumference);
}
Example:
1. If Statement: If statement is a control flow statement that is used to execute a
block of code depending on the result of a test expression. You can create an if statement
using the if keyword followed by the test condition within parentheses. The statements to
be executed when the specified condition is true are enclosed within braces following the
if statement.
Example: Syntax: (The if Statement) The syntax used to create an if statement is:
if (test expression) {
//...action statements
}
Class myclass {
Public static void main {string args[]} {
int salary = 3750
If {salary = 2500} {
System.out.printIn(“salary is greater than 2500”);
Salary = salary + (0.05*salary);
}
System.out.printIn(“salary;” +salary);
}
}
The action statements to be executed when the test expression returns false are enclosed
in the else block. If the test condition returns true, the statements in the if block are
executed. If the test condition returns false, the statements in the else block are executed.
if (test expression) {
//...action statements to execute when the test expression
returns true
} else
{
//...action statements to execute when the test expression
returns false
}
Class myclass {
Public static void main {string args[] {
The action statements to be executed when the result of the test condition matches the
constant value follow the case statements. Each case statement has an optional break
statement that moves the control of the program execution to the statement following the
switch block. The switch block also has a default statement that contains the code to be
executed when the result of the test condition does not match with any of the constant
values specified in the case statements.
10
Class switch{
Public static void main (string[] args){
int day = 5
Switch (day) {
Case 1
System.out.printIn(“Monday”);
Break;
Case 2
System.out.printIn(“Tuesday”);
Break;
Case 3
System.out.printIn(“Wednesday”);
Break;
Case 4
System.out.printIn(“Thursday”);
Break;
Case 5
System.out.printIn(“Friday”);
Break;
Default;
System.out.printIn(“it is weekend”);
}
}
}
During program execution, the test condition in the while loop is evaluated before the
action statements are executed. If the test condition returns true, the statements enclosed
within the while loop are executed. After executing the action statements, the program
control returns to the while statement where the test condition is tested once again. If the
test condition is true, the action statements within the while block are executed again.
This process continues until the test condition is false, in which case, the program
execution continues from the program statement following the while block.
11
Class whileloop {
Public static void main {string args[] {
int counter = 10
int i = 1
while (i <= counter) {
system.out.printIn(“counter =” + i);
i++;
}
}
}
During program execution, the statements within the do...while loop are executed once
before the test condition is evaluated. If the test condition is true, the action statements
within the do...while block are executed again. This process continues until the test
condition is false, in which case, the program execution continues from the program
statement following the do...while block.
Class Dowhile
Public static void main (Strign args[]){
int counter = 10;
int i = 1
do{
system.out.printIn(“counter;”+i);
i++
while (i <= counter);
}
}
12