There are a couple of other odds and ends
about the if statement that I want to briefly talk
about. So you know now that the if statement
makes a decision. You go one way or another, depending
on some condition. Now, sometimes, you're code is arranged
so that you don't actually have a decision to do
one thing or another, but the decision is between
one thing and nothing at all. For example, over here,
I've rearranged the code for computing the actual floor a
bit. I'm initializing actual flow with the floor and then
I only need to adjust it if the floor is
greater than 13 and otherwise I need do nothing at
all. In this case, it is perfectly legal to omit
the else part. That's like if you only choose between
doing something or just staying in place. It's okay to
do more than one thing. You could put other actions
inside here, maybe a print statement. Generally in more
complicated situations, there's going to be several statements in between the
braces. Conversely, if there is only one action here, it's
legal to omit the braces. But we won't ever do
that, it's just clear to always put them there.
Makes the code easier to read. Now go ahead and
work out a couple more practice problems with Sarah. And
then we'll talk more about how these conditions are formed.
Hay otro par de minucias
sobre la sentencia «if» de las que quiero hablar
un poco. Ahora ya sabes que la sentencia «if»
toma una decisión. Vas a proceder de un modo u otro dependiendo
de alguna condición. Ahora, a veces, tu código está dispuesto de
forma tal que en realidad no tienes que tomar un decisión
en un sentido u otro, sino que la decisión es entre
una cosa y nada en absoluto. Por ejemplo, aquí,
he reorganizado el código para calcular un poco el piso
real. Estoy inicializando actualFloor (pisoReal) con el piso y luego
sólo tengo que ajustarlo si el piso es
mayor de 13 y de otra manera no necesito hacer nada en
absoluto. En este caso, es perfectamente legal omitir
la parte «else». Eso es como si solo tuvieras que elegir entre
hacer algo o simplemente solo quedarte ahí. Está bien
hacer más de una cosa. Podrías poner otras acciones
aquí dentro, tal vez una sentencia «print». Por lo general, en situaciones
más complicadas, va a haber varias sentencias entre las
llaves. Por el contrario, si sólo hay una acción aquí, es
legal omitir las llaves. Pero no vamos a hacer eso
nunca, es sólo más claro ponerlos siempre ahí.
Hace que el código sea más fácil de leer. Ahora continúa y
resuelve otro par de problemas prácticos con Sara. Y
luego hablaremos más sobre cómo se forman estas condiciones.
Il y a quelques autres bricoles
à propos de l'instruction if
dont je veux parler brièvement.
Donc vous savez maintenant que
l'instruction if prend une décision.
Vous suivez un chemin ou un autre,
selon certaines conditions.
Maintenant, parfois,
votre code est arrangé
pour que vous n'ayez pas
à décider de faire une chose ou une autre,
mais la décision est entre
une chose et rien du tout.
Par exemple, ici,
j'ai réarrangé le code pour
calculer le sol.
J'initialise actualFloor avec floor
et ensuite je n'ai besoin de l'ajuster
que si le sol est supérieur à 13
et sinon je ne dois rien faire du tout.
Dans ce cas, on peut parfaitement
omettre la partie else.
C'est comme si vous choisissez
seulement entre
faire quelque chose
ou simplement rester en place.
On peut faire plusieurs choses à la fois.
Vous pouvez mettre
d'autres actions à ici,
peut-être une instruction d'impression.
Dans des situations plus compliquées,
il y aura plusieurs déclarations
entre les accolades.
Inversement, s'il n'y a qu'une action ici,
on peut omettre les accolades.
Mais nous ne ferons jamais cela,
c'est plus clair quand on
les met toujours là.
Cela rend le code plus facile à lire.
Allez-y et résolvez d'autres
problèmes de pratique avec Sara
et puis nous parlerons plus de la façon
dont ces conditions sont formées.
これからif文について
もう少し説明したいと思います
if文が条件分岐を行うことは
理解できましたね
ある条件によってどちらかに向かいます
たまにはコードを少し変更して
2種類のうちどちらかへ分岐するのではなく
1つのことを実行するかしないかの
条件分岐をしたい時があります
例えばここで実際の階数を計算するための
コードを再調整しています
私はfloorによってactualFloorを初期化します
階数が13より大きければ
調節しますが13以下は何もしません
この場合else節を省略しても
まったく問題ありません あなたが何かをするか
その場に留まるかを選択するのにも似ています
1つ以上のことをしても大丈夫です
他のアクションを
print文として入れることも可能です
より複雑な状況では
波括弧内にいくつかの条件文が入ります
反対に1つのアクションだけの場合
波括弧の省略は可能ですが私は省略しません
常に波括弧を使うことで指示が明確になるからです
より簡単にコードが読めます
次はサラと一緒に練習問題を解いてください
条件がどのように形成されるかを学びましょう
if문에 대해서 자질구레한
것들을 소개하고자
합니다. 이제 if문은 조건을
만든다는 것을 압니다. 조건에 따라 한 쪽이나
다른 쪽을 선택하게 됩니다. 가끔 여러분의 코드가
잘 정리가 되어 있어서 조건을 쓰지
않을 때도 있지만, 조건문의 의미는
모 아니면 도입니다. 예를 들어서,
실질적은 층을 구하는 프로그램을 제가 고쳐놨습니다.
actualFloor와 floor가 같다고 선언하면
floor가 13보다 클때만 조건을 걸면
다른 거 할 필요가 없습니다. else문을
꼭 쓸 필요는 없습니다.
else문을 꼭 쓸 필요가 없다는 건, 둘중 하나를
꼭 선택한다는 뜻입니다. 하나보다
더 많은 것을 해도 좋습니다. 여기 안에 다른 기능을
넣거나 결과를 출력할 수도 있겠죠. 일반적으로,
더 복잡한 상황에서는 중괄호 안에 더 많은 코드가
있을 겁니다. 역으로, 단 한가지의 코드가 있다면, 중괄호를
없애도 됩니다. 하지만 그렇게 하지는 않습니다. 왜냐하면
중괄호가 있어야 이해하기가 편하기 때문입니다.
코드가 읽기 쉬워진다는 뜻입니다. 이제 Sarah와
함께 if문에 대한 문제들을 풀어 봅시다. 그런 후에
조건이 어떻게 형성됬는지 알아보겠습니다.
Có hai kiểu viết câu lệnh if khác nhau
mà tôi muốn nói ngắn gọn cho bạn.
Bạn biết là, lệnh if để tạo một quyết định.
Bạn đi đường này hay đường kia, phụ thuộc vào điều kiện nào đó.
Bây giờ, thỉnh thoảng, code của bạn đã được sửa lại
để bạn không phải quyết định làm 1 việc này
hay việc kia nữa, mà là sự quyết định
giữa làm việc này và không làm gì cả. Ví dụ, ở đây,
tôi đã sửa lại code để tính số tầng thực sự một ít.
Tôi khởi tạo số tầng thực sự bằng số tầng,
và tôi chỉ phải sửa nó nếu số tầng lớn hơn 13,
nếu không thì tôi chả phải làm gì cả.
Trong trường hợp này, vẫn rất hợp lý khi bỏ phần else đi.
Đó kiểu như là bạn chỉ phải lựa chọn
giữa làm việc gì đó, hoặc là đứng yên, ko làm gì.
Nó cũng ổn để làm hơn 1 thứ. Bạn có thể thêm hành động khác
vào trong này, có thể là một lệnh in. Thường thì,
trong vài trường hợp phức tạp hơn, sẽ có rất nhiều lệnh ở trong này.
Ngược lại, nếu chỉ có 1 hành động ở đây,
bỏ dấu ngoặc { } đi cũng không sao. Nhưng ta đừng nên làm như vậy,
luôn cho nó vào để cho nó rõ ràng.
Làm cho code dễ đọc hơn. Bây giờ, tiếp tục,
và làm thêm vài ví dụ nữa với Sarah.
Và sau đó, ta sẽ nói thêm về hình thức của các điều kiện này.