C Can You Continue in an Else Statement
Suppose a person wants code to execute for the values as per the code is designed to be executed but forcefully the same user wants to skip out the execution for which code should have been executed as designed above but will not as per the demand of the user. In simpler words, it is a decision-making problem as per the demand of the user.
Real-Life Example:
Consider a man is climbing up to go to his house in between there are 11 stairs. Being in hurry to climb up he directly stepped onto 3 staircases and then 4, 5, 6, 7, 8, 9 and jumps to last one. During this he missed out staircase 1st, 2nd and 10th and he completed the goal to reach his house. He continued his journey skipping staircase of. his choices.
In computers, it interprets staircases which is/are supposed to be skipped as 'continue'. The action to miss out execution which are supposed to be executed, is interpreted as continue statement be it any programming language.
Continue statement is often used inside in programming languages inside loops control structures. Inside the loop, when a continue statement is encountered the control directly jumps to the beginning of the loop for the next iteration instead of executing the statements of the current iteration. The continue statement is used when we want to skip a particular condition and continue the rest execution. Java continue statement is used for all type of loops but it is generally used in for, while, and do-while loops.
- In the case of for loop, the continue keyword force control to jump immediately to the update statement.
- Whereas in the case of a while loop or do-while loop, control immediately jumps to the Boolean expression.
Syntax: continue keyword along with a semicolon
continue;
Flow Chart of Continue Statement
The above flowchart is most important for the understanding of this keyword. Always remember the condition is always placed inside diamond boxes and statements in rectangular boxes. Now jumping onto the implementation part
Case 1: Continue statement inside for loop
In this program, illustration for how to use the continue statement within For loop. When the value of 'i' becomes 10 or 12, the continue statement plays its role and skip their execution but for other values of' 'i' the loop will run smoothly.
Java
import
java.util.*;
public
class
GFG {
public
static
void
main(String args[])
{
for
(
int
i =
0
; i <=
15
; i++) {
if
(i ==
10
|| i ==
12
) {
continue
;
}
System.out.print(i +
" "
);
}
}
}
Output :
0 1 2 3 4 5 6 7 8 9 11 13 14 15
Time Complexity: O(1)
Auxiliary Space : O(1)
Case 2: Continue statement inside while loop
In the below program, we give example, how to use the continue statement within the While loop. When the value of count becomes 7 or 15, the continue statement plays its role and skip their execution but for other values of the count, the loop will run smoothly.
Java
public
class
GFG {
public
static
void
main(String args[])
{
int
count =
20
;
while
(count >=
0
) {
if
(count ==
7
|| count ==
15
) {
count--;
continue
;
}
System.out.print(count +
" "
);
count--;
}
}
}
Output:
20 19 18 17 16 14 13 12 11 10 9 8 6 5 4 3 2 1 0
Time Complexity: O(1)
Auxiliary Space : O(1)
Case 3: Continue statement inside do while loop
In the below program, we give example, of how to use the continue statement within the do-While loop. When the value of I becomes 4 or 18, the continue statement plays its role and skips its execution but for other values of I, the loop will run smoothly.
Java
import
java.util.*;
public
class
GFG {
public
static
void
main(String[] args)
{
int
i =
0
;
do
{
if
(i ==
4
|| i ==
18
) {
i +=
2
;
continue
;
}
System.out.println(i);
i +=
2
;
}
while
(i <=
35
);
}
}
Output:
0 2 6 8 10 12 14 16 20 22 24 26 28 30 32 34
Time Complexity: O(1)
Auxiliary Space : O(1)
Case 4: Continue statement inside Inner loop(Nested Loop)
In the below program, we give example, how to use the continue statement within Nested loops. When the value of i becomes 3 and j become 2, the continue statement plays its role and skip their execution but for other values of i and j, the loop will run smoothly.
Java
import
java.util.*;
public
class
GFG {
public
static
void
main(String[] args)
{
for
(
int
i =
1
; i <=
4
; i++) {
for
(
int
j =
1
; j <=
3
; j++) {
if
(i ==
3
&& j ==
2
) {
continue
;
}
System.out.println(i +
" * "
+ j);
}
}
}
}
Output:
1 * 1 1 * 2 1 * 3 2 * 1 2 * 2 2 * 3 3 * 1 3 * 3 4 * 1 4 * 2 4 * 3
Time Complexity: O(1)
Auxiliary Space : O(1)
Source: https://www.geeksforgeeks.org/continue-statement-in-java/
0 Response to "C Can You Continue in an Else Statement"
Post a Comment