peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Toán tử ba ngôi trong Visual Basic: If ((condition), exp1, exp2)
BẮT ĐẦU
RẺ NHÁNH
VÒNG LẶP
LỆNH NHẢY
KIỂU DỮ LIỆU
NÂNG CAO
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Toán tử ba ngôi trong Visual Basic: If ((condition), exp1, exp2)

Trong bài này mình sẽ giới thiệu các bạn toán tử ba ngôi trong Visual Basic, đây là toán tử giúp rút ngắn chương trình từ những lệnh if else đơn giản.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Nếu bạn đã từng học những ngôn ngữ khác rồi thì không còn quá xa lạ với thuật ngữ "toán tử ba ngôi", còn nếu bạn là người mới thì cần phải tìm hiểu về lệnh if rồi mới xem bài này nhé.

1. Cú pháp toán tử ba ngôi trong Visual Basic

Trong Visual Basic, toán tử ba ngôi sẽ luôn làm việc với 3 toán hạng. Sau đây là cú pháp xác định toán tử ba ngôi trong ngôn ngữ lập trình Visual Basic.

If((condition), exp1, exp2) 

Giải thích như sau: Nếu biểu thức conditiontrue thì biểu thức exp1 sẽ được chạy, ngược lai biểu thức exp2 sẽ chạy.

Bài viết này được đăng tại [free tuts .net]

Vì là một toán tử nên nó sẽ phải trả về một giá trị, nên exp1exp2 thường là những hàm hoặc phương thức trả về một giá trị.

Thực ra việc sử dụng lệnh if else để thay thế cho toán tử ba ngôi là hoàn toàn được. Tuy nhiên nếu chương trình không quá phức tạp thì việc sử dụng toán tử ba ngôi là rất hợp lý.

Ví dụ dưới đây cho thấy cách sử dụng của cả hai.

Dim x As Integer = 5, y As Integer = 20

Dim result As String
' Dùng lệnh if else
If x > y Then
     result = "x lớn hơn y"
Else
     result = "x bé hơn y"
End If

' Dùng toán tử ba ngôi
result = If((x > y), "x lớn hơn y", "x bé hơn y")

Như bạn thấy, việc sử dụng toán tử ba ngôi trong trường hợp này sẽ rút ngắn được rất nhiều đoạn code. Tuy nhiên, điều này chỉ phù hợp khi đó là những bài toán đơn giản.

2. Ví dụ toán tử ba ngôi trong Visual Basic

Sau đây là ví dụ sử dụng toán tử ba ngôi trong ngôn ngữ lập trình Visual Basic.

Module Module1
    Sub Main()
        Dim x As Integer = 5, y As Integer = 20
        Dim result As String
        result = If((x > y), "x lớn hơn y", "x bé hơn y")
        Console.WriteLine(result)
        Console.WriteLine("Press Enter Key to Exit..")
        Console.ReadLine()
    End Sub
End Module

Trong ví dụ này thì vì x < y nên biến result sẽ nhận giá trị là "x value less than y".

Như mình đã nói, một số trường hợp code quá dài thì không nên sử dụng toán tử ba ngôi, bởi nhìn vào code sẽ rất rối. Thay vì code ngắn nhưng khó đọc thì ta vẫn thích code dài mà dễ đọc.

Ví dụ sau đây là mình sử dụng toán tử ba ngôi lồng nhau, trông rất khó theo dõi luồng xử lý của Visual Basic.

Module Module1
    Sub Main()
        Dim x As Integer = 20, y As Integer = 20
        Dim result As String
        ' Nested Ternary Operator
        result = If((x > y), "x lớn hơn y", If((x < y), "x bé hơn y", "x bằng y"))
        Console.WriteLine(result)
        Console.WriteLine("Press Enter Key to Exit..")
        Console.ReadLine()
    End Sub
End Module

Như vậy là mình đã hướng dẫn xong cách sử dụng toán tử ba ngôi trong Visual Basic, đây là toán tử rất hay và nên sử dụng trong những trường hợp đơn giản.

Cùng chuyên mục:

Xử lý chuỗi trong Visual Basic

Xử lý chuỗi trong Visual Basic

Dữ liệu String rất quan trọng, nó có thể lưu trữ hầu hết mọi loại…

Mảng trong Visual Basic (Array)

Mảng trong Visual Basic (Array)

Lệnh continue trong Visual Basic

Lệnh continue trong Visual Basic

Câu lệnh continue giúp vòng lặp ngay lập tức chuyển sang lần lặp tiếp theo…

Lệnh Exit trong Visual Basic

Lệnh Exit trong Visual Basic

Trong bài này ta sẽ tìm hiểu về lệnh Exit trong Visual Basic

Vòng lặp For Each trong Visual Basic (VB)

Vòng lặp For Each trong Visual Basic (VB)

Trong Visual Basic, vòng lặp For Each dùng để lặp qua các phần tử trong…

Vòng lặp Do While trong Visual Basic (VB)

Vòng lặp Do While trong Visual Basic (VB)

Vòng lặp Do While dùng trong trường hợp bạn chưa biết tổng số lần lặp…

Vòng lặp While trong Visual Basic (VB While Loop)

Vòng lặp While trong Visual Basic (VB While Loop)

Nếu vòng lặp For biết trước được tổng số lần lặp thì vòng lặp While…

Vòng lặp for trong Visual Basic (For Loop)

Vòng lặp for trong Visual Basic (For Loop)

Vòng lặp là một hành động được lặp đi lặp lai nhiều lần, và trong…

Lệnh Select Case trong Visual Basic

Lệnh Select Case trong Visual Basic

Lệnh Select Case dùng trong trường hợp điều kiện rẻ nhánh là một giá trị…

Lệnh if else trong Visual Basic: Dùng để rẻ nhánh chương trình

Lệnh if else trong Visual Basic: Dùng để rẻ nhánh chương trình

Trong bài này ta sẽ học cách sử dụng lệnh If Else trong Visual Basic

Các toán tử trong Visual Basic (Operators)

Các toán tử trong Visual Basic (Operators)

Toán tử hay còn gọi là Operators, dùng để tạo ra những biểu thức tính…

Comment trong Visual Basic: Tạo ghi chú trong quá trình code

Comment trong Visual Basic: Tạo ghi chú trong quá trình code

Hầu như ngôn ngữ nào cũng hỗ trợ tính năng comment, và VB cũng không…

Bảng Keywords trong Visual Basic

Bảng Keywords trong Visual Basic

Không chỉ riêng mỗi Visual Basic là có những keywords này, mà mỗi ngôn ngữ…

Các kiểu dữ liệu trong Visual Basic - VB Data Type

Các kiểu dữ liệu trong Visual Basic - VB Data Type

Trong Visual Basic, kểu dữ liệu data type dùng để khai định dạng dữ liệu…

Chương trình Visual Basic Hello World

Chương trình Visual Basic Hello World

Visual Basic có một cú pháp lập trình rất đơn giản.

Cài đặt Visual Studio

Cài đặt Visual Studio

Để cài đặt Visual Basic bạn chỉ cần tải Visual Studio Editor, sau đó cài…

Top