Game Basics Control Statements
GCC Academy
Hello everyone
This is Lee Young-hoon for C#-based object-oriented programming course
This section covers commonly used conditional statements
and loop statements
Using the if statement, we will learn about branching based on values
and conditional branching
as well as switch statements and
branching based on values
We willl also learn how to do repetitive processing
using for statements and while statements
and ending loop or
skipping certain tasks with jump statement
if statement, switch statement
Control statements are used to control
the flow of a program
Important control statements include conditional statements and loop statements
In conditional statements, there are things like if statements and switch statements
And there are loops such as for loops and while loops
Let's learn them one by one
First, let's learn about if statements
To do this
Shall we create a simple project first?
Open solution
Right click here and add
Let me add one more project
Likewise, the console app
Click on the part that says (.NET Framework)
Since this is a control statement, control statements are usually in English
Control Statement
So I'll write it like this
Let's write ControlStatement
Let's create a project like this first
Then let's try the if statement first
Let me explain it briefly
if statement is written like this
It says if and then there are parentheses here
Then the curly brackets go in like this
Curly brackets are what I called scope last time
This is a domain, but the term scope is often used
So there is something called if and there is a condition in it
Here is a condition and if this condition is true
I said that true is true in English
If true, the contents inside will be executed
If it is false, it will not do anything and will immediately end this scope
And the if statement can also be written as a single statement like this
This is a condition, like yes or no
You can do it dichotomously too
That means, write else here
Also, if you make the scope like this
If this is true, the contents of this side will be executed
If this is false, the else statement is executed
You can make it like this in two ways
In this way, there are not two conditions
It branches out into three or four
or more
In that case, write it like this
If
Then say else if
Finally else
You can also write it like this
This is also a set, so if this is true
Here is the execution and it ends
If it is false, then come back to else if
Check the conditions
If this is true, then it's doing this
Otherwise, go to the else statement and do the work here
It's structured in such a way that it ends the whole thing
So, let's try coding this ourselves
Let's try it with an example
Let me make an example
of getting an age input and outputting it
So, let me write it down as a comment
Get age input
If you are 20 years old
You are 20 years old
Such a sentence will be output
in this example
We have done output first so far, but input is also possible
How to use input, in the console
let's try it with ReadLine. ReadLine
This basically returns a string
So it's returning a text
That's why it's called a string
Since it was input, let's call it inputString
If you do this, something will come in
If you press enter, the value will be inserted here
Let's output this first
Console, WriteLine
Auto-completion looks like this Let's run it
Just hold down Ctrl and press F5, like this
When I do this, the cursor blinks like this
If you type 20 here and press enter
You can see that 20 is output
So it's like this here
So you want to write it like this here
Your age is, you are 20 years old
I'll try outputting it like this
Like this, then
You need to check if it was entered correctly
So I'm going to check if this is 20
A simple way to check is if
Since we're learning about conditional statements, I'm going to try using a conditional statement
Say if and then open parentheses like this
Then, the conditional expression comes in here
Then, create an area here with curly brackets like this
I'm going to put this content in this area like this
Then, if this condition is true, this will be output
And if this condition is wrong
You can make it like this by saying else
Create an else statement 'it is not applicable'
This will be output
Then, inputString must have the value 20
If we compare with strings
You can make a comparison like this
So let's run like this
If you do this and press 20 enter like this
You are 20 years old
You can see the output like this
And then run it again
Let's try outputting a value other than 20
21, This does not apply
So you can see that it is output like this
If you write a conditional statement like this, it becomes a set
So, you can use it like this as a single word
And in this complex way
You can also add one more
If we add one else if like this
You can do something like this by adding more conditions
I'll try it here like 21
21
21, check, and compare
You are 21 years old It's also output like this
I'll put that here
ctrl+F5, and it works well
So if we say 21 like this
Look, this is what it says, you are 21 years old
If you look here == it's written like this
This is called a comparison operator
There are things called comparison operators and logical operators
The comparison operator is ==, this is equal
So, if A and B are equal, then it is true
And this is called not
not equal
So if A and B are not equal
If it's different, it's true
This is an inequality, so if A is greater than B
If less than, greater than or equal to, less than or equal to is true
There are operators like this
These operators are called comparison operators
or also called relational operators
And the one below are logical operators
It's a bit like a set concept
So if both condition A and condition B are true
it's true with this operator
This is an ampersand
This is called and, this is and
And you see on the keyboard
There is a won sign above the Enter key
Above the won sign
there is a stick like symbol, and it's called the or operator
So if we use the or operator like this A || B,
If either A or B is true
It's true if either one of them is true
And this is called not
It's a concept that turns things around
If A is true, then it is false
True when A is false
Such operators are called logical operators
Then let's try or first
20 years old or
Or 21 years old
Let's change the content like this
Should I give a scope like this this time?
So now if we compare this to a string
It's a bit difficult to deal with the scope
So let me convert this into numbers
It was given the number 20
Because this is now processed as a string
You can convert a string to a number
So this to an int type, an integer type
Create a variable called age
Let's make the input String into an integer variable
If you write int and press the dot here
Any function that int has
Things like these properties come out
There's something called Parse here
Parse, it's written here
The representation of a character as a corresponding signed integer
It's a function that converts it, so if you look at it
If you write int.Parse and put a character here,
If this is a number, it is automatically converted to an integer variable
It's a feature that automatically does that
So I'm going to write it like this
Then the value of age is 20 or something like 21
The values ​​entered like this will be stored
Then, change it like this
Then don't write like this
I want to write the age range as I am in my 20s
You are in your 20s
If we express the 20s as the age from 20 to 29,
You can write it like this: greater than or equal to 20
Then, the value called age, the value contained within age
is greater than or equal to 20, this is a bit wrong
I think this should be &&
Sorry, let me do and first
The value of age is greater than or equal to 20 and
it should be less than 30
Or you could say 29 or less
Let's try and first, this is and, so it's an intersection
So the value in age is greater than or equal to 20
And if it is less than or equal to 29 That's what this means
So this is now between 20 and 29, right?
Only the range within it is now applicable to this side
Otherwise, it will go to the not applicable side
Let me test it
Do this and press ctrl+F5
Let's try entering the age
If you enter 20 years old, it will naturally say you are in your 20s
25 is still 20s, right?
But if you're 19, then this doesn't apply
This is how you can handle branching
Then I'll try or too
else if, the range is 20s like this
If age is 30, or if age is 31
Then you are
You are 30 or 31 years old
You could express it like this
But usually when we make something like this,
I know this so I type it right away
but if I am to make this and give it to someone
Actually, I don't think it's right to do it like this
So first of all, enter your age here
I think it would be better to guide it in this way
Here
Do it like this
WriteLine means after this sentence
the cursor will move down to the next line
I want to keep the cursor here
I will write it like this
If you write Write, there is no line break
If you run it like this, it will look like this
Here the cursor will blink over here
After I make it like this, I'll put 31 in like this
Then it's over here
Because it doesn't apply here
Because the if statement at the very beginning is false
Then, enter the else if statement
I'm checking here
So if your age is 30 or 31, this applies to you
or is a union, right?
This one or this one, right?
If your age is 30 or 31
This means that it meets this condition, it is true
so this sentence is output
By using comparison operators like this
You can check various things like this
We can create branch statement and use it
for various processing
You can use the if statement by writing it like this
This time, let's try a switch case statement
Let's comment with ctrl+k+c
Comment out the block
Here's the second note
Input from 0 to 2
Let's try an example of outputtig that
This time, I'm going to try using something called a switch case statement
First, we need to get input
Likewise, let's write this as is
I'll write here, "Enter 0 to 2"
Likewise, this can be written as is
I will read one line as inputString
And this time, it is an int integer variable
Let's create a variable called number
I'm going to fill this variable with this value
We tried like this earlier, right?
I used a function called Parse
But there is one more type of function called Parse
So I'll try it another way
It's int
Like this, if you look at int, if you look at Parse
There's also a guy called TryParse
There are two: Parse and TryParse
What this does is it handles exceptions
What that means is, for example, in this string
We want the numbers to be filled in
But in reality, it's not a number
You can also enter English, alphabets
In that case, we're going to have a problem
It's not a number like this
So, if you want to do parsing like this
The word parsing simply means this
Converting any data into the format I want
Those things are usually called Parsing
When you convert it to the desired format
If the format of the data is different, there will be problems
So to prevent things like that
We use the function called TryParse
Here's how to use it. It's written exactly as you can see here
What this means is
If you look here, you can see that two of them are inserted like this
This is a function, and we will learn about functions later
I'll tell you more details then
So, first of all, you can think of it as a feature
So now when we use this guy
I need to enter two pieces of information
If you look at the first information
If you press ctrl+shift+space like this
You can see this content again
Good
Put it in parentheses here
If you hold down ctrl+shift and press the space bar at the same time
You can see the format of this function like this
It's called the declaration, you can see this
So, if you look, there are two pieces of information here
One is a string type
One is int type
So, for the string, put this in, what we received
Then we create a variable that will hold the information about this guy
The result of it This keyword called out
You can think of it as a function needed to return it
Let's go into more detail when we do functions
Usually, functions are with this =
This is how you receive the return value
But this Parse function returns the result like this
I receive it just like this
In the case of TryParse, the return value is bool
The bool type is logical, it comes with true and false
So, I'm converting this
When you want to know whether it was a success or a failure
You can use this function called TryParse
So we bool this and shall we call it success?
If you want to know if it was a success, you can write it like this
So if it was successful
I'll put a condition, say if, and if it succeeds
Like this
Additionally, a value is stored in the number
Right? And you can do something with this number
So I'll wrap it in an if statement like this
Then let's try outputting it first and see if it comes out well
Here, I did the output with Console earlier
With console
Now let's output the number
If you run it
Oh, wait a minute With an else
Let's see if it failed as well
Number conversion failed, I'll write it like this
And the actual number value is
Let me try outputting it as is
If you do this and see the output,
Of course, if you put in numbers, it's like this
The number is 5, so it comes out like this
I'll do this with WriteLine
But if you run it and use an alphabet like this
and press enter, it says conversion failed. It looks like this
This should not number
I need to output inputString
So if you want to see what the actual data is like this
You can output it like this
If you do this, you can see that the conversion has failed
This way, we can now receive only numbers
Then, add a switch case statement here
Let's try branching this once
Write it as switch
Then open the parentheses like this
Then here we have the actual values
Since we are going to branch, we will enter a reference value
Here, this guy called number will be the standard
Enter the number
Then write it like this in parentheses
Switch is written with case
We write case and here
The expected value would be 0
And then after writing 0, at the end, this is called a colon
This is called a colon
This is a semicolon
Can you see it well here?
: If you write it like this, it is a colon
Then like this; but below here
With a comma-shaped thing is called a semicolon
This is what we call it, Colon
Please write a colon like this and then write break
Write it like this
Then we usually do case by case
That's what we say, right?
So when there is a situation
we say it's case by case
Switch too
There can be multiple cases
There is a case where the number is 0
We have 0~2, so we should have 3
So, put in 3 things like this: 0, 1, 2
This one is 1, this one is 2, you could express it like this
If you look closely, you can see that it is a little different from the if statement
For example, if you do this with an if statement, it will be like this
if (number == 0) then do blah blah
Then else if (number == 1)
Something like this and that
And else if (number == 2)
You write it like this
We might accidentally write it like this
The conditions can be entered the same like this
If you do this, the if statement won't throw an error
No error occurs
And in fact this sentence will never come in
Because if it is already true above
it'll go here
Right? If this is false, then this is also false
This one won't come in either, right?
You can make mistakes like this, so
This condition
The value that serves as a basis for creating this conditional expression is
If you use different numbers
standard to divide the criteria like this
If you do it like this with an if statement, you may make some mistakes
So in this case, it is better to use a switch case statement
It's a bit more desirable
Because in the case of a switch case statement,
If these numbers are the same, an error occurs
Iit is impossible to have the same value'
Because it gives an error,
When you divide the value exactly like this, it is better
A switch case statement may be preferable
And in the switch case statement too
There's else statement that we used in the if statement, right?
If it's neither this nor that, right?
If you look here, 'not applicable'
means it doesn't apply here and it doesn't apply here either
It's a neither-or-none situation, right?
So, a situation like that,
That's possible in this switch statement too
Write default here
If you write it like this
what happens then is that this can now be called a jump door
If the value contained here is 0
Here, it goes straight to case 0
Then, if you do something here
After doing this, you meet break, right?
What do you do when you meet a break?
This will escape the switch statement
So that's how it ends, right?
For example, number 1 came in
Then it will go straight here
Then it does something here
and ends after meeting break
This is how it works
So, I'll output it here like this first
Whether the number you entered is 0, 1, or 2
Let me try outputting it
It's called Console, and I'll write down what's here quickly
Let me do that real quick
The number you entered is 0
Write it like this
This would be 1, this would be 2
I don't know what's next here
The number you entered is here
Let's output this number itself
Number is number
But this is now an exception, so I'll put it here
I'll try to express it a bit by using the exclamation mark like this
If you press ctrl+F5,
It asks me to enter a number
Then add 0 here
You can see that number 0 is output properly
Let me try something else, should I press 1?
Then comes here and prints this out
The number you entered is 1
Then if it is not within the range
If we try to write something that is not in the case we wrote
If you put 55 like this
You can see it expressed like this with exclamation marks
towards default
Like this, if statements and switch case statements
are what we've learned so far
This is a very commonly used phrase
Please remember it well
and practice a lot as well
Actually, there are a few more things you can use as conditional statements
There are things like ternary operators and such
Now that we're on the subject, let's try that out for a moment
Let's say there's one more thing, the ternary operator
This is very simple
For example, let's say we have an int number
Let's just input this too
If here, in number
Let's say there is a value of 10
And
Let's say bool type, int?
If there is any number
Depending on the value contained in this number
Let's say there's a part where you want to process some results
For example, if number is even, let's do this
0 if number is even
And if it is odd, it takes the value 1
Let's say I want to create some variables
I'll call this result
The result is
Usually, when expressing even numbers, you can do it like this
Among the operators, there is also something called the remainder operator
There are so many operators
So among the operators, %
There is something called the remainder operator
So what is this?
This is our division operator
Divide, 2 like this
Then, of course, 10/2 will return 5
If you write it like this
This will have the value 5
Let's try verifying it
Let me output it with console
5 is output like this
But this is 11
Then what's the output?
Again, 5 is output
Because this is an integer
it doesn't represent decimal points
Originally, it would be expressed as 5.5, not 5
If this is float
I see, that's because it's number
Because both are integers
2.0 Let's express it like this
Then like this 5.5
You can see that the decimal point is expressed like this
When calculating like this, if you calculate integers and integers,
an integer comes out
Even if the one receiving this is set to float
it can't express decimal points
So either one of them must be a real number
You need to make it a float type
for it to be stored like this
Last time, we talked about it during data types and variables
But if you want to know the remainder of this
Then, you write it like this
When 11 is divided by 2
The remainder is 1
2*5=10, there is a remainder of 1
So if you run it
You can see that 1 is expressed like this
And
if this is 10, then 0 will be expressed
0 will be output like this
There are some remainder operators like this
With this, if it's even, 0, if it's odd, 1
I want to express it like this, but actually this
you can just output this as is
Then let me try it in reverse
1, 0
You might want to do this
Then you can do it like this
? like this
Then if the result is 0
1:0 You can express it like this
This is called a ternary operator
If you look, ==, based on this
the left and right respectively will be compared
If you do number %2
Since this is 10 now, it's an even number so it will be 0
Then, since 0 and 0 are the same number, they are equal, right?
If they are the same, return the one after the question mark
If this is different, if it's false
The :, colon following this question mark
It returns what is immediately after the colon
These are called ternary operators
This is a ternary operator
Like this ? and :
So there are three terms
One, two, three
It is called a ternary operator because it has three terms
So there are things like this too
when processing conditions
So I'll keep it simple and move on
for Loop, while Loop, Jump Loop, continue and break
Let's try a loop
Repeat statements are usually for statements and while statements
These two are used a lot
There is also do while statement
but it you know while statement, do while statement is pretty simple
So there are two things we're trying to learn
Let's start with for statement
What does a for statement look like?
It's written like this
for
Open the parentheses like this
Next, there are three items that go into this
Put a semicolon here like this
Write two of them like this
Because there are two semicolons
Here are 1, 2, 3
You can put something like this
Then this also has its own area
So this has a scope
Open and close curly brackets
This is what a for statement looks like
Here, I'll express this as number 4
Tasks will go here
So what goes in the first?
This is where initialization comes in
Initial value
You can also declare some variables here
So that's the part that initializes that guy's value
Then what's in the second position?
It's a conditional expression
So if this condition is true, we continue doing this task
If it becomes false, this conditional statement will be escaped
No, not the conditional statement, escape from this loop
It's going to stop
And the third one is the increase and decrease equation
It has this structure
So if you look at this in the order of work
it starts with number 1
Then we check the second, condition expression
Then if this is true
The fourth, the task is done
Then after the fourth one is finished, do the third, increase and decrease expression
Then after the third one is finished, we check the condition again
So the flow is like this
The first one is just one time
From then on, it repeats 2, 4, 3, then 2, 4, 3 again
Here, if the condition is true
it will go here
If true, and if false
It's false
Then escape, it will end
When I say end, I mean end of this for statement
It is structured like this
It's a form that repeats itself
Okay, let's try coding this
A for statement
Let's start from 0 or 1 for simplicity
Let's try an example that prints numbers from 1 to 10
So, first of all, I said that we can put two semicolons here like this
And then we have this area
Then, we can put the initial value in the very first part
I created an integer variable called int i
The default value will be set to 0
Then the second one is until i is less than 10
Then it would be from 0 to 10
I said earlier that I would output numbers from 1 to 10
I think it would be right to do it this way
From 1 to less than or equal to 10
Right? Then it would be from 1 to 10
And you have to increase it one by one to reach 10
If i contains 1, but for i continue to increase
Here, we need to increase i by one in the increment and decrement formula
Then adding i means
It can be expressed like this: i = i + 1
But you can use this a little more conveniently
What this is is
Let's call this int a
If I put 0 in a
Then, increasing a by one means
a = a + 1
But this can be shortened
a++, You can also abbreviate it like this
So, for example, something like this
If you call this professor
This is similar to saying, "prof," in short
So, this ++, this ++ written like this
It's called an increment/decrement operator
There are two types of increment and decrement operators
This is the postfix
There is also a prefix
This can also be written as ++a
What this means is
In the case of a prefix, this is the timing that is added
For example, let me try this
Here
Let's say we do it like this
If I put this in int b
like this
Let me output this
Like this, with console
This is postfix
A prefix is what comes in front
If this is on this side, it's a postfix
I can't use them all at the same time
You need to use one at a time
If you put it in the back and press Ctrl+F5
If you look at it, you can clearly see that a has been increased by one
I thought I had it, but I can see that it outputs 0
Right? What this mean is, when you use a postfix
This semicolon, this one line, after it is executed
When moving down to the next line, a increases to 1
So, the order of the assignment operator = is
First, put a into b, and then increase a
So it contains 0, because a originally contained 0
This is one of the increment/decrement operators that is in the postfix form
What happens if you change this to a prefix?
First, increase a by 1
Then, the value of a is stored in b using the assignment operator =
Then, of course, 1 will be contained in b
If you run it, you can see that it contains 1
So when you use this increment/decrement operator
Depending on whether you write this on the left or right side of ++
There are some differences, so I think it would be good to remember them well
So it's like this, it can be written like this
but it can also be written like this
Of course, it's alone here now
so it doesn't matter if you just write it like this
But usually, almost like a habit
It is often written like this
I'll output it
If you output Console.WriteLine(i)
You can see that it outputs out numbers from 1 to 10
Here, the for statement meets this conditional expression
If it's true, I'll keep doing this
Then do this, increase it, and compare again
Do this, increase it, and compare it again
It keeps repeating this
So the moment this becomes false, it stop doing the work
It escapes the for loop right away
But usually, when you use something like a for loop
Things that are used together a lot, such as arrays
There are several, for example these args
When we first did this, we put some values ​​here
and you can output that too
For example, if I want to output this,
You can also write it like this here, writing for
The array starts from 0
This is called a subscript, an index
So, I write it like this, like this
This is how you write it
When writing a for loop, you can do it like this
int i=0; i< args.Length;
You can express it like this
Then output the ith value of args
You can mix it like this
Loops are often used with arrays like this
Now, we search by increasing the array's subscript number by one
It is used a lot because these things can be done
What is an array? As I explain, things keep coming up
An array is a variable that contains some data
It is a variable, but it is a variable contained in a continuous space
Yeah, that's why it's like this
Just briefly remember what it is
I'll try one of these
I want to output only even numbers. I can do something like this
There are many cases where formulas are created and processed like this
So let's try outputting only even numbers
If you do this, you can do this
If i is even
But earlier, when we were doing the ternary operator
To handle an even number, we did this
I did it using the remainder operator, the % operator
You can do the same thing, if you do i % 2
If the result is 0, it is even, and if it is 1, it is odd
Right? Using this principle
I want to multiply by 3, also in that case
You can just divide it by 3
So if it's 0 when divided by 3, it's a multiple of 3
You can do it like this
I want to output only even numbers
I'll try writing it like this
And actually, if you don't put use curly brackets
What happens if you don't use curly brackets? If you do this with an if statement
If you see a semicolon like this after parentheses like this,
This sentence ends
So, if you want to omit the curly brackets in the if statement
If you want to omit the curly brackets like this,
You can just write it like this
This is a coding style
Side effects? Those problems can arise
For example, if you accidentally write something like this
Then, as I mentioned earlier, this if statement
It ends when you meet this semicolon
Then if we write like this, the Console.WriteLine(i) part
You might think it belongs inside an if statement, but it doesn't
So you might make a lot of mistakes
So, if possible, use curly brackets like this
By creating a scope like this, even one line
I recommend writing it like this
I'll try outputting only even numbers like this
Then you can see that only even numbers are output
Then, let's try writing this using a while loop
The for loop is written like this, but how do you write a while loop?
It's simpler, type while
Then, open the parentheses here like this
Then create a scope
You can write it like this
If this condition is true, continue doing this
I'm going to keep doing this until it turns out to be false
It's much simpler
So if you do it like this above
Let's create something called int j
Set it to 0 and j
Should I do the same as above?
You can configure it like this from 1 to 10
And here we increase j by one
In a way, the principle is exactly the same
But a while loop is a bit more dangerous than for loop
There is also a chance that it will fall into an infinite loop
For example, this is usually written like this
but it's also written like this a lot
Set this to true
Here, with an if statement, for example if j does something
Stop repeating this and stop now
There are many configurations like this as well
If this condition is not met the while loop
could keep spinning infinitely
So, you have to be a little careful when writing while loops
First, let's try writing it the same way
Let's output only the even numbers like this. In the same way
I guess you could write it like this
Let me just leave this as a comment for now
You can see that only even numbers are output
If you look at it, it's not that difficult, right?
And besides while, there is also something called do while
Put do {}
Then at the end, write while(); like this:
And here the conditions go like this
You can do it like this with a semicolon
It's like this, and if you write it exactly the same way, it will be like this
It's like this, and this content
will go in here
Actually, the difference between while and do while is
while loop, if a condition
If it doesn't become true even once
If this isn't true when you first start out
it doesn't do all this at all
And it ends right away
But do while does this once, then
Then checks this
And if that's true, then we go back up and do this
So do while unconditionally does what is here once
There is a difference that it then checks the conditions
So, these are things that you need when you are coding
There are times when you want to handle it like this
In that case, you can use the do while loop
Usually, just a while loop is enough
Most of the writing is possible
So I'll just keep this simple and move on
Now finally, there is something called a jump statement
Then, with the existing for loop
I'll try making a jump statement at last
What is a jump statement?
It determines whether
it will continue the repetition
or it will go straight to the increment/decrement expression
without doing the {} task
Or if there were some conditions here
End this loop right away without going up to the conditional expression
You can also process it like this
So let's first look at the concept of skipping
Actually, there are two keywords
One is continue
The other one is called break
Earlier, we used break briefly when doing a switch case statement
The guy called break is
ending the sentence right away
It means to brake, to stop
And continue is the concept of continuing
So if you want to output only even numbers
You can also write it like this
Write this outside the if statement like this
Then, I want to do only even numbers in the conditional expression
So, if it's an odd number,
This in other words, if it's an odd number,
I want to skip
You might want to do this
So, to express this as an odd number, you do it like this
If the remainder when this value i is divided by 2 is 1, then it is an odd number
In that case, write continue here
So, what happens when you encounter continue?
At first, 1 is put into i
Then we ask if i is less than 10, which is true
Then it comes down, and it's 1
Then it will be caught here, it will be caught in the conditional statement and it will be true
Right? Because 1 divided by 2 has a remainder of 1
Then you will come across continue
When you meet continue, you don't come down here
From here, it goes straight to the increment/decremet expression
So we do this and increase by one
Check the conditions again and come down
Then, since i increased by 1, it would have changed from 1 to 2
If the value is then 2, divided by 2, the remainder is 0
So it doesn't get caught here
Then this is output
It goes round and round like this and ends when it meets the conditions here
We can work with that syntax by changing it
Let's run it again
As you can see, only even numbers are output
The existing method, the existing method was done like this, with this syntax
This is outputting it only when it is 0
The difference in the way continue is used is
Actually there is no big difference
But when we code, readability improves a lot
What I mean is, in fact, this conditional statement
If you do it this way to get in, right?
When i is 5, you may not want to do this at all
I don't want to do if (i==5)
If you want to do it this way, you don't actually have to use continue like this
If you coded it like this, you should do it like this
&& i != 5, You have to write it like this
And so it becomes an intersection
It's like this: output only when i is even and i is not 5
If you write down the conditions in this way
This is where it gets really complicated and later on when we read it
When I read the code, I go, what does this mean?
There are some drawbacks that require a lot of looking into
So these things
If you use something like continue, it becomes very convenient
This is how you write it
then
First, it filters out all the continue above
When i is not 5, no, when i is 5
When i is 5, skip it
Then skip the odd numbers of i
And just output the rest
Right? Then below, I actually
Just do the work you want to do
You can filter everything from above
Right? So if you write it like this, it would be a bit easier
This makes it easier to read the code
And finally, there's this guy called break
I want to break, randomly, I'll say
If it's 9, stop
I want to perform a command like that
If i is 9, I want to stop the loop
You can do it like this
So here you can write like this
if (i == 9) { break; }
You can write it like this
Let's give it a try
Then, originally, it was output up to 10
Because there is a break at 9, there is no more repetition
It can't reach 10
So it can't output 10, right?
Because the moment you meet 9, you get out
Originally it went from 9 to 10
and output up to 10
Then this loop ended
Because I ended it with break right away when i was 9
So it can't output 10
Now, let's summarize what we learned in this unit
if statement, switch statement Control statement Used to control the flow of the program
Conditional statement if statement If the condition is true, execute {}; If false, terminate {}
Switch statement Allows multiple cases Errors occur with duplicate values, so it’s used for exact value branching
Ternary operator An operator with three terms A structure that outputs 1 if number is even, 0 if number is odd int number=10; float a=number%2==0?1:0;
If the values ​​are equal, return the value following the question mark and if they are different, return the value following the question mark colon
Comparison Operators and Logical Operators Comparison Operators A==B True if A and B are equal A!=B True if A and B are different
True if A>BA is greater than B True if A<BA is less than B True if A>=BA is greater than or equal to B True if A<=BA is less than or equal to B
True if both logical operators A&&BA and B are true, otherwise false
True if either A||BA,B is true, otherwise false False if AA is true, true if false
for loop, while loop, jump statement continue and break Iteration statement for loop If the condition is true, {} is repeated, and if false, the loop is terminated
Structure: for(initial value; condition; increment/decrement expression) {to-do}
while loop Repeats {} until the condition is false Be careful because there is a chance of falling into an infinite loop
Depending on the situation, a do while statement is sometimes used, which checks the if statement after performing {} once
Jump statement Determines whether to continue the loop or send it in incremental order without {} break: Used to arbitrarily end the loop
Continue with the continue statement. When continue is encountered, it immediately moves to the increment expression of the for loop.