Homework the first
For loop
Using the for loop, you can repeat any number of times for any code
First, you need to identify the parameters, factors, or data that we give to the loop that is doing the loop
- The Initialize Value, which is a value for initializing the Loop, and it is the starting point from which the Loop starts
- The condition or the Test counter, which is the condition we will set for the For condition, according to which the program continues to iterate or whether it continues to repeat and the condition is related to Initialize Value
- The increment and the decreases the increase and decrease of the Initialize Value Every time the code executes, we increase, add or decrease the Initialize Value
Syntax
for(initialization, condition, incr/decr)
Example
void main() {
for(var i=0; i<=9 ;i++)
{
print(i);
}
}
output
0
1
2
3
4
5
6
7
8
9
IF statement
The IF statement is used to execute several block of code commands if a certain condition is fulfilled, and this condition returns two values: true and false.
Syntax
If (condition) {
//statement(s)
}
Example
void main () {
/* define a variable which hold numeric value */
var n = 35;
/* if statement check the given condition */
if (n<40){
print("The number is smaller than 40")
};
}
output
The number is smaller than 40
- In this example, we use the if statement to check a certain condition and if it is true, we just execute a set of code
- We create variable n and set its value to be 35
- We add a condition inside the if statement is 35< 40 and in this case return true because 35<40
- After checking the condition, the code is executed inside it, which is to print the sentence “the number is smaller than 40”
If else statement
In the if -else statement, a condition clause is set. If this condition is met, a set of code is executed inside the parentheses of the if statement. If not, and returns false, it will execute another code in the else.
Syntax
if(condition) {
// statement(s);
} else {
// statement(s);
}
Example
void main() {
var x = 20;
var y = 30;
print("if-else statement example");
if(x > y){
print("x is greater than y");
} else {
print("y is greater than x");
};
}
output
If-else statement example
The given number is even
In this previous example, we created a variable of type integer with the name num and stored in it a value equal to 20 and we used the if else statement to check whether it is an odd or even number and in this case the condition value will be true because 20 is an even number and it is stored inside the variable num so the if code will be executed and it prints this sentence “the given number is even”
Homework the first
Example
//# ┌─┐┬┌─┐┌┐┌┌─┐┌─┐┬─┐┌─┐┌─┐┌─┐┌─┐┌┬┐┌─┐┌┬┐┬ ┬
//# ├─┘││ ││││├┤ ├┤ ├┬┘└─┐├─┤│ ├─┤ ││├┤ │││└┬┘
//# ┴ ┴└─┘┘└┘└─┘└─┘┴└─└─┘┴ ┴└─┘┴ ┴─┴┘└─┘┴ ┴ ┴
//# __ __ ___
//# | | | | / \ Hussien Almalki
//# | |__| | / ^ \ Homework the first.
//# | __ | / /_\ \ Nov 13 2021
//# | | | | / _____ \ Instructor: Abdulrahmman
//# |__| |__| /__/ \__\
//#
//#
void main(){
for(var i=0; i<=9; i++){
if(i!=5){
print(i);
print("abed");
}
}
}
output
0
abed
1
abed
2
abed
3
abed
4
abed
6
abed
7
abed
8
abed
9
abed