Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Other ways to write a switch ?
Forum Updated to NodeBB v4.3 + New Features

Other ways to write a switch ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 253 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ApprenticeRenoA Offline
    ApprenticeRenoA Offline
    ApprenticeReno
    wrote on last edited by
    #1

    Hi guys, i was wondering if there was a better way to write this part of code:

    switch(number){ // int that can contains numbers from 0 to 59. 
          case 0:
            //code that writes my data strings s0,s1,s2,s3....s9
            as[0]=s0+v+s1+v+s2+....s9  // array string that contains my data 
          break;
    

    Instead of doing something like :

    switch(number){ 
          case 0:
            //data strings s0,s1,s2,s3....s9
            as[0]=s0+v+s1+v+s2+....s9  
          break;
     case 1:
            //data strings s0,s1,s2,s3....s9
            as[1]=s0+v+s1+v+s2+....s9  
          break;
     case 2:
            //data strings s0,s1,s2,s3....s9
            as[2]=s0+v+s1+v+s2+....s9  
          break;
    .
    .
    .
     case 59:
            //data strings s0,s1,s2,s3....s9
            as[59]=s0+v+s1+v+s2+....s9  
          break;
    

    Is there a better way to do this ? thank you.

    M 1 Reply Last reply
    0
    • ApprenticeRenoA ApprenticeReno

      Hi guys, i was wondering if there was a better way to write this part of code:

      switch(number){ // int that can contains numbers from 0 to 59. 
            case 0:
              //code that writes my data strings s0,s1,s2,s3....s9
              as[0]=s0+v+s1+v+s2+....s9  // array string that contains my data 
            break;
      

      Instead of doing something like :

      switch(number){ 
            case 0:
              //data strings s0,s1,s2,s3....s9
              as[0]=s0+v+s1+v+s2+....s9  
            break;
       case 1:
              //data strings s0,s1,s2,s3....s9
              as[1]=s0+v+s1+v+s2+....s9  
            break;
       case 2:
              //data strings s0,s1,s2,s3....s9
              as[2]=s0+v+s1+v+s2+....s9  
            break;
      .
      .
      .
       case 59:
              //data strings s0,s1,s2,s3....s9
              as[59]=s0+v+s1+v+s2+....s9  
            break;
      

      Is there a better way to do this ? thank you.

      M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      @ApprenticeReno
      From your code, it seems you do the same addition for all cases (0-59)
      so I don't know why you not simply do:
      as[number]=...

      ApprenticeRenoA 1 Reply Last reply
      3
      • M mpergand

        @ApprenticeReno
        From your code, it seems you do the same addition for all cases (0-59)
        so I don't know why you not simply do:
        as[number]=...

        ApprenticeRenoA Offline
        ApprenticeRenoA Offline
        ApprenticeReno
        wrote on last edited by
        #3

        @mpergand thank you

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved