Computer Programming Flashcards
(31 cards)
Sequential Structure
Executes a sequence of statements in order.
Control structure
Provides alternatives to sequential program and are used to alter the flow of execution
2 control structures
Selection structure
Repetition Structure
Selection structure
The program executes partical statements depending on the given condition. This alters the flow of program by making a selection or choice.
Repetition Structure
The program repeats statement a certain number of times, depending on the given condition. This alters the fliw of program execution by the repetition of one or more statements.
One way selection
If
If…else
switch statement
If and If…else statements
Used to create one-way, two-way and multiple selections
Switch Statement
is used to test a single variablr against a series of values.
The syntax for If statement
int score = 70;
if (score >= 60 ) {
System.out.printlb(“The student passed!”);
}
The Two-way selection
Used to choose between (2) alternatives. The if…else statement is used to implement two-way selections in Java.
The syntax for If…else statement
if ( score >=60)
{
System.out.println(“The student passed!”);
}
else
{
System.out.println(“The student failed!”);
}
Compound statements or Block statements
These are any kinfdof statements grouped together within curly braces.
Syntax for compound Statement
if (price => 500.00) {
discount = price * 0.10;
salePrice = price - discount;
System.out.println(“The discounted price is “ + salePrice);
}
else {
System.out.println(“No discount!”);
System.out.println(“The price is “ + price”);
}
Multiple Selections
When one control statement, either selection or repetition is located within another, it is said to be nested.
Nested if statement
allows a program to perform multiple selections
Syntax for nested if
if (score => 90){
System.out.println(“The grade is A”);
}
else if (score => 80) {
System.out.println(“The grade is B”);
}
else if (score => 70) {
System.out.println(“The grade is C”);
}
else {
System.out.println(“The grade is D”);
}
Short Circuit Evaluation
Is a process which the computer evaluates a logical expression from left to right and stops as soon as the valur of the expression is determined.
Switch Statement
Allows a single variable to be tested for equality against a list of values and, depending on its value, executes a certain block of statements.
Rules for a switch case
The ff variables are allowed: int, byte, short, char, and String
Syntax for Switch Statement
char grade = “A”;
switch (grade)
{
case ‘A’:
System.out.println(“Excellent!”);
break;
case ‘B’:
System.out.println(“Very Good!”);
break;
case ‘C’:
System.out.println(“Good!”);
break;
case ‘D’:
System.out.println(“Satisfactory”);
break;
case ‘E’:
System.out.println(“Failed”);
break;
}
While
repeats a block of statements while a given condition evaluates to true
Infinite loop
A loop that continues to execute endlessly
Syntax for while loop
int num = 0;
while( num<= 10)
{
System.out.ptintln(num);
num++;
}
For Loop
executes a sequence of atatements multiple times and abbreviates the code that manages the loop variable. It is used when a definite number of loop iterations is required.