You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/io-conditionals.md
+30-12Lines changed: 30 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,15 +20,14 @@ Okay so let's learn about the control flow of a Java program.
20
20
21
21
Let's go through the code line by line.
22
22
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.
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 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 java runtime 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.
32
31
33
32
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.
34
33
@@ -80,10 +79,30 @@ Lets go through the if-else code line by line.
80
79
81
80
ok so that's all. now let's see some more examples.
82
81
83
-
```cpp
84
-
// code goes here
82
+
```java
83
+
//A Java Program to demonstrate the use of if-else statement.
84
+
//It is a program of odd and even number.
85
+
publicclassIfElseClass {
86
+
publicstaticvoidmain(String[] args) {
87
+
//defining a variable
88
+
int number=13;
89
+
//Check if the number is divisible by 2 or not
90
+
if(number%2==0){
91
+
System.out.println("even number");
92
+
}
93
+
else{
94
+
System.out.println("odd number");
95
+
}
96
+
}
97
+
}
85
98
```
86
99
100
+
Let's go through the code line by line.
101
+
102
+
-`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.
103
+
-`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.
104
+
105
+
87
106
## **Switch Statements**
88
107
89
108
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.
@@ -100,7 +119,6 @@ Switch statements are used to execute different code depending on the value of a
100
119
case value2:
101
120
// code to execute if expression is value2
102
121
break;
103
-
...
104
122
default:
105
123
// code to execute if expression is not value1 or value2
0 commit comments