Related Articles

# Output of C programs | Set 50 (Control Structures)

• Difficulty Level : Easy
• Last Updated : 12 Jul, 2019

Here are some C programs which is related to the control structures With the detailed explanation, guess the output of the following programs:-

1.what will be the output of the below program?

 `#include ``int` `main()``{``    ``int` `x = 2;``    ``switch` `(x) {``        ``x--;``        ``switch` `(x) {``        ``case` `1:``            ``printf``(``"Hello"``);``            ``break``;``        ``case` `2:``            ``printf``(``"GFG"``);``            ``break``;``        ``case` `3:``            ``printf``(``"Welcome"``);``            ``break``;``        ``default``:``            ``printf``(``"BYE"``);``        ``}``    ``}``    ``return` `(0);``}`

Options:
1. Hello
2. GFG
3. No Output
4. BYE

`The answer is option(3).`

Explanation:The switch case control structure execute the statement present in the case. But in the above program, the outer switch does not has cases. Therefore, control does not enter inside the switch case. Thus, the program does not give any output and compiler does not produce any error.

2.Guess the correct answer of the mentioned program?

 `#include ``int` `main()``{``    ``int` `x = 2;``    ``switch` `(x) {``    ``case` `1:``        ``printf``(``"Hello"``);``        ``break``;``    ``case` `2:``        ``printf``(``"Hello"``);``        ``break``;``    ``case` `3:``        ``printf``(``"GFG"``);``        ``break``;``    ``default``:``        ``printf``(``"BYE"``);``    ``}``    ``return` `(0);``}`

Options:
1. Abnormal termination
2. Compile time error
3. No output
4. Hello

`The output is option(4).`

Explanation: In the above program, value of x is 2 so case 2 will execute and it will print Hello.
Resource: https://www.geeksforgeeks.org/switch-statement-cc/

3.How many times does the loop run?

 `#include ``int` `main()``{``    ``int` `i;``    ``for` `(i = 1; i <= 100; i++) {``        ``printf``(``"GFG\n"``);``        ``if` `(i == 5)``            ``break``;``    ``}``    ``return` `(0);``}`

Options:
1. 5 times
2. 15 times
3. 10 times
4. 20 times

`The answer is option(1).`

Explanation: Here the loop print GFG five times. Because when the for loop counter value i.e. i is 5, the break statement will transfer the control to outside loop.
Resource:www.geeksforgeeks.org/break-statement-cc/

4.what will be the output?

 `#include ``int` `main()``{``    ``int` `i;``    ``for` `(i = 1; i <= 10; i++) {``        ``printf``(``"welcome\n"``);``        ``continue``;``        ``printf``(``"hii"``);``    ``}``    ``return` `(0);``}`

Options:
1. print welcome 5 times
2. print hii 5 times
3. print hii 10 times
4. print welcome 10 times

`The answer is option(4).`

Explanation : When the control of the for loop reaches continue statement, the control automatically transfers to the beginning of the loop and leave the printf(“hii”); statement.
Resource: https://www.geeksforgeeks.org/continue-statement-cpp/

5.How many time the program will print?

 `#include >``int` `main()``{``    ``int` `i;``    ``for` `(i = 2; i = 100; i += 2) {``        ``printf``(``"Hi\\n"``);``    ``}``    ``return` `(0);``}`

Options:
1. print 2 times
2. Infinitely
3. print 10 times
4. print 5 times

`The answer is option(2).`

Explanation: Because the value 100 is assigned to i every time and it does not check for any condition because here there is no = sign in the for loop. Therefore the loop goes into infinite loop.

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7019474,"math_prob":0.8615322,"size":3221,"snap":"2021-31-2021-39","text_gpt3_token_len":918,"char_repetition_ratio":0.13242151,"word_repetition_ratio":0.17926186,"special_character_ratio":0.30083826,"punctuation_ratio":0.19912152,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96940273,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-27T02:14:26Z\",\"WARC-Record-ID\":\"<urn:uuid:afcabd5b-7265-4e30-ace9-5cc477a79dbd>\",\"Content-Length\":\"121910\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:24602bfb-365a-4957-bb99-f7631d848364>\",\"WARC-Concurrent-To\":\"<urn:uuid:21e06e82-fdaa-4e4e-80a4-7e9a73861167>\",\"WARC-IP-Address\":\"23.205.105.172\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/output-c-programs-set-50-control-structures/?ref=lbp\",\"WARC-Payload-Digest\":\"sha1:GKPSHRQMPA5NIAZY5I2GM4HADUNM3ZM4\",\"WARC-Block-Digest\":\"sha1:QBNTYH4SFYNLDOZCGFNZHA6OFBCJH7RC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780058222.43_warc_CC-MAIN-20210926235727-20210927025727-00636.warc.gz\"}"}