2 Replies - 757 Views - Last Post: 09 September 2013 - 08:46 AM Rate Topic: -----

#1 parthsl   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-September 13

if statement problem.

Posted 09 September 2013 - 02:54 AM

Is there any way in if...else statement so that both the if and else statement gets executed(by applying condition in if statement).
Is This A Good Question/Topic? 0
  • +

Replies To: if statement problem.

#2 jimblumberg   User is online

  • member icon

Reputation: 5525
  • View blog
  • Posts: 17,115
  • Joined: 25-December 09

Re: if statement problem.

Posted 09 September 2013 - 03:58 AM

No only one or the other will execute. If you want both to execute at the same time you don't need an if/else.

Jim
Was This Post Helpful? 1
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: if statement problem.

Posted 09 September 2013 - 08:46 AM

Quote

Is there any way in if...else statement so that both the if and else statement gets executed(by applying condition in if statement).

You can not execute multiple control paths in a if-else, it's a one-hot structure. You can however do work in the conditions, and only execute the paths for error handling.

i.e.
int sockfd;

/* execute all condition work until one of them fails */
if((sockfd = socket(/* params */)) < 0) {
   /* socket error here */
}
else if(bind(sockfd, /* parameters */) < 0) {
   /* bind error */
}
else {
   /* success */
}



This post has been edited by jjl: 09 September 2013 - 08:47 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1