---
description: We already talked about if statements in last week lab, today we’re going to make a little revision about it and talk about the else part, finally, we will talk briefly about loops, which is one of the most interesting things in programming!
---
<h1 style='border: none'><center>Programming I Lab 3</center></h1>
<h2 style='border: none'><center>Conditions & Intro to Loops</center></h2>
<h5><center>The Islamic University of Gaza<br>Engineering Faculty<br>Department of Computer Engineering</center></h5>
<h6>Author: Mohammed Nafiz ALMadhoun<span style="float:right">2021/10/08</span></h6>
---
<p style='text-align:justify'>
We already talked about if statements in last week lab, today we’re going to make a little revision about it and talk about the else part, finally, we will talk briefly about loops, which is one of the most interesting things in programming!
</p>
<p style='text-align:justify'>
At first, you should know that this lab is just a revision for what you should know, if you want to know the details please return to your assigned textbook, especially section 3.
</p>
## Lab Objectives
- Be able to write complex conditions
- Be able to use else-if block.
- Understand the concept of loops.
- Be able to write simple loops.
## If Statements
### If-else Statment
<p style='text-align:justify'>
A lot of times when we write our if statements, we want to know if the condition is true, and do something about it and if it is false and do another thing about it, so that is why we have the else part, instead of checking the complement of the condition again, we can use the else part.
</p>
```java=
boolean cond = true;
if ( cond ) {
// This will be executed if the cond == true
} else {
// This will be executed if the cond == false
}
```
<div style="page-break-after: always;"></div>
### Else-if Statment
<p style='text-align:justify'>
Same as else part, the creators of programming languages notice that we usually have more than two paths, so they did a shortcut for us, instead of writing if-else then in the else part write another condition we can use the else if and execute more than two paths easily.
</p>
in the following example, we will get the grade of a mark, we will do it with and without the else-if blocks.
Here is an example if we don't have else-if blocks:
```java=
int testscore = 95;
char grade;
if (testscore >= 90) {
grade = 'A';
} else {
if (testscore >= 80) {
grade = 'B';
} else {
if (testscore >= 70) {
grade = 'C';
} else {
if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
}
}
}
```
And here it's with the else-if blocks:
```java=
int testscore = 95;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
```
A lot easier, right?
Note that without the else part, you will get F even if your score is 100!
### Inline if statments
There is a shorthand for if-else statements, this short-handed statments will be useful if you need to just return one value.
```java=
variable = (condition) ? valueIfTrue : valueIfFalse;
```
Here is an example if you want to know the minimum number between two numbers, using regular if-else, and using inline if.
Using regular if-else statment:
```java=
int a = 10;
int b = 20;
int min;
if (a < b) {
min = a;
} else {
min = b;
}
```
Using inline if statment:
```java=
int a = 10;
int b = 20;
int min = (a < b) ? a : b;
```
<div style="page-break-after: always;"></div>
## Introduction to loops
<p style='text-align:justify'>
What of the main things about programs is that it repeats a certain job, we could write loops in a different way, but they mean the same thing, repeat executing code until a certain condition.
</p>
### While loop
<p style='text-align:justify'>
one of the basic blocks in programming languages to repeat something is the while, the while syntax is very similar to if-statement syntax, you will give it a condition and it will repeat itself until the condition went false.
</p>
```java=
while (condition) {
// Do stuff until condition is false
}
```
Here is an example to print `"Hi from java"` ten times:
```java=
int count = 0;
while ( count < 10 ) {
// This will be executed 10 times ;)
System.out.println("Hi from java");
count ++;
}
```
## Lab Tasks
### Task 1: Check IUG ID
In this task, you should ask the user to enter an IUG ID, then you should print the following messages according to the ID.
- valid and starts with a `2`: `You are a female registered in 2020`
- valid and starts with a `1`: `You are a male registered in 2020`
- not valid: `Invalid IUG ID`
**Note:** to get a sub string from a string in Java, you could use substring method.
```java=
String test = "This is just a test";
System.out.println(test.substring(8,12));
// This will print `just`
```
`substring` method takes two inputs, the beginIndex, and the endIndex
<div style="page-break-after: always;"></div>
**Note:**: To compare a string don't use `==`, you should use `equals` method as following:
```java=
String test = "test";
if (test.equals("test")) {
// String equals test!
}
```
<center>

Task 1 Expected Output
</center>
### Task 2: Guess the number game
In this task you should use if statements and loops to create a game, this game will let the user guess a number.
If the player enters a bigger number it will print `The number is smaller`, if the player enters a smaller number it will print `The number is larger`.
<center>

Task 2 expected output
</center>
###### tags: `Programming` `Java` `IUG`
<center>End Of Lab 3</center>