Author Topic: If else statements  (Read 2063 times)

0 Members and 1 Guest are viewing this topic.

Offline Madsam97

  • NULL
  • Posts: 4
  • Cookies: 0
    • View Profile
If else statements
« on: May 02, 2011, 04:34:07 pm »
I have to write a program for school where i have nested if else statments. Its required so that you can go into the nested statements and stay there until you tell the program to go back to the original if else statements.
Any code snippets or ideas would be wonderful.

Offline I_Learning_I

  • Knight
  • **
  • Posts: 267
  • Cookies: 26
  • Nor black or white, not even grey. What hat am I?
    • View Profile
    • Hacking F0r Fr33
Re: If else statements
« Reply #1 on: May 02, 2011, 06:03:36 pm »
I must say, nesting If's to decide whether the program closes or not doesn't sound much useful.

Also nested if's and else's are usually the lack of the operand && as you can easily make a check in the same line.

Code: [Select]
if(opt1==true){
     if(opt2==true){
       cout<<"alsas";
     }else{
       cout<<"Missing 2"<<endl;
    }
}else{
       cout<<"Missing 1"<<endl;
}

The code above could be coded like:


Code: [Select]
if( (opt1==true) && (opt2==true) ){
   cout<<"alsas";
}else if(opt2!=true){
      cout<<"Missing 2"<<endl;
}else{
      cout<<"Missing 1"<<endl;
}

If you want to perform something untill you get an input you should simply make a while().
Like:

Code: [Select]
while(strncasecmp(option, "exit", 4) != 0){
      //yourcode
}
return 0; // or exit(0);

Anyhow I made some stupid thing, I don't know if it's any helpful, hope it is.

Code: [Select]
int main (int argc, char * argv[]){

bool opt=false;

cout<<"Insert a number"<<endl;

while(opt==false){
cin>>mynum;
if((mynum >0)&& (mynum<100)){
cout<<mynum*mynum<<endl;
cout<<"Do you wish to continue? or exit?"<<endl;
if((strncasecmp(option, "exit", 4)) == 0){
opt=true;
}else if((strncasecmp(option, "continue", 12)) == 0){
cout<<"You can simply insert a number if you wish to continue"<<endl;
}
}

}

return 0;
}
« Last Edit: May 02, 2011, 06:09:27 pm by I_Learning_I »
Thanks for reading,
I_Learning_I

Offline Madsam97

  • NULL
  • Posts: 4
  • Cookies: 0
    • View Profile
Re: If else statements
« Reply #2 on: May 02, 2011, 10:04:09 pm »
Thats actually exactly what I needed. Thanks!

Offline hubl

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: If else statements
« Reply #3 on: May 05, 2011, 12:12:57 am »
Since when was this a homework hub?

DIY

Offline I_Learning_I

  • Knight
  • **
  • Posts: 267
  • Cookies: 26
  • Nor black or white, not even grey. What hat am I?
    • View Profile
    • Hacking F0r Fr33
Re: If else statements
« Reply #4 on: May 07, 2011, 04:09:07 am »
I'm not making anyone's homework, I'm solving an exercise and explaining it, for those that want to learn, read the explanation, and with the homework done, they'll learn, those that don't will have to do it themselves next time.
But hey, at least I'm contributing.
Thanks for reading,
I_Learning_I