Table of Contents
CPP if keyword
Return to if, C++ Reserved words, C++, Reserved Words, CPP Glossary, CPP Topics
This comparison delves into the `if` keyword in C++ and its equivalents in Python, Java, C#, Kotlin, JavaScript, TypeScript, PHP, Go, Rust, Swift, Transact-SQL, and PL/SQL. The `if` statement is a fundamental control structure in programming languages, used for conditional execution based on the evaluation of an expression.
- C++
In C++, the `if` statement is used to execute a block of code conditionally. It can be followed by an optional `else` clause.
```cpp if (condition) {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
C++ documentation: s://en.cppreference.com/w/cpp/language/if(https://en.cppreference.com/w/cpp/language/if)
- Python
Python's `if` statement is similar to C++ but relies on indentation to define the scope of the blocks.
```python if condition:
# code to be executed if condition is trueelse:
# code to be executed if condition is false```
Python documentation: s://docs.python.org/3/tutorial/controlflow.html#if-statements(https://docs.python.org/3/tutorial/controlflow.html#if-statements)
- Java
Java uses the `if` statement in a manner similar to C++, including the syntax and the optional `else` clause.
```java if (condition) {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
Java documentation: s://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html(https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html)
- C#
C#'s `if` statement syntax and usage are identical to those in C++ and Java.
```csharp if (condition) {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
C# documentation: s://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else(https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else)
- Kotlin
Kotlin's `if` statement can also be used as an expression, returning a value.
```kotlin val result = if (condition) {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
Kotlin documentation: s://kotlinlang.org/docs/control-flow.html#if-expression(https://kotlinlang.org/docs/control-flow.html#if-expression)
- JavaScript
JavaScript's `if` statement is used in the same way as in other C-like languages, including an optional `else` clause.
```javascript if (condition) {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
JavaScript documentation: s://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else)
- TypeScript
TypeScript, being a superset of JavaScript, uses the `if` statement in the same manner.
```typescript if (condition) {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
TypeScript documentation: s://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types(https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#union-types) (While not directly about `if`, this discusses using types in conditions, relevant to TypeScript's enhancements over JavaScript.)
- PHP
PHP's `if` statement syntax and functionality are similar to those in C++, Java, and C#.
```php if ($condition) {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
PHP documentation: s://www.php.net/manual/en/control-structures.if.php(https://www.php.net/manual/en/control-structures.if.php)
- Go
Go's `if` statement includes an optional statement to execute before the condition; parentheses are not used around the condition.
```go if statement; condition {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
Go documentation: s://golang.org/doc/effective_go#if(https://golang.org/doc/effective_go#if)
- Rust
Rust's `if` statement can also be used as an expression, similar to Kotlin.
```rust let result = if condition {
// code to be executed if condition is true
} else {
// code to be executed if condition is false}; ```
Rust documentation: s://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions(https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions)
- Swift
Swift uses the `if` statement similarly to other C-like languages, with an optional `else` clause.
```swift if condition {
// code to be executed if condition is true} else {
// code to be executed if condition is false} ```
Swift documentation: s://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID127(https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID127)
- Transact-SQL
Transact-SQL uses `IF…ELSE` to perform conditional logic in SQL scripts.
```sql IF condition
BEGIN -- code to be executed if condition is true ENDELSE
BEGIN -- code to be executed if condition is false END```
Transact-SQL documentation: s://docs.microsoft.com/en-us/sql/t-sql/language-elements/if-else-transact-sql(https://docs.microsoft.com/en-us/sql/t-sql/language-elements/if-else-transact-sql)
- PL/SQL
PL/SQL's `IF` statement allows for conditional logic within Oracle databases, supporting `ELSIF` for multiple conditions.
```plsql IF condition THEN
-- code to be executed if condition is trueELSIF condition2 THEN
-- code to be executed if condition2 is trueELSE
-- code to be executed if no conditions are trueEND IF; ```
PL/SQL documentation: s://docs.oracle.com/cd/B19306_01/appdev.102/b14261/if_statement.htm(https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/if_statement.htm)
These summaries illustrate the fundamental role of the `if` statement across a wide array of programming languages, demonstrating its universal application in conditional logic and flow control.
Fair Use Sources
- 9781617298509 ([2024]])
C++: C++ Fundamentals, C++ Inventor - C++ Language Designer: Bjarne Stroustrup in 1985; C++ Keywords, C++ Built-In Data Types, C++ Data Structures (CPP Containers) - C++ Algorithms, C++ Syntax, C++ OOP - C++ Design Patterns, Clean C++ - C++ Style Guide, C++ Best Practices ( C++ Core Guidelines (CG)) - C++ BDD, C++ Standards ( C++ 23, C++ 20, C++ 17, C++ 14, C++ 11, C++ 03, C++ 98), Bjarne Stroustrup's C++ Glossary, CppReference.com, CPlusPlus.com, ISOcpp.org, C++ Compilers (Compiler Explorer, MinGW), C++ IDEs, C++ Development Tools, C++ Linter, C++ Debugging, C++ Modules ( C++20), C++ Packages, C++ Package Manager ( Conan - the C/C++ Package Manager), C++ Standard Library, C++ Libraries, C++ Frameworks, C++ DevOps - C++ SRE, C++ CI/CD ( C++ Build Pipeline), C++ Data Science - C++ DataOps, C++ Machine Learning, C++ Deep Learning, Functional C++, C++ Concurrency, C++ History, C++ Topics, C++ Bibliography, Manning C++ Series, C++ Courses, CppCon, C++ Research, C++ GitHub, Written in C++, C++ Popularity, C++ Awesome , C++ Versions. (navbar_cplusplus – see also navbar_cpp_containers, navbar_cppcon, navbar_cpp_core_guidelines, navbar_cpp23, navbar_cpp20, navbar_cpp17, navbar_cpp14, navbar_cpp11)
Reserved Words: Programming Language Keywords, aka Reserved Identifiers. (navbar_reserved_words - see also navbar_programming)
© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers
SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.