C# 将for和if循环组合在一起

C# 将for和if循环组合在一起,c#,loops,for-loop,C#,Loops,For Loop,刚开始编程和阅读操作手册。问题是关于循环。我有以下代码: public bool DoThisJob(string job, int numShift) { if (!string.IsNullOrEmpty(currentJob)) return false; for (int i = 0; i < jobsICanDo.Length; i++) { if (jobsICanDo[i] == job) {

刚开始编程和阅读操作手册。问题是关于循环。我有以下代码:

public bool DoThisJob(string job, int numShift) {
        if (!string.IsNullOrEmpty(currentJob))
            return false;
        for (int i = 0; i < jobsICanDo.Length; i++) {
            if (jobsICanDo[i] == job) {
                currentJob = job;
                this.shiftsToWork = numberOfShifts;
                shiftsWorked = 0;
                return true;
            }
        return false;
public bool DoThisJob(字符串job,int numShift){
如果(!string.IsNullOrEmpty(currentJob))
返回false;
for(int i=0;i

如果currentJob字符串不为空,则返回false或true?没有else语句,那么如果它为真,我们如何知道该怎么办


下一次运行for循环,并且再次运行for循环,因为它返回false或true?上一次运行if语句,这是自解释的。

没有其他语句,因此我们如何知道如果它是true该怎么办?

if..else
(或者经典地称为
if..then..else
)中,构造
else
if可选,如果没有它,它将进入
if
块外的下一个语句,在这种情况下是
for
语句

您的上述函数可以等效为

public bool DoThisJob(string job, int numShift) {
        if (!string.IsNullOrEmpty(currentJob))
            return false;
        else {
            for (int i = 0; i < jobsICanDo.Length; i++) {
                if (jobsICanDo[i] == job) {
                    currentJob = job;
                    this.shiftsToWork = numberOfShifts;
                    shiftsWorked = 0;
                    return true;
                }
            return false;
        }
public bool DoThisJob(字符串job,int numShift){
如果(!string.IsNullOrEmpty(currentJob))
返回false;
否则{
for(int i=0;i
由于“return”而没有其他内容语句。它立即中止函数的执行并返回调用函数。您可以使用else编写它,它将执行相同的函数。

如果当前作业字符串为null,则不会运行
If
语句,它将进入
for
循环

如果
for
循环中的条件触发它返回true,则该方法返回true,并且永远不会到达最后一次返回


如果
for
循环没有返回true,它将落在最后一行,方法将返回false。

返回语句将在该点停止函数的执行,并将控制权返回给调用过程

public bool DoThisJob(string job, int numShift) {
        if (!string.IsNullOrEmpty(currentJob))
            return false; // If it is not empty then function call returns from this statement

        // Else, flow control falls through and below code is executed
        for (int i = 0; i < jobsICanDo.Length; i++) {
            if (jobsICanDo[i] == job) {
                currentJob = job;
                this.shiftsToWork = numberOfShifts;
                shiftsWorked = 0;
                return true;
            }

        return false;
        }
public bool DoThisJob(字符串job,int numShift){
如果(!string.IsNullOrEmpty(currentJob))
return false;//如果它不是空的,则函数调用从此语句返回
//否则,流控制将失效,并执行以下代码
for(int i=0;i

我希望,这澄清了问题。

您的函数可以简化如下,无需循环

public bool DoThisJob(string job, int numShift) {

    if (!string.IsNullOrEmpty(currentJob) || !jobsICanDo.Contains<string>(job))
    {
       //if currentJob NOT null/empty OR 
       //job is not in the jobsICanDo[] array
       return false;
    }
    else
    {
        currentJob = job;
        this.shiftsToWork = numberOfShifts;
        shiftsWorked = 0;
        return true;
    }
}
public bool DoThisJob(字符串job,int numShift){
如果(!string.IsNullOrEmpty(currentJob)| |!jobsICanDo.Contains(job))
{
//如果currentJob不为null/空或
//作业不在jobsICanDo[]数组中
返回false;
}
其他的
{
currentJob=job;
this.shiftsToWork=班次数;
shiftsWorked=0;
返回true;
}
}

它不需要else,因为您只想为其中一种情况执行特定的操作。对于其他所有操作,您都希望方法中的其余代码运行。这类似于在(arg==null)抛出新ArgumentNullException(“arg”)时执行
保护方法中的无效参数。
return
立即停止方法的执行。这通常称为保护语句。它在功能上相当于将整个方法包装在if(string.IsNullOrEmpty(currentJob)){/*everything*/}else{return false;}
。这两种方法都不是对的,也不是错的,尽管我个人更喜欢guard语句的可读性。实际上有一个else语句。else语句是整个
for loop
。我会读更多你的编程书籍。