Co to jest instrukcja If w C?

Instrukcja If to po prostu zestaw operacji, których można użyć do porównania wyrażeń. Na ogół mają dwie wartości LHS i RHS. Ten operator porównuje wyrażenie lewej i prawej strony. Dla porównania zwraca po prostu wartość logiczną

Składnia

Ogólna składnia instrukcji If w C to:

if(expression to be evaluated ) (
// sets of instruction which needs to be executed
)

Objaśnienie składni

Teraz zrozummy powyższą składnię

Ogólne oświadczenie If kompromituje w wyżej wymieniony sposób i zawiera różne części. Wyjaśnijmy każdą część

  • Wyrażenie do oceny - W tej części dokonywana jest ocena instrukcji. Ta sekcja ogólnie składa się z lewej i prawej strony. Porównano i oceniono zarówno lewą stronę, która jest LHS, jak i prawą stronę, która jest RHS. Jeśli LHS jest równe RHS lub wyrażenie jest prawdziwe, wówczas kontrola wchodzi do sekcji if
  • Zestawy instrukcji, które należy wykonać - Jeżeli wyrażenie bloku if jest spełnione, wykonywane są zestawy instrukcji, które należy wykonać

Typowym przykładem pierwszej części może być sytuacja, gdy „1 jest mniejsza niż 10”, a prostym przykładem kodu, który należy wykonać, może być wydrukowanie dowolnej liczby

Różne typy instrukcji If

Są to różne typy instrukcji If. Wyjaśnijmy dogłębnie składnię

  • Instrukcja if-else
  • Instrukcja if-elseif-else

Instrukcja if-else

W tej składni jest podobny do:

if(expression to be evaluated ) (
// sets of instruction which needs to be executed
) else (
// sets of instruction which needs to be executed
)

Instrukcja if-elseif-else

W tej składni jest podobny do:

if( expression to be evaluated ) (
// sets of instruction which needs to be executed for if-block
) else if(
// sets of instruction which needs to be executed for else-if block
) else (
// sets of instruction which needs to be executed for else block
)

W tej sekcji każdy blok jest oceniany, a kod jest wykonywany zgodnie z oceną

Zobaczmy teraz ogólny schemat blokowy instrukcji If w C

Schemat blokowy instrukcji If

Przykłady

Teraz zrozummy powyższą składnię z przykładami

Przykład instrukcji If

Spójrzmy na to z przykładem

#include
void main () (
int varNumValue = 1;
if( varNumValue < 10 ) ( // checks the condition
printf("if statement instructions"); // sets of instructions which needs to be executed
)
)

Teraz skopiuj powyższy fragment kodu i uruchom go

Wyświetli następujące dane wyjściowe

Przykład instrukcji if-else

Spójrzmy na to z przykładem

#include
void main () (
char favoritePlaceToVisit() = "New York";
if (favoritePlaceToVisit == "New York") ( // checks the condition
printf(" Your favorite place to visit is New York "); // sets of instructions which needs to be executed for if block
) else (
printf("Your favorite place is different city"); // sets of instructions which needs to be executed for else block
)
)

Teraz skopiuj powyższy fragment kodu i uruchom go

Wyświetli następujące dane wyjściowe:

Teraz zainicjujmy zmienną favoritePlaceToVisit z wartością powiedz „Vegas”, aby w przeciwnym razie blok został wykonany

Spójrzmy na to z przykładem

#include
void main () (
char favoriteFruit() = "Apple";
if (favoriteFruit == "Kiwi") ( // checks the condition
printf("You like to eat Apple"); // sets of instructions which needs to be executed for if block
) else (
printf("You don't like to eat Apple"); // sets of instructions which needs to be executed for else block
)
)

Teraz skopiuj powyższy fragment kodu i uruchom go

Wyświetli następujące dane wyjściowe:

Teraz łatwo zrozumieć, co to jest instrukcja If, a co instrukcja if-else

Przykład instrukcji if-elseif-else

#include
void main () (
int enterNumberOfCarsYouHave = 1;
if( enterNumberOfCarsYouHave == 1 ) ( // checks the condition
printf("You have one car"); // sets of instructions which needs to be executed for if block
)
else if( enterNumberOfCarsYouHave == 2 ) ( // checks the condition
printf("You have two cars"); // sets of instructions which needs to be executed for if else block
)
else if( enterNumberOfCarsYouHave == 3 ) ( // checks the condition
printf("You have three cars"); // sets of instructions which needs to be executed for if else block
)
else if( enterNumberOfCarsYouHave == 4 ) ( // checks the condition
printf("You have four cars"); // sets of instructions which needs to be executed for if else block
)
else if( enterNumberOfCarsYouHave == 5 ) ( // checks the condition
printf("You have five cars"); // sets of instructions which needs to be executed for if else block
)
else if( enterNumberOfCarsYouHave == 6 ) ( // checks the condition
printf("You have six cars"); // sets of instructions which needs to be executed for if else block
)
else if( enterNumberOfCarsYouHave == 7 ) ( // checks the condition
printf("You have seven cars"); // sets of instructions which needs to be executed for if else block
)
else if( enterNumberOfCarsYouHave == 8 ) ( // checks the condition
printf("You have eight cars"); // sets of instructions which needs to be executed for if else block
)
else if( enterNumberOfCarsYouHave == 9 ) ( // checks the condition
printf("You have nine cars"); // sets of instructions which needs to be executed for if else block
)
else (
printf("You have more than 10 cars"); // sets of instructions which needs to be executed for else block
)
)

Teraz skopiuj powyższy fragment kodu i uruchom go

Wyświetli następujące dane wyjściowe:

Wniosek

C jest językiem programowania, w którym istnieje wiele pojęć, które należy studiować. Jeśli oświadczenie jest jednym z nich. Operatory te w zasadzie wykonują kod, aby sprawdzić, czy wartość wyrażenia jest prawdziwa, czy nie. Na podstawie oceny wyrażenia wykonuje kod. A jeśli instrukcja jest szeroko stosowana w dowolnym języku programowania do różnych logicznych wyrażeń programowania

Polecane artykuły

Jest to przewodnik po instrukcji If w C. Tutaj omawiamy różne typy instrukcji If z odpowiednim wyjaśnieniem składni wraz z przykładowym kodem. Możesz także zapoznać się z następującymi artykułami, aby dowiedzieć się więcej -

  1. Instrukcja C # if
  2. Instrukcja if-else w C
  3. Instrukcja zamiany w C
  4. Jeśli instrukcja Else w Pythonie
  5. Instrukcja Jquery IF (składnia)
  6. Instrukcja If w Pythonie
  7. Przewodnik po instrukcji If w R z przykładami