If else ladder is giving me a headache 😥. Please help out. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

If else ladder is giving me a headache 😥. Please help out.

Please explaining to me what happens when the second if statement's condition is false. Every time I run the code, it of course skips the code Inside the second if statement, but it also doesn't output the else statement the required number of times (which is 13, since the condition used in the loop is the size of the string). Here's the code👇. Please help out. Int main( ) { string str = "Hello There! "; int n = str.size(); cout << n << endl; for(int i = 0; str[i] != '\0'; i++){ if(isalpha(str[i])){ if(str[i] == 'a'){ cout << "Cracked it" << endl; } } else cout << "Oops" << endl; } return 0; }

13th May 2022, 9:28 PM
DevAbdul
DevAbdul - avatar
1 Answer
+ 6
The else-statement belongs to the outer if-statement. It will only be executed if isalpha[i] is false
13th May 2022, 9:41 PM
Lisa
Lisa - avatar