hello Dear!! I'm a new coming in the community I want your help. about coding on MATLAB does the code is correct ? or the using of (&&) is no faire? and how can I correct it? thank you for respond .
29 views (last 30 days)
Show older comments
if app.LikeCheckBox.Value==true
app.EditField.Value='you Like our channal'
elseif app.LikeCheckBox.Value==true && app.ShareCheckBox.Value==true
app.EditField.Value='you liked and shared'
elseif app.ShareCheckBox.Value==true
app.EditField.Value='you Share our channal'
elseif app.ShareCheckBox.Value==true && app.SubscribeCheckBox.Value==true
app.EditField.Value='you shared and subscribed'
elseif app.SubscribeCheckBox.Value==true
app.EditField.Value='you Subscribed to our channal'
elseif app.LikeCheckBox.Value==true && app.SubscribeCheckBox.Value==true
app.EditField.Value='good job'
else app.EditField.Value='Choose on item'
app.EditField.HorizontalAlignment='center'
end
0 Comments
Answers (3)
Jacob Mathew
on 21 Nov 2024 at 16:09
Hey god,
You can use double ampersand (&&) as logical and with an if statement. The following piece of code demonstrates the same:
a = true;
b = true;
c = false;
a && b
a && c
0 Comments
Image Analyst
on 21 Nov 2024 at 16:16
The double && is correct. However you don't need to do a logical comparison to true. If it's true, then app.LikeCheckBox.Value==true will return true when it evaluates that equality expression, which is just the same as app.LikeCheckBox.Value. So you can more simply just do
elseif app.LikeCheckBox.Value && app.ShareCheckBox.Value
etc. and not use the ==true.
0 Comments
Walter Roberson
on 21 Nov 2024 at 19:37
if app.LikeCheckBox.Value==true
app.EditField.Value='you Like our channal'
elseif app.LikeCheckBox.Value==true && app.ShareCheckBox.Value==true
Consider that if LikeCheckBox value is true then the first action will be triggered -- and no further checking of elseif will be done. So the only way to trigger the elseif is if LikeCheckbox is something other than true. But if LikeCheckBox value is something other than true, then it cannot be true for the purpose of the app.LikeCheckBox.Value==true test in the elseif clause.
You have two major choices:
- You can check the most exclusive set of conditions first -- liked and shared and subscribed -- and then elseif different subsets to narrow down the possibilities.
- You can build up a set of flags, flags(1,3) = false; flags(1) = app.LikeCheckBox.Value; flags(2) = app.ShareCheckBox.Value; flags(3) = app.SubscribeCheckBox.Value; . Now build up strings based on the flags. Such as if flags(2); if any(flags(1)) State = State + " and "; end; State = State + "shared"; end . At the end, the complete string will have been built up.
1 Comment
Steven Lord
on 21 Nov 2024 at 20:01
Or rather than just using if / elseif / elseif / else, you could include some nested if statements to give some structure to the checking.
if app.LikeCheckBox.Value
% You at least liked the channel, now let's see about sharing /
% subscribing
if app.ShareCheckBox.Value
% You liked and shared, so let's check subscribed
if app.SubscribeCheckBox.Value
str = "You Liked, Shared, and Subscribed to our channel";
else
str = "You Liked and Shared our channel";
end
else
% You liked but didn't share, check subscribed
if app.SubscribeCheckBox.Value
str = "You Liked and Subscribed to our channel";
else
str = "You Liked our channel";
end
end
else
% You didn't like the channel
if app.ShareCheckBox.Value
if app.SubscribeCheckBox.Value
str = "You Shared and Subscribed to our channel";
else
str = "You Shared our channel";
end
else
if app.SubscribeCheckBox.Value
str = "You Subscribed to our channel";
else
str = "Why didn't you Like, Share, or Subscribe to the channel?";
end
end
end
From here you can see that all eight combinations of options are covered, by inspection of the eight messages assigned to the variable str. This would get unwieldy if you had more options or if you had more than two possible values for the options, but 2^3 options IMO is still manageable.
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!