if语句的这种用法是否会导致未定义的行为?

时间:2019-03-11 16:24:14

标签: c++ if-statement conditional language-lawyer undefined-behavior

我想知道条件语句的正确用法,例如if语句,以避免未定义的行为。让我们从一个示例开始:

uint8_t x = 0;
bool y = false;
bool z = false;

if ((x == 135) and !y and !z) {
    //do something
}
else if ((x == 135) and y) {
    x = 5;
    z = true;   
}
else if ((x == 5) and z) {
    x = 135;
    z = false;
}
else {
    //do something
}

现在,通过不将所有3个变量都包含在每个条件中,我是否会得到不确定的行为?是否将所有未说明的条件都放入else语句中?如果是这样,如果我放弃else语句会怎样?我有完全相同的if语句(在更复杂的情况下),而且似乎每次都不会进入正确的语句。

如果有此规定,请启发我?

3 个答案:

答案 0 :(得分:5)

  

通过在每个条件中不包含所有3个变量,我是否会得到不确定的行为?

不完全没有将所有变量都包含在每个条件中的行为。

  

所有无法解释的情况都会进入else语句吗?

如果条件为false,则执行语句为false(即关键字else之后的语句)。

  

如果我放弃else语句会怎样?

从if语句之后的语句继续执行。

答案 1 :(得分:2)

根据经验,是的,尝试读取尚未初始化的变量不是一个好主意,因为这样做的行为通常是 undefined 。但这不是事实:所有变量都已初始化。

但是在if(...)条件下,您所需要做的只是评估为truefalse。在这一点上,您的代码绝对正确。我会使用&&||而不是andor,尽管前者更常见。

有关一组非常全面的未定义构造,请参见What are all the common undefined behaviours that a C++ programmer should know about?

答案 2 :(得分:0)

否,您在这里没有未定义的行为。这听起来像是您没有条件(布尔)逻辑的准确心智模型。 var apprange = function () { function initAndSetupTheSliders(rangeValue, disabled = false,limitValue) { var inputs = [].slice.call(document.querySelectorAll('.range-slider input')); inputs.forEach(function (input) {return input.setAttribute('value', rangeValue);}); inputs.forEach(function (input) {return apprange.updateSlider(input,limitValue, disabled);}); // Cross-browser support where value changes instantly as you drag the handle, therefore two event types. if(!disabled){ ############## Following two lines are not working as it should. ############ inputs.forEach(function (input) {return input.addEventListener('input', eventHandler(input, limitValue, disabled));}); inputs.forEach(function (input) {return input.addEventListener('change', eventHandler(input, limitValue, disabled));}); ############# Code end ################## }else{ inputs.forEach(function (input) {return input.removeEventListener('input', eventHandler(input, limitValue, disabled));}); inputs.forEach(function (input) {return input.removeEventListener('change', eventHandler(input, limitValue, disabled));}); console.log('#### 2'); } } function eventHandler(input,limitValue, disabled){ return apprange.updateSlider(input,limitValue, disabled); } function updateSlider(element, limitValue, disabled) { if (element) { var parent = element.parentElement, lastValue = parent.getAttribute('data-slider-value'); if (lastValue === element.value) { return; // No value change, no need to update then } parent.setAttribute('data-slider-value', element.value); var $thumb = parent.querySelector('.range-slider__thumb'), $bar = parent.querySelector('.range-slider__bar'), $thumbLine = parent.querySelector('.range-slider__line'), pct = element.value * ((parent.clientHeight - $thumb.clientHeight) / parent.clientHeight); pctLimitLine = limitValue * ((parent.clientHeight - $thumb.clientHeight) / parent.clientHeight); $thumb.style.bottom = pct + '%'; $thumbLine.style.bottom = pctLimitLine + 7 + '%'; $bar.style.height = 'calc(' + pct + '% + ' + $thumb.clientHeight / 2 + 'px)'; $thumb.textContent = element.value + ''; document.getElementById('range').value = element.value; /* Emoticons Margins */ if(disabled && false){ var sliderHeight = parent.clientHeight; var multiplier = sliderHeight / 100; var UpperRegionHeight = (100 - limitValue) * multiplier; var LowerRegionHeight = sliderHeight - UpperRegionHeight; document.getElementsByClassName('icon_max_smily')[0].setAttribute("style", "height:"+UpperRegionHeight+"px"); document.getElementsByClassName('icon_high_smily')[0].setAttribute("style", "height:"+(LowerRegionHeight/3)+"px"); document.getElementsByClassName('icon_medium_smily')[0].setAttribute("style", "height:"+(LowerRegionHeight/3)+"px"); document.getElementsByClassName('icon_low_smily')[0].setAttribute("style", "height:"+(LowerRegionHeight/3)+"px"); if(document.getElementsByClassName('icon_max_smily')[1] !== undefined){ document.getElementsByClassName('icon_max_smily')[1].setAttribute("style", "height:"+UpperRegionHeight+"px"); document.getElementsByClassName('icon_high_smily')[1].setAttribute("style", "height:"+(LowerRegionHeight/3)+"px"); document.getElementsByClassName('icon_medium_smily')[1].setAttribute("style", "height:"+(LowerRegionHeight/3)+"px"); document.getElementsByClassName('icon_low_smily')[1].setAttribute("style", "height:"+(LowerRegionHeight/3)+"px"); } apprange.HighlightSmiley(limitValue, element.value); } } } function HighlightSmiley(limitValue, selectedValue){ if(selectedValue >= limitValue){ console.log('if'); document.getElementsByClassName('icon_max_smily')[0].querySelector('img').setAttribute("width", "70px"); if(document.getElementsByClassName('icon_max_smily')[1] !== undefined){ document.getElementsByClassName('icon_max_smily')[1].querySelector('img').setAttribute("width", "70px"); } }else{ var limitMultiplier = limitValue/3; if(selectedValue >= 0 && selectedValue < limitMultiplier){ document.getElementsByClassName('icon_low_smily')[0].querySelector('img').setAttribute("width", "70px"); if(document.getElementsByClassName('icon_low_smily')[1] !== undefined){ document.getElementsByClassName('icon_low_smily')[1].querySelector('img').setAttribute("width", "70px"); } }else if(selectedValue >= limitMultiplier && selectedValue < 2*limitMultiplier){ document.getElementsByClassName('icon_medium_smily')[0].querySelector('img').setAttribute("width", "70px"); if(document.getElementsByClassName('icon_medium_smily')[1] !== undefined){ document.getElementsByClassName('icon_medium_smily')[1].querySelector('img').setAttribute("width", "70px"); } }else if(selectedValue >= 2*limitMultiplier && selectedValue < limitValue){ document.getElementsByClassName('icon_high_smily')[0].querySelector('img').setAttribute("width", "70px"); if(document.getElementsByClassName('icon_high_smily')[1] !== undefined){ document.getElementsByClassName('icon_high_smily')[1].querySelector('img').setAttribute("width", "70px"); } } } } function removeEventHandler(){ } return { initAndSetupTheSliders: initAndSetupTheSliders, updateSlider: updateSlider, HighlightSmiley: HighlightSmiley, eventHandler: eventHandler}; }(); 始终是 *, *:before, *:after { box-sizing: border-box; } html, body { height: 100%; } body { margin: 0; background: #3D3D4A; color: white; min-height: 400px; font-family: sans-serif; } .info { position: absolute; top: 0; left: 0; padding: 10px; opacity: .5; } .container { padding-top: 40px; position: relative; display: inline-block; top: 100%; left: 50%; transform: translate(-50%, -100%); padding-bottom: 20px; } @media (min-height: 500px) { .container { position: absolute; top: 50%; transform: translate(-50%, -50%); padding-bottom: 0; } } .range-slider { display: inline-block; width: 40px; position: relative; text-align: center; height: 300px; max-height: 100%; } .range-slider:before { position: absolute; top: -2em; left: .5em; content: attr(data-slider-value) "%"; color: white; font-size: 90%; } .range-slider__thumb { position: absolute; left: 5px; width: 30px; height: 30px; line-height: 30px; background: white; color: #777; font-size: 50%; box-shadow: 0 0 0 4px #3D3D4A; border-radius: 50%; pointer-events: none; } .range-slider__bar { left: 12px; bottom: 0; position: absolute; background: linear-gradient(to left, #ffc000 37%, #c64e13 50%); pointer-events: none; width: 18px; border-radius: 10px; } .range-slider input[type=range][orient=vertical] { position: relative; margin: 0; height: 100%; width: 100%; display: inline-block; position: relative; writing-mode: bt-lr; -webkit-appearance: slider-vertical; } .range-slider input[type=range][orient=vertical]::-webkit-slider-runnable-track, .range-slider input[type=range][orient=vertical]::-webkit-slider-thumb { -webkit-appearance: none; } .range-slider input[type=range][orient=vertical]::-webkit-slider-runnable-track { border: none; background: #343440; width: 18px; border-color: #343440; border-radius: 10px; box-shadow: 0 0 0 2px #3D3D4A; } .range-slider input[type=range][orient=vertical]::-moz-range-track { border: none; background: #343440; width: 18px; border-color: #343440; border-radius: 10px; box-shadow: 0 0 0 2px #3D3D4A; } .range-slider input[type=range][orient=vertical]::-ms-track { border: none; background: white; width: 18px; border-color: #343440; border-radius: 10px; box-shadow: 0 0 0 2px #3D3D4A; color: transparent; height: 100%; } .range-slider input[type=range][orient=vertical]::-ms-fill-lower, .range-slider input[type=range][orient=vertical]::-ms-fill-upper, .range-slider input[type=range][orient=vertical]::-ms-tooltip { display: none; } .range-slider input[type=range][orient=vertical]::-webkit-slider-thumb { width: 30px; height: 30px; opacity: 0; } .range-slider input[type=range][orient=vertical]::-moz-range-thumb { width: 30px; height: 30px; opacity: 0; } .range-slider input[type=range][orient=vertical]::-ms-thumb { width: 30px; height: 30px; opacity: 0; } .theme1 { background: linear-gradient(pink, deeppink); } .theme2 { background: linear-gradient(tomato, red); } .theme3 { background: linear-gradient(yellow, orange); }语句的可选部分。

{p> 恰好一个 <div class="container"> <div class="range-slider"> <input type="range" orient="vertical" min="0" max="100" /> <div class="range-slider__bar"></div> <div class="range-slider__thumb"></div> </div> </div>将被执行。更改其中的elseif{ ... }的值将不会导致其他任何值被执行,因此首先确定决定。 >