From the course: Programming Foundations: Fundamentals

Conditionals across languages - Python Tutorial

From the course: Programming Foundations: Fundamentals

Conditionals across languages

- By now you're pretty familiar with conditionals in Python. We start with an if, followed by our condition, then we end the condition with a colon. Then on the next line we indent the block of code that we want executed. And the else is very similar, except there's no condition to check. In the Java programming language, the if else statement has a slightly different syntax. Remember, syntax is just the same thing as saying rules for how a programming language expects its code to be written. After the if keyword, there's a set of opening and closing parentheses. Inside these parentheses is where you put the condition you want to be tested. And instead of a colon after the condition, a pair of opening and closing curly braces are used to denote the code block for the if. The same thing is true for the else clause. Now although it's customary to indent the statements in the code blocks, it's not a requirement the way it is for Python. Let's look at one final example with the Ruby programming language. Ruby is known for its ease of use and flexibility. But just like the other languages, the structure of the if else statement is the same, but the syntax differs. With Ruby there are no parentheses or a colon to separate the condition. Even the else clause just stands on a line all alone. The one thing that's more noticeably different is the addition of the end keyword. This is how Ruby notes that it's done with this if else statement. Not based on indentation or a curly brace like Python and Java. The cool thing about learning the basics of programming is that once you grasp the structure of the core statements and principles, you can more readily recognize them across all languages.

Contents