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>


分享到:


相關文章: