Skip to content

Conversation

@Ayushi7456
Copy link
Contributor

Fixes #5
Added Jumps statement Lesson.
Issue name - new lesson: jumps statements

What changes made

  • added different jumps statement with code examples and explanation.
  • added syntax with examples.
  • added comments to make the lesson more understandable.

Checklist

  • Added description of change
  • Added code examples, test must pass
  • Added documentation so that the program is self-explanatory and educational.
  • Relevant documentation/comments is changed or added
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

@Utkarsh1504
Copy link
Owner

@Ayushi7456 I think you should remove the whole Conditional Jumps section from the lesson as it is already covered in this lesson https://utkarsh1504.github.io/DSA-Java/io-conditionals

Just covered the below topics in your Pull request:

  • break + code examples + explanation
  • continue + code examples + explanation
  • break vs continue (comparison)
  • break vs continue vs return

you if face any issue during the contribuion you ask in the discussion forum.

Thank you for your hard work.🎈

Copy link
Owner

@Utkarsh1504 Utkarsh1504 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the things look good to me, just do the requested changes and your PR is ready for merge

lessons/jumps.md Outdated
Comment on lines 14 to 232
## **Conditional Jumps**

Conditional jumps are used to jump to a specific line in a program. It has two parameters, the line number to jump to and a condition. There are five types of conditional jumps:

- `if`

- `if-else`

- `nested-if`

- `if-else-if`

- `switch case`

## **if**
`if` statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not. That is, if a certain condition is true then a block of statement is executed otherwise not.

### Syntax:

```java
if(condition)
{
// statements to excute if condition is true.
}
```
Note: Take care of braces here. If we do not provide the curly braces ‘{‘ and ‘}’ after **if( condition )** then by default `if` statement will consider the very first statement to be inside its block.
For ex:
```java
if(condition)
statement1
statement2
statement3
// Here if the condition is true, **if** block will consider only statement1 to be inside its block.
```

Example.
```java
public class ifDemo {
public static void main(String[] args) {
int i = 10;
if(i < 15)
{
System.out.println("10 is less than 15");
}
}
}
```
Output: `10 is less than 15`


## **if-else**

The `if` statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the `else` statement. We can use the `else` statement with `if` statement to execute a block of code when the condition is false.

### Syntax:
```java
if(condition)
{
// Executes this block if condition is true.
}
else
{
// Executes this block if condition is false.
}
```
Example.
```java
public class ifelseDemo {
public static void main(String[] args) {
int i = 10;
if(i < 15)
{
System.out.println("i is less than 15");
}
else
{
System.out.println("i is greater than 15");
}
}
}
```
Output: `i is less than 15`


## **nested-if**

Java allows us to nest `if` statements within `if` statements. That is, we can place an `if` statement inside another `if` statement.

### Syntax:

```java
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
```
Example.
```java
class NestedIfDemo{
public class void Main(String args[]){
int i = 10;
if(i==10)
{
// First if statement
if(i<15){
System.out.println("i is less than 15");
}
if(i<12){
System.out.println("i is less than 12 too");
}
else{
System.out.println("i is greater than 15");
}
}
}
}
```
Output:
`i is smaller than 15`
`i is smaller than 12 too`


## **if-else-if**
Here, a user can decide among multiple options. The `if` statements are executed from the top down. As soon as one of the conditions controlling the` if` is true, the statement associated with that `if` is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final `else` statement will be executed.

### Syntax:

```java
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
```
Example:
```java
class ifelseifDemo{
public static void main(String args[])
{
int i = 20;

if (i == 10)
System.out.println("i is 10");
else if (i == 15)
System.out.println("i is 15");
else if (i == 20)
System.out.println("i is 20");
else
System.out.println("i is not present");
}
}

```
Output: `i is 20`




## **switch-case**
The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.

### Syntax:
```java
switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
```
- Expression can be of type `byte`, `short`, `int`, `char` or an `enumeration`. Beginning with JDK7, expression can also be of type `String`.
- Duplicate case values are not allowed.
- The `default` statement is optional.
- The `break` statement is used inside the switch to terminate a statement sequence.
- The `break` statement is optional. If omitted, execution will continue on into the next case.

Example:
```java
class switchCaseDemo{
public static Main(String args[]){
int i = 9;
switch(i)
{
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 2.");
}
}
}
```
Output: `i is greater than 2.`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this section.

@Ayushi7456
Copy link
Contributor Author

I have made the changes. Please review and let me know if it's fine now.

Comment on lines 25 to 36
public class HelloWorld {
public static void main(String[] args) {
// Initially loop is set to run from 1-5
for(int i=1; i<=5; i++){
// terminate loop when i is 4.
if(i=4){
break;
System.out.print(i);
}
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class HelloWorld {
public static void main(String[] args) {
// Initially loop is set to run from 1-5
for(int i=1; i<=5; i++){
// terminate loop when i is 4.
if(i=4){
break;
System.out.print(i);
}
}
}
}
public class HelloWorld {
public static void main(String[] args) {
// Initially loop is set to run from 1-5
for(int i=1; i<=5; i++){
// terminate loop when i is 4.
if(i==4){
break;
System.out.print(i);
}
}
}
}

please fix the indentation, a small tweak in the code.

lessons/jumps.md Outdated
Let's see one by one how it works.

### **Unconditional Jumps**
## **break**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## **break**
## **Break Statements**

lessons/jumps.md Outdated

Conditional jumps are used to jump to a specific line in a program.
It has two parameters, the line number to jump to and a condition. there are two types of conditional jumps:
## **continue**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## **continue**
## **Continue Statement**

Comment on lines 50 to 61
public class HelloWorld {
public static void main(String[] args) {
// Initially loop is set to run from 1-5
for(int i=1; i<=6; i++){
// terminate loop when i is 4.
if(i=4){
continue;
System.out.print(i);
}
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class HelloWorld {
public static void main(String[] args) {
// Initially loop is set to run from 1-5
for(int i=1; i<=6; i++){
// terminate loop when i is 4.
if(i=4){
continue;
System.out.print(i);
}
}
}
}
public class HelloWorld {
public static void main(String[] args) {
// Initially loop is set to run from 1-5
for(int i=1; i<=6; i++){
// terminate loop when i is 4.
if(i==4){
continue;
System.out.print(i);
}
}
}
}

lessons/jumps.md Outdated
Comment on lines 99 to 100
public static void main(String[] args)
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public static void main(String[] args)
{
public static void main(String[] args) {

lessons/jumps.md Outdated
## **Methods returning a value**
```java
// Main method
class CodeExample{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class CodeExample{
class CodeExample {

lessons/jumps.md Outdated



## **return**
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## **return**
## **Return Statement**

Copy link
Owner

@Utkarsh1504 Utkarsh1504 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ayushi7456 Please fix the indentation and some suggested changes, and PR is ready to merge
Thanks

@Utkarsh1504
Copy link
Owner

@Ayushi7456 Thanks a lot, really appreciate your work.🎈🎉

@Utkarsh1504 Utkarsh1504 added approved for approved PRS hacktoberfest-accepted eligible PRs for hactoberfest ✅ Ready for merge approved pull request labels Oct 1, 2021
@Utkarsh1504 Utkarsh1504 merged commit abdc27c into Utkarsh1504:main Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved for approved PRS hacktoberfest-accepted eligible PRs for hactoberfest ✅ Ready for merge approved pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new lesson: Jumps Statements

2 participants