📜  C / C++中的决策(if,if..else,嵌套的if,if-else-if)

📅  最后修改于: 2021-05-30 17:22:27             🧑  作者: Mango

在现实生活中,有些情况下我们需要做出一些决定,并根据这些决定来决定下一步该做什么。在编程中也会出现类似的情况,我们需要做出一些决策,然后基于这些决策,我们将执行下一个代码块。例如,在C中,如果出现x,则执行y,否则执行z。也可能有多个条件,例如在C中,如果x发生,则执行p;否则,如果条件y发生,则执行q,否则执行r。 C else-if条件是导入多个条件的多种方式之一。

编程语言中的决策陈述决定了程序执行流程的方向。在C或C++中可用的决策声明为:

  1. 如果声明
  2. if..else语句
  3. 嵌套if语句
  4. if-else-if梯子
  5. 切换语句
  6. 跳转语句:
    1. 休息
    2. 继续
    3. 返回

C / C++中的if语句

if语句是最简单的决策语句。它用于决定是否执行某个语句或语句块,即如果某个条件为真,则执行语句块,否则不执行。
语法

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

在此,评估后的条件为真或假。 C if语句接受布尔值-如果该值为true,则它将执行其下面的语句块,否则不执行。如果我们在if(condition)之后不提供大括号'{‘和’}’,则默认情况下if语句将紧接其后的第一个语句放在其块内。
范例

if(condition)
   statement1;
   statement2;

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

流程图

Java中的if语句

C
// C program to illustrate If statement
#include 
 
int main() {
    int i = 10;
 
    if (i > 15)
    {
       printf("10 is less than 15");
    }   
    
    printf("I am Not in if");
}


C++
// C++ program to illustrate If statement
#include
using namespace std;
 
    int main()
    {
        int i = 10;
  
        if (i > 15)
        {
           cout<<"10 is less than 15";
        }   
        
        cout<<"I am Not in if";
    }


C
// C program to illustrate If statement
#include 
 
int main() {
    int i = 20;
 
    if (i < 15)
        printf("i is smaller than 15");
    else
        printf("i is greater than 15");
             
    return 0;   
}


C++
// C++ program to illustrate if-else statement
#include
using namespace std;
 
int main()
 {
        int i = 20;
  
        if (i < 15)
            cout<<"i is smaller than 15";
        else
            cout<<"i is greater than 15";
             
    return 0;   
 }


C
// C program to illustrate nested-if statement
#include 
 
int main() {
    int i = 10;
 
    if (i == 10)
    {
        // First if statement
        if (i < 15)
           printf("i is smaller than 15\n");
 
        // Nested - if statement
        // Will only be executed if statement above
        // is true
        if (i < 12)
            printf("i is smaller than 12 too\n");
        else
            printf("i is greater than 15");
    }
 
    return 0;
}


C++
// C++ program to illustrate nested-if statement
#include 
using namespace std;
 
int main()
{
    int i = 10;
 
    if (i == 10)
    {
        // First if statement
        if (i < 15)
           cout<<"i is smaller than 15\n";
 
        // Nested - if statement
        // Will only be executed if statement above
        // is true
        if (i < 12)
            cout<<"i is smaller than 12 too\n";
        else
            cout<<"i is greater than 15";
    }
 
    return 0;
}


C
// C program to illustrate nested-if statement
#include 
 
int main() {
    int i = 20;
  
    if (i == 10)
        printf("i is 10");
    else if (i == 15)
        printf("i is 15");
    else if (i == 20)
        printf("i is 20");
    else
        printf("i is not present");
}


C++
// C++ program to illustrate if-else-if ladder
#include
using namespace std;
 
int main()
{
    int i = 20;
  
    if (i == 10)
        cout<<"i is 10";
    else if (i == 15)
        cout<<"i is 15";
    else if (i == 20)
        cout<<"i is 20";
    else
        cout<<"i is not present";
}


C
// C program to illustrate
// Linear Search
 
#include 
 
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) {
            printf("Element found at position: %d", (i + 1));
            break;
        }
    }
}
 
int main() {
    int arr[] = { 1, 2, 3, 4, 5, 6 };
     
    // no of elements
    int n = 6; 
     
    // key to be searched
    int key = 3;
 
    // Calling function to find the key
    findElement(arr, n, key);
 
    return 0;
}


C++
// CPP program to illustrate
// Linear Search
#include 
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;
}


C
// C program to explain the use
// of continue statement
#include 
 
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
            printf("%d ", i);
    }
 
    return 0;
}


C++
// C++ program to explain the use
// of continue statement
 
#include 
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;
}


C++
#include
using namespace std;
 
int main(){
    int gfg=0; // local variable for main
    cout<<"Before if-else block "<


C
#include 
 
int main() {
 
      int gfg=0; // local variable for main
    printf("Before if-else block %d\n",gfg);
    if(1){
        int gfg = 100; // new local variable of if block
        printf("if block %d\n",gfg);
    }
    printf("After if block %d",gfg);
  return 0;
}


C
// C program to print numbers
// from 1 to 10 using goto statement
#include 
 
// function to print numbers from 1 to 10
void printNumbers()
{
    int n = 1;
label:
    printf("%d ",n);
    n++;
    if (n <= 10)
        goto label;
}
 
// Driver program to test above function
int main() {
    printNumbers();
    return 0;
}


C++
// C++ program to print numbers
// from 1 to 10 using goto statement
#include 
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;
}


C
// C code to illustrate return
// statement
#include 
 
// 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)
{
    printf("The sum is %d", s2);
    return;
}
 
int main()
{
    int num1 = 10;
    int num2 = 10;
    int sum_of = SUM(num1, num2);
    Print(sum_of);
    return 0;
}


C++
// C++ code to illustrate return
// statement
#include 
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;
}


输出:
I am Not in if

由于if语句中存在的条件为false。因此,不执行if语句下面的块。

C / C++中的if-else

if语句就告诉我们,如果条件为true,它将执行语句块,如果条件为false,则不会。但是,如果条件为假,我们想做其他事情怎么办。这是C else语句。当条件为false时,可以将else语句与if语句一起使用以执行代码块。
语法

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

流程图

if-else语句

例子:

C

// C program to illustrate If statement
#include 
 
int main() {
    int i = 20;
 
    if (i < 15)
        printf("i is smaller than 15");
    else
        printf("i is greater than 15");
             
    return 0;   
}

C++

// C++ program to illustrate if-else statement
#include
using namespace std;
 
int main()
 {
        int i = 20;
  
        if (i < 15)
            cout<<"i is smaller than 15";
        else
            cout<<"i is greater than 15";
             
    return 0;   
 }
输出:
i is greater than 15

如果if语句中的条件为false,则执行else语句之后的代码块。

如果在C / C++中嵌套

在C中嵌套的if是if语句,它是另一个if语句的目标。嵌套的if语句表示另一个if语句内的if语句。是的,C和C++都允许我们将if语句嵌套在if语句内,即,可以将if语句放置在另一个if语句内。
句法:

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

流程图

嵌套如果

例子:

C

// C program to illustrate nested-if statement
#include 
 
int main() {
    int i = 10;
 
    if (i == 10)
    {
        // First if statement
        if (i < 15)
           printf("i is smaller than 15\n");
 
        // Nested - if statement
        // Will only be executed if statement above
        // is true
        if (i < 12)
            printf("i is smaller than 12 too\n");
        else
            printf("i is greater than 15");
    }
 
    return 0;
}

C++

// C++ program to illustrate nested-if statement
#include 
using namespace std;
 
int main()
{
    int i = 10;
 
    if (i == 10)
    {
        // First if statement
        if (i < 15)
           cout<<"i is smaller than 15\n";
 
        // Nested - if statement
        // Will only be executed if statement above
        // is true
        if (i < 12)
            cout<<"i is smaller than 12 too\n";
        else
            cout<<"i is greater than 15";
    }
 
    return 0;
}
输出:
i is smaller than 15
i is smaller than 12 too

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

在这里,用户可以在多个选项中进行选择。 C if语句从上至下执行。一旦控制if的条件之一为true,就会执行与if关联的语句,其余的C else-if阶梯将被绕过。如果所有条件都不成立,则将执行最终的else语句。
句法:

if (condition)
    statement;
else if (condition)
    statement;
.
.
else
    statement;

如果其他如果梯子

例子:

C

// C program to illustrate nested-if statement
#include 
 
int main() {
    int i = 20;
  
    if (i == 10)
        printf("i is 10");
    else if (i == 15)
        printf("i is 15");
    else if (i == 20)
        printf("i is 20");
    else
        printf("i is not present");
}

C++

// C++ program to illustrate if-else-if ladder
#include
using namespace std;
 
int main()
{
    int i = 20;
  
    if (i == 10)
        cout<<"i is 10";
    else if (i == 15)
        cout<<"i is 15";
    else if (i == 20)
        cout<<"i is 20";
    else
        cout<<"i is not present";
}
输出:
i is 20

C / C++中的跳转语句

这些语句在C或C++中用于通过程序中的功能进行无条件控制。它们支持四种类型的跳转语句:

  1. C中断:此循环控制语句用于终止循环。一旦从循环内遇到break语句,循环迭代便在那里停止,控制权立即从循环返回到循环后的第一个语句。
    句法:
break;
  1. 基本上,在不确定循环的实际迭代次数或希望根据某些条件终止循环的情况下,会使用break语句。

  1. 例子:

C

// C program to illustrate
// Linear Search
 
#include 
 
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) {
            printf("Element found at position: %d", (i + 1));
            break;
        }
    }
}
 
int main() {
    int arr[] = { 1, 2, 3, 4, 5, 6 };
     
    // no of elements
    int n = 6; 
     
    // key to be searched
    int key = 3;
 
    // Calling function to find the key
    findElement(arr, n, key);
 
    return 0;
}

C++

// CPP program to illustrate
// Linear Search
#include 
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;
}
输出:
Element found at position: 3
  1. C继续:此循环控制语句就像break语句一样。 Continue语句与break语句相反,它不是终止循环,而是强制执行循环的下一个迭代。
    顾名思义,continue语句将强制循环继续执行或执行下一个迭代。当在循环中执行continue语句时,continue语句之后的循环内的代码将被跳过,并且循环的下一个迭代将开始。
    句法:
continue;

  1. 例子:

C

// C program to explain the use
// of continue statement
#include 
 
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
            printf("%d ", i);
    }
 
    return 0;
}

C++

// C++ program to explain the use
// of continue statement
 
#include 
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;
}
输出:
1 2 3 4 5 7 8 9 10

如果在C / C++中的if else中创建变量,则该变量仅在if / else块中是局部的。您可以在if / else块中使用全局变量。如果您在if / else中创建的变量名称与任何全局变量相同,则将优先考虑“局部变量”。

C++

#include
using namespace std;
 
int main(){
    int gfg=0; // local variable for main
    cout<<"Before if-else block "<

C

#include 
 
int main() {
 
      int gfg=0; // local variable for main
    printf("Before if-else block %d\n",gfg);
    if(1){
        int gfg = 100; // new local variable of if block
        printf("if block %d\n",gfg);
    }
    printf("After if block %d",gfg);
  return 0;
}
  1. C goto: C / C++中的goto语句也称为无条件跳转语句,可用于在函数从一个点跳转到另一点。
    语法
Syntax1      |   Syntax2
----------------------------
goto label;  |    label:  
.            |    .
.            |    .
.            |    .
label:       |    goto label;
  1. 在以上语法中,第一行告诉编译器转到或跳到标记为标签的语句。这里label是一个用户定义的标识符,它指示目标语句。 “ label:”之后紧随其后的语句是目标语句。 “标签:”也可以出现在“转到标签”之前;上面语法中的语句。

去

  1. 以下是有关如何使用goto语句的一些示例:
    例子:

C

// C program to print numbers
// from 1 to 10 using goto statement
#include 
 
// function to print numbers from 1 to 10
void printNumbers()
{
    int n = 1;
label:
    printf("%d ",n);
    n++;
    if (n <= 10)
        goto label;
}
 
// Driver program to test above function
int main() {
    printNumbers();
    return 0;
}

C++

// C++ program to print numbers
// from 1 to 10 using goto statement
#include 
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;
}
输出:
1 2 3 4 5 6 7 8 9 10
  1. C return: C或C++中的return将执行流返回到调用它的函数。该语句不需要强制性的任何条件语句。一旦执行该语句,程序流将立即停止,并从调用它的位置返回控件。 return语句可能会或可能不会为void函数返回任何内容,但对于非void函数,必须返回一个返回值。
    句法:
return[expression];
  1. 例子:

C

// C code to illustrate return
// statement
#include 
 
// 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)
{
    printf("The sum is %d", s2);
    return;
}
 
int main()
{
    int num1 = 10;
    int num2 = 10;
    int sum_of = SUM(num1, num2);
    Print(sum_of);
    return 0;
}

C++

// C++ code to illustrate return
// statement
#include 
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;
}
输出:
The sum is 20

?list = PLqM7alHXFySGg6GSRmE2INI4k8fPH5qVB

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”