C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

在現實生活中,有些情況下我們需要做出一些決策,並根據這些決策來決定下一步該做什麼。在編程中也會出現類似的情況,我們需要做出一些決策,然後基於這些決策,我們將執行下一個代碼塊。例如,在C中,如果出現x,則執行y,否則執行z。也可能有多個條件,例如在C中,如果x發生,則執行p;否則,如果條件y發生,則執行q,否則執行r。else-if條件是導入多個條件的多種方式之一。

C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

程序設計語言中的決策語句決定了程序執行的流向。在C或C++中可用的決策語句是:

  • if statement
  • if..else statements
  • nested if statements
  • if-else-if ladder
  • switch statements
  • Jump Statements: breakcontinuegotoreturn

C/C++中的if語句

if語句是最簡單的決策語句。它用於決定是否執行某個語句或語句塊,即如果某個條件為真,則執行某個語句塊,否則不執行。

語法:

<code>if(condition) 
{
// Statements to execute if
// condition is true
}/<code>

在此,評估後的條件為真或假。if語句接受布爾值,如果該值為true,則它將執行其下面的語句塊,否則不執行。如果我們在if(condition)之後不提供大括號“ {”和“}”,則默認情況下,if語句將緊接其後的第一個語句放在其塊內。

例如:

<code>if(condition)
statement1;
statement2;

// Here if the condition is true, if block
// will consider only statement1 to be inside
// its block./<code>

流程圖

C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

<code>// C++ program to illustrate If statement 
#include<iostream>
using namespace std;

int main()
{
int i = 10;


if (i > 15)
{
cout< }

cout< } /<iostream>/<code>

輸出:

<code>I am Not in if/<code>

由於if語句中存在的條件為false。因此,不執行if語句下面的塊。

C/C++中的if-else

if語句本身告訴我們,如果條件為true,它將執行一個語句塊,如果條件為false,則不會執行。但是,如果條件為false,我們希望執行其他操作,該怎麼辦。下面是else語句。當條件為false時,我們可以使用else語句和if語句來執行代碼塊。

語法:

<code>if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}/<code>

流程圖:

C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

示例:

<code>// C++ program to illustrate if-else statement 
#include<iostream>
using namespace std;

int main()
{
int i = 20;

if (i < 15)
cout< else
cout<
return 0;
} /<iostream>/<code>

輸出:

<code>i is greater than 15/<code>

如果if語句中的條件為false,則執行else語句之後的代碼塊。

C/C++中嵌套if

C和C++都可以嵌套if語句中的if語句,也就是說,我們可以將if語句放在另一個if語句中。

語法:

<code>if (condition1) 
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}/<code>

流程圖

C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

示例:

<code>// C++ program to illustrate nested-if statement 
#include <iostream>
using namespace std;

int main()
{
int i = 10;

if (i == 10)
{
// First if statement
if (i < 15)
cout<
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
cout< else

cout< }

return 0;
} /<iostream>/<code>

輸出:

<code>i is smaller than 15
i is smaller than 12 too/<code>

C/C++中的if-else-if階梯

在這裡,用戶可以在多個選項中進行選擇。if語句從上到下執行。一旦控制if的條件之一為true,則將執行與if關聯的語句,其餘的C else-if階梯將被繞過。如果所有條件都不成立,則將執行最終的else語句。

語法:

<code>if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;/<code>
C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

示例:

<code>// C++ program to illustrate if-else-if ladder 
#include<iostream>
using namespace std;

int main()
{
int i = 20;

if (i == 10)
cout< else if (i == 15)
cout< else if (i == 20)
cout< else
cout<} /<iostream>/<code>

輸出:

<code>i is 20/<code>

C/C++中的跳轉語句

這些語句在C或C++中用於通過程序中的函數進行無條件的控制流。它們支持四種類型的跳轉語句:

1.break:此循環控制語句用於終止循環。一旦循環中遇到break語句,循環迭代便在那裡停止,控制權立即從循環返回到循環後的第一個語句。

語法:

<code>break;/<code>

在不確定循環的實際迭代次數或希望根據某些條件終止循環的情況下,會使用break語句。

C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

示例:

<code>// CPP program to illustrate 
// Linear Search
#include <iostream>
using namespace std;

void findElement(int arr[], int size, int key)
{
// loop to traverse array and search for key
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
cout << "Element found at position: " << (i + 1);
break;
}
}
}

// Driver program to test above function

int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6 };
int n = 6; // no of elements
int key = 3; // key to be searched

// Calling function to find the key
findElement(arr, n, key);

return 0;
} /<iostream>/<code>

輸出:

<code>Element found at position: 3/<code>

2.continue:此循環控制語句就像break語句一樣。continue語句與break語句相反,它不是終止循環,而是強制執行循環的下一個迭代。

顧名思義,continue語句將強制循環繼續執行或執行下一個迭代。當在循環中執行continue語句時,continue語句之後的循環內代碼將被跳過,並且循環的下一個迭代將開始。

語法:

<code>continue;/<code>
C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

示例:

<code>// C++ program to explain the use 
// of continue statement

#include <iostream>
using namespace std;

int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {

// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;

else
// otherwise print the value of i
cout << i << " ";
}

return 0;
} /<iostream>/<code>

輸出:

<code>1 2 3 4 5 7 8 9 10/<code>

3.goto:C/C++中的goto語句也稱為無條件跳轉語句,可用於在函數中從一個點跳轉到另一點。

語法:

<code>Syntax1      |   Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;/<code>

在上述語法中,第一行告訴編譯器轉到或跳到標記為label的語句。這裡label是一個用戶定義的標識符,它指示目標語句。緊接在“ label:”之後的語句是目標語句。在上述語法中,“label:”也可以出現在“goto label;”語句之前。

C/C Plus Plus中的決策(if,if else,嵌套if,if-else-if)

以下是有關如何使用goto語句的一些示例:

<code>// C++ program to print numbers 
// from 1 to 10 using goto statement
#include <iostream>
using namespace std;

// function to print numbers from 1 to 10
void printNumbers()
{
int n = 1;
label:
cout << n << " ";
n++;
if (n <= 10)
goto label;
}

// Driver program to test above function
int main()
{
printNumbers();
return 0;
} /<iostream>/<code>

輸出:

<code>1 2 3 4 5 6 7 8 9 10/<code>

4.return:C或C++中的return將執行流返回到調用它的函數。該語句不需要任何條件語句。一旦執行該語句,程序流將立即停止,並從調用它的位置返回。對於void函數,return語句可以返回也可以不返回任何內容,但是對於非void函數,必須返回返回值。

語法:

<code>return[expression];/<code>

示例:

<code>// C++ code to illustrate return 
// statement
#include <iostream>
using namespace std;

// non-void return type
// function to calculate sum
int SUM(int a, int b)

{
int s1 = a + b;
return s1;
}

// returns void
// function to print
void Print(int s2)
{
cout << "The sum is "<< s2;
return;
}

int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
Print(sum_of);
return 0;
} /<iostream>/<code>

輸出:

<code>The sum is 20/<code>


分享到:


相關文章: