Skip to content

Commit 914eba1

Browse files
authored
Merge pull request #18 from DurveshKumarPal/patch-1
Some changes
2 parents 5b60da8 + b1e2503 commit 914eba1

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

lessons/io-conditionals.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ Okay so let's learn about the control flow of a Java program.
2020

2121
Let's go through the code line by line.
2222

23-
- `#include` is a preprocessor directive that includes a header file. `#`(hash) is a special character that is used to indicate that the rest of the line is a preprocessor directive.
24-
- `iostream` is a header file that contains the input/output class.
25-
- `using namespace std;` is a preprocessor directive that tells the compiler to use the standard library. `using` is a keyword that is used to tell the compiler to use a namespace. `std` is a namespace that contains the standard library.
26-
- `int main()` is a function definition. The execution of a program begins with the execution of the main function and it is mandatory for a C++ program.
27-
- `cout << "Hello World!" << endl;` is a function call. `cout` is the output stream, `<<`(Insertion Operator) is a function call, and `Hello World!` is the string to be output. `<<`(Extraction Operator) is a left-to-right operator and `endl` is used to add a line break.
28-
- `return 0;` is a function return and `0` is the return value.
29-
- `{` is the start of a block and inside the block is known as **Function body**.
30-
- `}` is the end of the function.
31-
- `;` is the end of the line and also it is very important that the line ends with a semicolon otherwise the compiler will give an error.
32-
33-
Okay that's a lot of information to digest and it is the basic for a C++ program. Now let's move on to the next section.
23+
- **``public static void main(String[] args)``** Java main method is the entry point of any Java program. You can only change the name of String array argument, for example you can change ``args`` to ``myStringArgs``.
24+
- **``public``** This is the access modifier of the main method. It has to be ``public`` so that Java Runtime Environment(JRE) can execute this method. Remember that if you make any method non-public then it’s not allowed to be executed by any program, there are some access restrictions applied. So it means that the main method has to be public.
25+
- **``Static``** When JRE starts, there is no object of the class present. That’s why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present.
26+
- **``Void``** Java programming mandates that every method provide the return type. Java main method doesn’t return anything, that’s why it’s return type is void. This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM. If we try to return something from the main method, it will give compilation error as an unexpected return value.
27+
- **``Main``** method is similar to the main function in C and C++.
28+
The ``main`` method accepts a single argument: an array of elements of type String.
29+
- **``String[] args``** Java main method accepts a single argument of type String array. This is also called as java command line arguments.
30+
- **``System.out.println("Hello, World");``** Outputs the string “Hello, World” followed by a new line on the screen.Output is actually accomplished by the built-in println( ) method. ``System`` is a predefined class that provides access to the system, and ``out`` is the variable of type output stream that is connected to the console.
31+
32+
> System, out, println these are default classes and methods built into Java. If you want to read docs do a `ctrl+click` (IntelliJ Idea) on them.
33+
>
34+
Okay that's a lot of information to digest and it is the basic for a Java program. Now let's move on to the next section.
3435

3536
## **If-Else Statements**
3637

@@ -42,7 +43,7 @@ It will be really easy to understand as we will see in the following example.
4243

4344
if-else statements are written as follows.
4445

45-
```cpp
46+
```java
4647
// syntax
4748
if (condition) {
4849
// Code to execute if condition is true
@@ -80,17 +81,37 @@ Lets go through the if-else code line by line.
8081

8182
ok so that's all. now let's see some more examples.
8283

83-
```cpp
84-
// code goes here
84+
```java
85+
//A Java Program to demonstrate the use of if-else statement.
86+
//It is a program of odd and even number.
87+
public class IfElseClass {
88+
public static void main(String[] args) {
89+
//defining a variable
90+
int number = 13;
91+
//Check if the number is divisible by 2 or not
92+
if(number % 2 == 0){
93+
System.out.println("even number");
94+
}
95+
else{
96+
System.out.println("odd number");
97+
}
98+
}
99+
}
85100
```
86101

102+
Let's go through the code line by line.
103+
104+
- `if` is the keyword that indicates that the code block is an if statement. `(number%2==0)` is the condition. we can have multiple conditions in a single if statement. we're checking if ``number`` is divisible by ``2`` if this turn out to be true then the code block under the if statement will be executed, if not then we'll move on the next statement.
105+
- `else` is the keyword that indicates that the code block is an else statement. if the condition from the above statements are turn out to be false then the code block under the else statement will be executed.
106+
107+
87108
## **Switch Statements**
88109

89110
Switch statements are used to execute different code depending on the value of a variable. We use Switch statements for the condition based problem solving. Switch statements are used when we have multiple conditions in a single if statement. It is just a substitute for multiple if-else statements. In this a variable is compared to multiple cases one by one and when the case is matched then the code block under that case will be executed.
90111

91112
#### Semantics
92113

93-
```cpp
114+
```java
94115
// syntax
95116

96117
switch (expression) {
@@ -100,7 +121,6 @@ Switch statements are used to execute different code depending on the value of a
100121
case value2:
101122
// code to execute if expression is value2
102123
break;
103-
...
104124
default:
105125
// code to execute if expression is not value1 or value2
106126
break;

0 commit comments

Comments
 (0)