Indentation of blocks within case statements in vim - vim

I'm trying to get vim to play nicely with GTK's coding style by setting cinoptions but I can't figure out what to use for case statements.
Desired:
switch (x)
{
case 1:
{
foo();
}
case 2:
bar();
}
A) What I get with (not desired): setlocal et sw=2 cinoptions=>4,n-2,{2
switch (x)
{
case 1:
{
foo();
}
case 2:
bar();
}
B) What I get with (not desired): setlocal et sw=2 cinoptions=>4,n-2,{2,=0
switch (x)
{
case 1:
{
foo();
}
case 2:
bar();
}
Is there a way of getting the desired indentation automatically?

From the vim :help cino-:
*cino-:*
:N Place case labels N characters from the indent of the switch().
(default 'shiftwidth').
cino= cino=:0
switch (x) switch(x)
{ {
case 1: case 1:
a = b; a = b;
default: default:
} }
*cino-=*
=N Place statements occurring after a case label N characters from
the indent of the label. (default 'shiftwidth').
cino= cino==10 >
case 11: case 11: a = a + 1;
a = a + 1; b = b + 1;
You need add :0 to your cinoptions.
If need to also set =3 if you want 3 characters of indent under case.

Related

Checking for non terminating block statements in a file

I am working on a project that wants me to check for multi comments in a text file and also to see if it is a non terminating block statements. Pretty much I am using get char to check each character and compare it to the multi comment symbols and use peek to see if the next character matches the other symbols. The first part is working but to know when there is no terminating block statements is confusing please help.
if (c == '#' && inFile.peek() == '|') {
char next = '\0';
multipleComment += c;
while (inFile.get(c)) {
next = inFile.peek();
multipleComment += c;
if (c == '\n')
lineNumber++;
if (c == '|' && next == '#')
{
multipleComment += next;
tokenTypes.push_back(multipleComment);
values.push_back("COMMENT");
lineNumbers.push_back(lineNumber);
multipleComment.clear();
break;
}
else {
values.push_back("UNDEFINED");
tokenTypes.push_back(text);
lineNumbers.push_back(lineNumber);
}
}
}

Indent statement after case only without braces in vim cindent

My cinoptions is the folowing :
:set cinoptions={1s,t0,f0s,g0,i0,(0,=0
it works well with brace contained case statement, but not unbraced one :
switch(foo)
{
case(0):
{
...
break;
}
case(1):
... <-- should be indented
break;
}
i need the {1s for all my code need to be formated like that, if i drop the =0 i get this.
switch(foo)
{
case(0):
{
... <-- should not be idented so much
break;
}
case(1):
...
break;
}
Is there any way to specify vim not to indent case in any special way ?
Finaly done it myself, using a in house indent method :
function Indent(line)
" Store current pos
let l:indent = cindent(a:line)
let l:lineprec = a:line - 1
let l:linefirst = split(getline(a:line), " ")[0]
if l:linefirst ==# "{"
let l:case = split(getline(l:lineprec), " ")[0]
if l:case ==# "case"
let l:indent = indent(l:lineprec) + &shiftwidth
endif
endif
return l:indent
endfunction
add in .vimrc :
:set indentexpr=Indent(line(\".\"))
it's kinda specific to my coding style ( case must be followed by a space )

Switch Statement: Homework Confusion

Question: What is y after the following switch statement is executed? Rewrite the code using an if statement.
y = 3; x = 3;
switch (x + 3)
{
case 6: y = 1;
default: y += 1;
}
I'm at the beginning of the C++ hike. I don't know what do with this. It's not working in C++ Visual Studio 2013. I've put it in as is and nothing happens.
I use:
y = 3; x = 3;
switch (x + 3)
{
case 6: y = 1;
default: y += 1;
}
return 0;
}
And nothing happens. I have both the answers but I have no clue how to get them...
y is 2
if (x + 3 == 6)
y = 1;
y += 1;
I'm strictly supposed to be using
#include <iostream>
using namespace std;
int main()
{
return 0;
}
switch basics in C/C++:
switch expression is evaluated once, at the beginning. Then the 1st matching case is executed
after the case clause, the execution falls through to the next clause unless there's a break. Since this typically isn't what was indended, fall-throughs are normally documented with a comment or (if multiple cases refer to the same code) by stacking them together (case 2: case 3: <code>).

Switch statement bigger than

How to make a switch statement condition look like this:
if(age>18)
I want it to look like this, but then in a switch-statement version.
Is this possible?
Depends on the language you use.
C# => Not possible: switch case
Each case label specifies a constant value.
Java => Not possible: switch case
An if-then-else statement can test expressions based on ranges of
values or conditions, whereas a switch statement tests expressions
based only on a single integer
You will have to use an if-elseif-elseif-else approach in case you use Java or C#.
Javascript => Possible: switch case
case expressionN A case clause used to match against expression.
switch (true) {
case age > 18:
document.write("You are older than 18");
break;
}
mostly the switch statements are used when you are exactly matching the phrase.
switch (age>18)
{
case true:
//Do something
break;
case false:
//Do something
break;
}
In your case use if condition.It will also gives you better way to check your data.If your age is less than 18 or any other case.
if(age>18)
{/* Do something if your condition is true */ }
else
{ /* When your condition is false */ }
Yes, it's possible with a sort of 'reverse condition' trick - if that's supported by the language used, of course. That's how it looks in JavaScript:
function checkAge(age) {
switch (true) {
case age < 18:
console.log('Really young');
break;
case age < 25:
console.log('Young and proud');
break;
case age < 32:
console.log('Mature and proud');
break;
default:
console.log('Well, I got some news for you...');
}
}
checkAge(17); // Really young
checkAge(24); // Young and proud
checkAge(31); // Mature and proud
The trick is that each subsequent case value is checked against the expression specified as switch value. Still, as you need to delimit each section with break, I really doubt that's more readable than if-else statement.
You haven't specified a language, but the below construct is possible in languages which take an expression in the switch, viz:
switch (age>18)
{
case true:
// Do over 18 stuff here
break;
case false:
// Do under 18 stuff here
break;
}
Although this works, this wouldn't be an intuitive branching technique so readability of the code may suffer - use of if / then else for branches, or the conditional operator for conditional evaluation would be more common.
in php you can do:
switch($age) {
case ($age < 17):
print "do something";
break;
case ($age > 18): print "over 18"; break;
}
Technically you can put it like that (C# code sample):
switch (age) {
case 0:
case 1:
case 2:
...
case 18:
break;
default: // <- if(age>18)
...
break;
}
but do you really want it? In C#, Java etc. we usually use if else:
if (age <= 5) { // from 0 up to 5
...
}
else if (age <= 10) { // from 5 up to 10
...
}
else if (age <= 18) { // from 10 up to 18
...
}
else { // over 18
...
}
In case of a SQL dialect (where often you don't have if construction) you can put it like that:
case
when (age > 18) then
...
else...
end
for instance
select case
when (age > 18) then
'Over 18'
else
'18 or under 18'
end
from MyTable

Can I jump from one case to another in a switch statements?

For switch statements, is it possible to change the value of the switch inside the switch statement so that it can jump around to different cases?
Ex:
int w = 0;
switch(w)
{
case 1:
doSomething();
w = 3;
case 2:
doSomething();
break;
case 3:
doSomething();
break;
}
Basically what I'm asking is, if I do not place a break statement for a case and I change the value of the switch in the same case, will the code execute both cases?
Yes you can change the value inside switch but it will not execute case for new value until you break in the case where you changed the value.
In your case it will not go in any case as there is no case for 0. But if you change to w = 1 then it will go for case 1 and then for case 2 as you do not have break; but it will not go for case 3.
No, it will not change and will not execute new case statement.
Remember that, once appropriate match is found with the case statement corresponding to a value inside the switch statement, that particular case is executed and once that is executed ( if break is provided after each case to prevent falling through all cases) , then the control returns to the end of switch statement.
Sample Code :
public class A {
public static void main(String [] args) {
int i=1;
switch(i) {
case 1 :
System.out.println("Case 1");
i = 2;
break;
case 2 :
System.out.println("Changed to Case 2");
break;
default:
System.out.println("Default");
break;
}
System.out.println("Final value of i " + i);
}
}
Output :
Case 1
Final value of i 2
Note : Inserting proper breakpoints, try to debug. You will come to know yourself, what exactly is happening.
If we do not give break after each case then Java will start executing the statement from matching case and keep on executing statements for following cases as well, until either a break statement is found or switch statements end is encountered.
If case 1 happens to execute, it's just a fall through to the case 2. And since there is a break in case 2, further fall through doesn't happen. It doesn't jump to case 3 because of the statement w = 3 ; in case 1.
No the switch's case will not change when changing the value in a particular case it checks only once
let a = 1
switch (a) {
case 1:
console.log("Hello from 1")//only this will be printed
a = 2//value of a is changed
break
case 2:
console.log("hello from 2")//this will not execute
a = 3
break
case 3:
console.log("hello from 3")//this will not execute
a = 4
break
case 4:
console.log("hello from 4")//this will not execute
break
default:
console.log("default")
}
console.log(a)//value will be 2

Resources